Implementing Lexical Analyser in C++

Here is C++ implementation of Lexical analyser (a phase of compiler).

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void main()
{
	int a,b,s,tc,t2;
	tc=0;
	s=50;
	char curr,c1,c2[10];
	FILE *l, *i, *t, *o, *db, *u;
	l= fopen("D:\\an\\literal.txt","w");
	i= fopen("D:\\an\\identifier.txt","w");
	t= fopen("D:\\an\\terminal.txt","w");
	u= fopen("D:\\an\\ust.txt","w");
	db= fopen("D:\\an\\db.txt","r");

	cout<<"\n Enter a string:";
	char st[40];
	for(a=0;a<s;a++)
	{
		cin>>st[a];
		if(st[a]=='$')
		{
			break;
		}
	}

	cout<<"\n Entered String:";
	for(a=0;a<s;a++)
	{
		if(st[a]=='$')
		{
			break;
		}
		cout<<st[a];
	}

	for(a=0;a<s;a++)
	{
		//Scan if it is in db
		curr=st[a];
		if(st[a]=='$')
		{
			break;
		}

		db=fopen("D:\\an\\db.txt","r");
		while(1==1)
		{
			if(feof(db))
			{
				break;
			}
			fscanf(db,"%c",&c1);
			if(c1==curr)
			{
				//cout<<"\n Found";
				fprintf(t,"\n%i %c",tc,c1);
				tc++;
				break;
			}
		}
	}

	tc=0;

	for(a=0;a<s;a++)
	{
		curr=st[a];
		if(st[a]=='$')
		{
			break;
		}

		if(isalpha(curr))
		{
			fprintf(i,"\n%i %c",tc,curr);
			tc++;
		}
	}

	tc=0;

	for(a=0;a<s;a++)
	{
		curr=st[a];
		if(st[a]=='$')
		{
			break;
		}

		if(isdigit(curr))
		{
			
			fprintf(l,"\n");
			fprintf(l,"%i %c",tc,curr);
			
			b=a;
			b++;
			if(b<s && isdigit(st[b]))
			{
				fprintf(l,"%c",st[b]);
				a=b;
			}

			b++;
			if(b<s && isdigit(st[b]))
			{
				fprintf(l,"%c",st[b]);
				a=b;
			}

			tc++;
		}
	}

	fclose(l);
	fclose(i);
	fclose(t);
	fclose(db);

	cout<<"\n Tokens Generated\n";
	cout<<"\n Uniform symbol table";
	cout<<"\nToken\tType\tPointer\n";
	//building uniform symbol table
	l= fopen("D:\\an\\literal.txt","r");
	i= fopen("D:\\an\\identifier.txt","r");
	t= fopen("D:\\an\\terminal.txt","r");
	for(a=0;a<20;a++)
	{
		if(!feof(l)&&fgetc(l)!=-1)
		{
			fscanf(l,"%i %s",&t2,&c2);
			fprintf(u,"%s LIT %i\n",c2,t2);
			printf("%s\tLIT\t%i\n",c2,t2);
		}
		if(!feof(i)&&fgetc(i)!=-1)
		{
			fscanf(i,"%i %s",&t2,&c2);
			fprintf(u,"%s IDN %i\n",c2,t2);
			printf("%s\tIDN\t%i\n",c2,t2);
		}
		if(!feof(t)&&fgetc(t)!=-1)
		{
			fscanf(t,"%i %s",&t2,&c2);
			fprintf(u,"%s TER %i\n",c2,t2);
			printf("%s\tTER\t%i\n",c2,t2);
		}
	}
	
	fclose(u);
	fclose(i);
	fclose(t);
	fclose(l);
	cout<<"\n Uniform Symbol table constructed";

	getch();
}

/* OUTPUT


 Enter a string:a=b+c*(60/j)+k*80$

 Entered String:a=b+c*(60/j)+k*80
 Tokens Generated

 Uniform symbol table
Token   Type    Pointer
60      LIT     0
a       IDN     0
=       TER     0
80      LIT     1
b       IDN     1
+       TER     1
c       IDN     2
*       TER     2
j       IDN     3
(       TER     3
k       IDN     4
/       TER     4
)       TER     5
+       TER     6
*       TER     7

 Uniform Symbol table constructed

*/

Leave a Reply

Your email address will not be published. Required fields are marked *