How to do lexical analysis for c/c++ source file?

0

How to do lexical analysis for c/c++ source file accepts as input?

i have c++ code for Lexical analysis to get result of Expression/String as input and produces output as list of tokens that are: Total Identifier, Total Constants, Total Literals, Total Keywords, Total Operators, Total Delimiters, Invalid Tokens & Used Tokens.
So, i want to know same for a C/C++ file, then how to do?

How am passing the expression for analysis as below:

void main()
{
	clrscr();
	char mstr[50];
	printf("\n\n\tEnter String: ");// Here i want file as input
	gets(mstr);
	Lex_Ana oblex(mstr);//Here how to pass all lines to analysis
	oblex.Find_Lex();
	oblex.Rem_Dup();
	oblex.Sep_Val();
	oblex.Display();
	getch();
}

i tried like below:

void main()
{
	clrscr();
	ifstream indata;
	char file_name[40], *mainStr,*ch;
	printf("\n\n\tEnter File name: ");
	gets(file_name);
	indata.open(file_name);
	if(indata==NULL)
	{
		perror("Error while opening the file.\n");
		exit(EXIT_FAILURE);
	}
	else
	{
		while(!indata.eof())
		{
			getline(indata,ch);
			strcat(mainStr,ch);
		}
		Lex_Ana oblex(mainStr);
		oblex.Find_Lex();
		oblex.Rem_Dup();
		oblex.Sep_Val();
		oblex.Display();
		indata.close();
	}
	getch();
}

this is not working..

Note: am using Turbo c++ compiler
Any help would be appreciated..

Tags: asked April 17, 2013

Your Answer