C Program to Implement Lexical Analyzer
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
char buffer[80];
int i,len,pos;
char token[30];
int ibuff;
void skip()
{
for(;buffer[ibuff]==' '||buffer[ibuff]=='\t';ibuff++);
}
void insert(char *a,char x)
{
int i;
i=strlen(a);
a[i]=x;
a[i+1]=0;
}
char terminals[30][10]={{"{"},{"}"},{"("},{")"},{"if"},{"else"},{"while"},
{"int"},{"char"},{"float"},{"="},{"=="},{"+"},{"-"},{"*"},{"/"},
{"%"},{"<"},{"<="},{">"},{">="},{"!="},{"&&"},{"||"},{"!"},
{";"},{"&"},{"|"},{"void"}};
char identifiers[20][20];
int iterminals=29;
int iidentifiers;
int literals[20];
int iliterals=0;
int searchterminals(char *a)
{
for(i=0;i<iterminals;i++)
if(strcmp(terminals[i],a)==0)
return 1;
return 0;
}
void printterminal(char *a)
{
for(i=0;i<iterminals;i++)
if(strcmp(terminals[i],a)==0)
{
printf("\n%10s TRM %d",a,i);
return;
}
}
void printliteral(char *a)
{
int x;
x=atoi(a);
for(i=0;i<iliterals;i++)
if(literals[i]==x)
break;
if(i==iliterals)
{
literals[i]=x;
iliterals++;
}
printf("\n%10s LTS %d",a,i);
}
void printidentifier()
{
int i=0;
for(i=0;i<iidentifiers &&strcmp(token,identifiers[i])!=0;i++);
if(i>=iidentifiers)
{
strcpy(identifiers[iidentifiers],token);
iidentifiers++;
}
printf("\n%10s IDN %d",token,i);
pos=i;
}
int state;
void main()
{
FILE *ptr;
char infile[12];
clrscr();
printf("\n Enter source file name:");
gets(infile);
ptr=fopen(infile,"r");
if(ptr==NULL)
{
printf("\n Could not open the file:");
exit(0);
}
printf("\n Uniform symbol Table");
printf("\n______________________");
while(!feof(ptr))
{
fgets(buffer,80,ptr);
len=strlen(buffer);
buffer[len]='\0';
buffer[len+1]=0;
ibuff=0;
state=0;
skip();
for(;buffer[ibuff]!=0;ibuff++)
{
switch(state)
{
case 0:
switch(buffer[ibuff])
{
case '#':
printf("\npreprocessor directive:%s",buffer);
ibuff=strlen(buffer)-1;
break;
case ' ':skip();ibuff--;break;
case '"':
token[0]=0;
ibuff++;
do
{
insert(token,buffer[ibuff]);
ibuff++;
}while(buffer[ibuff]!='"');
printf("\n string %s",token);
break;
case '\n':break;
case '(':printterminal("(");break;
case ')':printterminal(")");break;
case '}':printterminal("}");break;
case '{':printterminal("{");break;
case ';':printterminal(";");break;
case ',':printterminal(",");break;
case '*':printterminal("*");break;
case '/':printterminal("/");break;
case '%':printterminal("%");break;
case '+':state=70;break;
case '-':state=80;break;
case '=':state=90;break;
case '!':state=100;break;
case '<':state=110;break;
case '>':state=120;break;
case '|':state=130;break;
case '&':state=140;break;
default:
if(isdigit(buffer[ibuff]))
{
token[0]=0;
do
{
insert(token,buffer[ibuff]);
ibuff++;
}while(isdigit(buffer[ibuff]));
printliteral(token);
ibuff--;
}
else
if(isalpha(buffer[ibuff]))
{
token[0]=0;
do
{
insert(token,buffer[ibuff]);
ibuff++;
}while(isalnum(buffer[ibuff]));
if(searchterminals(token))
printterminal(token);
else
printidentifier();
ibuff--;
}
else
{
printf("\n unknown token=%s",buffer);
exit(0);
}
break;
}
break;
case 70:if(buffer[ibuff]=='+')
printterminal("++");
else
{
printterminal("+");
ibuff--;
}
state=0;
break;
case 80:if(buffer[ibuff]!='-')
printterminal("-");
else
{
printterminal("-");
ibuff--;
}
state=0;break;
case 90:if(buffer[ibuff]=='=')
printterminal("==");
else
{
printterminal("=");
ibuff--;
}
state=0;break;
case 100:if(buffer[ibuff]=='=')
printterminal("!=");
else
{
printterminal("!");
ibuff--;
}
state=0;break;
case 110:if(buffer[ibuff]=='=')
printterminal("<=");
else
{
printterminal("<");
ibuff--;
}
state=0;break;
case 120:if(buffer[ibuff]=='=')
printterminal(">=");
else
{
printterminal(">");
ibuff--;
}
state=0;break;
case 130:if(buffer[ibuff]=='|')
printterminal("||");
else
{
printterminal("|");
ibuff--;
}
state=0;break;
case 140:if(buffer[ibuff]=='&')
printterminal("&&");
else
{ printterminal("&");
ibuff--;
}
state=0;break;
}
}
}
printf("\n Press a key to print Symbol Table.....\n");
getch();
for(i=0;i<iidentifiers;i++)
printf("\n %s",identifiers[i]);
printf("\n Pres a key to print Literal Table.......\n");
getch();
for(i=0;i<iliterals;i++)
printf("\n%d",literals[i]);
getch();
}
/*
Input Program
#include<stdio.h>
#include<conio.h>
void str()
{
int x=2,y=3,z;
z=x+y;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
char buffer[80];
int i,len,pos;
char token[30];
int ibuff;
void skip()
{
for(;buffer[ibuff]==' '||buffer[ibuff]=='\t';ibuff++);
}
void insert(char *a,char x)
{
int i;
i=strlen(a);
a[i]=x;
a[i+1]=0;
}
char terminals[30][10]={{"{"},{"}"},{"("},{")"},{"if"},{"else"},{"while"},
{"int"},{"char"},{"float"},{"="},{"=="},{"+"},{"-"},{"*"},{"/"},
{"%"},{"<"},{"<="},{">"},{">="},{"!="},{"&&"},{"||"},{"!"},
{";"},{"&"},{"|"},{"void"}};
char identifiers[20][20];
int iterminals=29;
int iidentifiers;
int literals[20];
int iliterals=0;
int searchterminals(char *a)
{
for(i=0;i<iterminals;i++)
if(strcmp(terminals[i],a)==0)
return 1;
return 0;
}
void printterminal(char *a)
{
for(i=0;i<iterminals;i++)
if(strcmp(terminals[i],a)==0)
{
printf("\n%10s TRM %d",a,i);
return;
}
}
void printliteral(char *a)
{
int x;
x=atoi(a);
for(i=0;i<iliterals;i++)
if(literals[i]==x)
break;
if(i==iliterals)
{
literals[i]=x;
iliterals++;
}
printf("\n%10s LTS %d",a,i);
}
void printidentifier()
{
int i=0;
for(i=0;i<iidentifiers &&strcmp(token,identifiers[i])!=0;i++);
if(i>=iidentifiers)
{
strcpy(identifiers[iidentifiers],token);
iidentifiers++;
}
printf("\n%10s IDN %d",token,i);
pos=i;
}
int state;
void main()
{
FILE *ptr;
char infile[12];
clrscr();
printf("\n Enter source file name:");
gets(infile);
ptr=fopen(infile,"r");
if(ptr==NULL)
{
printf("\n Could not open the file:");
exit(0);
}
printf("\n Uniform symbol Table");
printf("\n______________________");
while(!feof(ptr))
{
fgets(buffer,80,ptr);
len=strlen(buffer);
buffer[len]='\0';
buffer[len+1]=0;
ibuff=0;
state=0;
skip();
for(;buffer[ibuff]!=0;ibuff++)
{
switch(state)
{
case 0:
switch(buffer[ibuff])
{
case '#':
printf("\npreprocessor directive:%s",buffer);
ibuff=strlen(buffer)-1;
break;
case ' ':skip();ibuff--;break;
case '"':
token[0]=0;
ibuff++;
do
{
insert(token,buffer[ibuff]);
ibuff++;
}while(buffer[ibuff]!='"');
printf("\n string %s",token);
break;
case '\n':break;
case '(':printterminal("(");break;
case ')':printterminal(")");break;
case '}':printterminal("}");break;
case '{':printterminal("{");break;
case ';':printterminal(";");break;
case ',':printterminal(",");break;
case '*':printterminal("*");break;
case '/':printterminal("/");break;
case '%':printterminal("%");break;
case '+':state=70;break;
case '-':state=80;break;
case '=':state=90;break;
case '!':state=100;break;
case '<':state=110;break;
case '>':state=120;break;
case '|':state=130;break;
case '&':state=140;break;
default:
if(isdigit(buffer[ibuff]))
{
token[0]=0;
do
{
insert(token,buffer[ibuff]);
ibuff++;
}while(isdigit(buffer[ibuff]));
printliteral(token);
ibuff--;
}
else
if(isalpha(buffer[ibuff]))
{
token[0]=0;
do
{
insert(token,buffer[ibuff]);
ibuff++;
}while(isalnum(buffer[ibuff]));
if(searchterminals(token))
printterminal(token);
else
printidentifier();
ibuff--;
}
else
{
printf("\n unknown token=%s",buffer);
exit(0);
}
break;
}
break;
case 70:if(buffer[ibuff]=='+')
printterminal("++");
else
{
printterminal("+");
ibuff--;
}
state=0;
break;
case 80:if(buffer[ibuff]!='-')
printterminal("-");
else
{
printterminal("-");
ibuff--;
}
state=0;break;
case 90:if(buffer[ibuff]=='=')
printterminal("==");
else
{
printterminal("=");
ibuff--;
}
state=0;break;
case 100:if(buffer[ibuff]=='=')
printterminal("!=");
else
{
printterminal("!");
ibuff--;
}
state=0;break;
case 110:if(buffer[ibuff]=='=')
printterminal("<=");
else
{
printterminal("<");
ibuff--;
}
state=0;break;
case 120:if(buffer[ibuff]=='=')
printterminal(">=");
else
{
printterminal(">");
ibuff--;
}
state=0;break;
case 130:if(buffer[ibuff]=='|')
printterminal("||");
else
{
printterminal("|");
ibuff--;
}
state=0;break;
case 140:if(buffer[ibuff]=='&')
printterminal("&&");
else
{ printterminal("&");
ibuff--;
}
state=0;break;
}
}
}
printf("\n Press a key to print Symbol Table.....\n");
getch();
for(i=0;i<iidentifiers;i++)
printf("\n %s",identifiers[i]);
printf("\n Pres a key to print Literal Table.......\n");
getch();
for(i=0;i<iliterals;i++)
printf("\n%d",literals[i]);
getch();
}
/*
Input Program
#include<stdio.h>
#include<conio.h>
void str()
{
int x=2,y=3,z;
z=x+y;
}