Lexical analyzer Code in C Language
Implement Lexical Analyzer code for subset of C using C Language.
Lexical Analysis-Finite Automate, Regular Expression, RE to DFA,Implementation of lexical Analyzer,Syntax Analysis,Context Free Grammars ,Derivation of Parse Tress,Parsers,Top Down Parsers: Recursive Descent Parser, Predictive Parser,Bottom Up Parsing Shift Reduce Parser, SLR parser and Lexical analyzer Code.
Lexical analyzer Code
#include
#include
#include
#include
#define max 100
typedef struct lit_tab
{
char token[max];
}littab;
typedef struct iden_tab
{
char token[max];
}identab;
typedef struct ust
{
char token[max];
char type;
int pos;
}ustx;
littab lit_tab[50];
identab iden_tab[50];
ustx ust[100];
int ust_ptr=0;
char terminal[max][max]={"{","}","(",")","<",">","<=",">=","=",",",";","\"","+","-","*","/","void","printf","int","float","if","else"};
void print_ust()
{
printf("UST\n");
int i=0;
printf("TOKEN\tTYPE\tPOS\n");
while(i<ust_ptr) <br=""> {
printf("%s\t%c\t%d\n",ust[i].token,ust[i].type,ust[i].pos);
i++;
getch();
}
}
void print_ter()
{
printf("Literal Table");
int i=0;
clrscr();
while(strcmp(terminal[i],NULL)!=0)
{
printf("%d.%s\n",i+1,terminal[i]);
i++;
}
getch();
}
void print_lit()
{
printf("\nLiteral Table");
int i=0;
clrscr();
while(strcmp(lit_tab[i].token,NULL)!=0)
{
printf("%d.%s\n",i+1,lit_tab[i].token);
i++;
}
getch();
}
void print_iden()
{
printf("\nIdentifier Table");
int i=0;
clrscr();
while(strcmp(iden_tab[i].token,NULL)!=0)
{
printf("%d.%s\n",i+1,iden_tab[i].token);
i++;
}
getch();
}
int search_lit(char str[])
{
int i=0;
while(strcmp(lit_tab[i].token,NULL)!=0)
{
if(strcmp(lit_tab[i].token,str)==0)
return i;
i++;
}
return -1;
}
int insert_lit(char str[])
{
int i=0;
while(strcmp(lit_tab[i].token,NULL)!=0)
{
i++;
}
strcpy(lit_tab[i].token,str);
return i;
}
int search_iden(char str[])
{
int i=0;
while(strcmp(iden_tab[i].token,NULL)!=0)
{
if(strcmp(iden_tab[i].token,str)==0)
return i;
i++;
}
return -1;
}
int insert_iden(char str[])
{
int i=0;
while(strcmp(iden_tab[i].token,NULL)!=0)
{
i++;
}
strcpy(iden_tab[i].token,str);
return i;
}
int search(char str[])
{
int i=0;
while(strcmp(terminal[i],NULL)!=0)
{
if(strcmp(terminal[i],str)==0 )
return i;
i++;
}
return -1;
}
void ust_insert(char token[],char type,int pos)
{
strcpy(ust[ust_ptr].token,token);
ust[ust_ptr].type=type;
ust[ust_ptr].pos=pos;
ust_ptr++;
}
void main()
{
FILE *fp;
clrscr();
fp=fopen("lex.txt","r");
if(fp==NULL)
{
printf("File Not Found");
return;
}
char str[max],token[max];
int i,j;
while( !feof(fp) )
{
fgets(str,max,fp);
i=0;
strcpy(token,NULL);
while(str[i]!='\n' && str[i]!=NULL)
{
while(str[i]==' ' || str[i]=='\t')
i++;
j=0;
if(str[i]=='/')
{
if(str[i+1]=='/')
{
fgets(str,max,fp);
continue;
}
else if(str[i+1]=='*')
{
i+=2;
while(str[i]!='*' && str[i+1]!='/')
{
if(str[i]=='\n')
{
fgets(str,max,fp);
i=0;
}
i++;
}
i+=2;
}
}
while(str[i]!=' ' && str[i]!='\t' && str[i]!='\n')
{
if(str[i-1]=='"' && str[i]!=')')
{
while(str[i]!='"')
{
token[j]=str[i];
j++;i++;
}
break;
}
token[j]=str[i];
j++;
i++;
if(ispunct(str[i]))
//if(str[i]=='(' || str[i]==')' ||str[i]==';' ||str[i]==','||str[i]=='+'||str[i]=='='||str[i]=='-'||str[i]=='/'||str[i]=='*')
break;
if(ispunct(str[i-1]) )
//if(str[i-1]==','||str[i-1]=='='||str[i-1]=='/'||str[i-1]=='+'||str[i-1]=='-'||str[i-1]=='*')
break;
}
token[j]=NULL;
int x;
int l=strlen(token);
int pos;
x=search(token);
if(x!=-1)
ust_insert(token,'T',x);
else if(str[i-l-1]!='"' && !(token[0]>='0'&& token[0]<='9')&&strcmp(token,NULL)!=0 )
{
x=search_iden(token);
if(x==-1)
{
pos=insert_iden(token);
ust_insert(token,'I',pos);
}
else
ust_insert(token,'I',x);
}
else if( strcmp(token,NULL)!=0)
{
x=search_lit(token);
if(x==-1)
{
pos=insert_lit(token);
ust_insert(token,'L',pos);
}
else
ust_insert(token,'L',x);
}
}
}
print_ter();
print_lit();
print_iden();
print_ust();
getch();
}


Leave a Reply