It's a program that works sort of like a lexical analyzer, but it's just supposed to pull any given data from a .txt file and display it. Nothing else. I'm not satisfied with the array I have so far.

/****************************************************************
Array holding all keywords for checking.
*****************************************************************/

char 
*keywords[]={"procedure","is","begin","end","var","cin","cout","if",
          "then","else","and","or","not","loop","exit","when",
          "while","until"};

/****************************************************************
Array holding all arithmetic operations for checking.
*****************************************************************/

char arithmetic_operator[]={'+','-','*','/'};

/****************************************************************
Array holding all comparison operations for checking.
*****************************************************************/

char *comparison_operator[]={"<",">","=","<=","<>",">="};

/****************************************************************
Array holding all special for checking.
*****************************************************************/

char special[]={'%','!','@','~','$'};

I didn't know how to create an array that will just pull anything from any given text file, so that's why it checks for different things. How can I create such an array? The input files look similar to this one:

class A {
int a[11],x,y,z;
char *oneString;
public:
A() { oneString= new char[100]; }
A() { delete oneString; }
void f();
};
void A::f() {
int temp;
x=y+1;
z=x+2; z= x*x;
}

Recommended Answers

All 8 Replies

I am not sure if I understand your question correctly. Do you mean you want to read a file into an array of words? or lines?
Anyway, why use arrays instead of vector?
I would do it like this

#include <fstream>
#include <string>
#include <vector>

int main()
{
   std::vector<std::string> words;
   std::ifstream in("filetoread.txt");
   std::string s;
   while(in >> s)
       words.push_back(s);
}

hope this helped

I am not sure if I understand your question correctly. Do you mean you want to read a file into an array of words? or lines?
Anyway, why use arrays instead of vector?
I would do it like this

#include <fstream>
#include <string>
#include <vector>

int main()
{
   std::vector<std::string> words;
   std::ifstream in("filetoread.txt");
   std::string s;
   while(in >> s)
       words.push_back(s);
}

hope this helped

Yes, just like the example. It's C++ reserved words, variables etc. One per line. Ok, this would do it too. Opens the file and gets the data, right?

of course it would. avoid using char[] use std::string. Avoid using T[], use std::vector<T>

for example you can test two strings for equality with ==, unlike char[].

you can iterate over a vector (not the best way, but the easiest) the following way:
vector<int> my_array;
//fill data
for(int i = 0; i<my_array.size(); ++i)
{
do something with my_array
}

if you have any questions about vectors or anything else feel free to contact me via email lordn3mrod@gmail.com or post here

your keywords array, for example would look like
std::vector<std::string> keywords;
keywords.push_back("if");
keywords.push_back("else");
keywords.push_back("for");
....

special would be
std::vector<char> special;
special.push_back('&');
...

Thank you. I will have to modify other parts too, then.

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>

/****************************************************************
Functions prototype.
*****************************************************************/

void   Open_File();
void   Demage_Lexeme();
int    Search(char[256],int);
void   analyze();
void   Skip_Comment();
void   Read_String();
void   Is_Keyword_Or_Not();
void   Is_Identifier_Or_Not();
void   Is_Operator_Or_Not();
void   Read_Number();
void   Is_Special_Or_Not();
void   Is_Comparison_Or_Not();
void   Add_To_Lexical (char[256],int,char[256]);
void   Print_ST();
void   Print_TOKEN();
void   Token_Attribute();

/****************************************************************
Data structure used in program.
*****************************************************************/

struct lexical
{
    char    data[256];          //Value of token.
    int     line[256];          //Line # which token appear in input 
file.
    int     times;              //# of times that token appear in input
file.
    char    type[256];           //Type of each token.
    struct  lexical *next;
};

typedef struct lexical Lex;
typedef Lex *lex;

/****************************************************************
File pointer for accessing the file.
*****************************************************************/

FILE *fp;
FILE *st;
FILE *token;
char lexeme[256],ch;
int f,flag,line=1,i=1;
lex head=NULL,tail=NULL;

/****************************************************************
Array holding all keywords for checking.
*****************************************************************/

char 
*keywords[]={"procedure","is","begin","end","var","cin","cout","if",
		  "then","else","and","or","not","loop","exit","when",
		  "while","until"};

/****************************************************************
Array holding all arithmetic operations for checking.
*****************************************************************/

char arithmetic_operator[]={'+','-','*','/'};

/****************************************************************
Array holding all comparison operations for checking.
*****************************************************************/

char *comparison_operator[]={"<",">","=","<=","<>",">="};

/****************************************************************
Array holding all special for checking.
*****************************************************************/

char special[]={'%','!','@','~','$'};

/****************************************************************

			**************
			*MAIN PROGRAM*
			**************

*****************************************************************/

void main()
{
  Open_File();
  analyze();
  fclose(fp);
  Print_ST();
  Print_TOKEN();
}

/****************************************************************
This function open input source file.
*****************************************************************/

void Open_File()
{

  fp=fopen("source.txt","r");   //provide path for source.txt here
  if(fp==NULL)
  {
	printf("!!!Can't open input file - source.txt!!!");
	getch();
	exit(0);
  }
}

/****************************************************************
Function to add item to structure of array to store data and
information of lexical items.
*****************************************************************/

void Add_To_Lexical (char value[256],int line,char type[256])
{
	lex new_lex;

	if (!Search(value,line))    //When return 1 the token not found.
	{

	  new_lex=malloc(sizeof(Lex));

	  if (new_lex!=NULL)
	  {
		strcpy(new_lex->data,value);
		new_lex->line[0]=line;
		new_lex->times=1;
		strcpy(new_lex->type,type);
		new_lex->next=NULL;

		if (head==NULL)
		   head=new_lex;
		else
		   tail->next=new_lex;

		tail=new_lex;
	  }
	}
}

/****************************************************************
Function to search token.
*****************************************************************/

int Search (char value[256],int line)
{
  lex x=head;
  int flag=0;

  while (x->next!=NULL && !flag)
  {
    if (strcmp(x->data,value)==0)
    {
      x->line[x->times]=line;
      x->times++;
      flag=1;
    }
    x=x->next;
  }
  return flag;
}

/****************************************************************
Function to print the ST.TXT .
*****************************************************************/

void Print_ST()
{
  lex x=head;
  int j;

  if ((st=fopen("ST.TXT","w"))==NULL)
      printf("The file ST.TXT cat not open. 
");

  else

  {
    fprintf(st,"	 %s 	    %s 	 %s 
","Line#","Lexeme","Type");
    fprintf(st,"	 ---- 	    ------ 	 ---- 
");

    while (x!=NULL)
    {
      if ((strcmp(x->type,"num")==0)         ||
	 (strcmp(x->type,"keyword")==0)      ||
	 (strcmp(x->type,"identifier")==0))
      {
	 fprintf(st,"	 ");

	 for (j=0;j<x->times;j++)
	 {
	   fprintf(st,"%d",x->line[j]);
		if (j!=x->times-1)      //This condition to prevent the comma
	   fprintf(st,",",x->line[j]);  //"," to not print after last line #.
	 }

	fprintf(st,"	    %-6s   	%-6s 
",x->data,x->type);
      }
      x=x->next;
    }

    fclose(st);
  }
}

/****************************************************************
Function to print the TOKENS.TXT .
*****************************************************************/

void Print_TOKEN()
{
  int flag=0;

  fp=fopen("source.txt","r");

    if(fp==NULL)
    {
       printf("!!!Can't open input file - source.txt!!!");
       getch();
       exit(0);
    }

  else

    {
	if ((token=fopen("TOKENS.TXT","w"))==NULL)
	  printf("The file ST.TXT cat not open. 
");

      else

      {
	ch=fgetc(fp);

	while (!(feof(fp)))
	{

	  if (ch==' ' && !flag)
	  {
	    do
	      ch=fgetc(fp);
	    while (ch==' ');

	    fseek(fp,-2,1);
	    ch=fgetc(fp);
	    flag=1;
	  }

	  if (ch!='
' && ch!='	')
	    fprintf(token,"%c",ch);

	  if (ch=='
')
	  {
	    fprintf(token,"
");
	    Token_Attribute();
	    i++;
	    flag=0;
	  }

	  ch=fgetc(fp);
	}
      }
    }
    fclose(fp);
    fclose(token);
}

/****************************************************************
Function to put the token and atrribute in TOKENS.TXT .
*****************************************************************/

void Token_Attribute()
{
  lex x=head;
  int j;

  while (x!=NULL)
  {
    if (x->line[0]==i)
    {
      fprintf(token,"token : %-4s	",x->type);

      if ((strcmp(x->type,"num")==0)         ||
	 (strcmp(x->type,"keyword")==0)      ||
	 (strcmp(x->type,"identifier")==0))

      {
	  fprintf(token,"attribute : line#=%-4d 
",i);
      }

      else

      {
	  fprintf(token,"attribute : %-4s 
",x->data);
      }

    }
    x=x->next;
  }
  fprintf(token,"
");
}


/****************************************************************
This function read all character of strings.
*****************************************************************/

void Read_String()
{
  int j=0;

  do
  {
    lexeme[j++]=ch;
    ch=fgetc(fp);
  } while(isalpha(ch));

    fseek(fp,-1,1);
    lexeme[j]='

Not sure if this was the most efficent way to do it. As you can see, it adds items to structure of the array. I thought it was easier just to store it in an array. So I would just need the print function basically, and the vector. Is that what you mean?

oops, it just occurred to me... are you writing in C or C++? 'Cause there are no vector's and strings in C... and, if it is C you are using then you've posted in a wrong thread :)

Sorry. I posted the code now. Didn't know vectors. This would probably be a better fit then.

include <string>
#include <fstream>
#include <iostream>
 
int main()
{
	// Open file for input
	std::ifstream ifs("input.txt");
 
	std::string line; // string to contain each line
 
	
	while(std::getline(ifs, line))
	{
		// deal with each line here...
		std::cout << line << std::endl;
	}
 
	return 0;
}

I just don't see why you posted so much code here...
if you wanna do it by an array, you can do it like this

char words[maxwordcount][maxwordlength];

int wordcount = 0;
while(inputstream >> words[wordcount ++]);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.