Implementation Of Lexical Analyzer Algorithm Source C/C++ Program

Algorithm Source code For Lexical Analyzer CS342 Compiler Lab
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size 128
#define NONE -1
#define EOS '\0'
#define NUM 256
#define keyword 257
#define punt 258
#define id 259
#define assign 260
#define arith 200
#define rel_op 261
#define DONE 262
#define MAX 999
char buffer[size],lexemes[MAX];
int lastchar=-1,lastentry=0,tokenval=NONE,lineno=1;
struct entry
{
char *lexptr;
int token;
}symtable[100];
struct entry keywords[]={"if",keyword,"else",keyword,"for",keyword,"int",keyword,"float",
keyword,"double",keyword,"struct",keyword,"return",keyword,0,0};
int look_up(char s[])
{
int k;
for(k=lastentry;k>0;k=k-1)
if(strcmp(symtable[k].lexptr,s)==0)
return k;
return 0;
}
int insert(char s[],int tok)
{
int len;
len=strlen(s);
lastentry=lastentry+1;
symtable[lastentry].token=tok;
symtable[lastentry].lexptr=&lexemes[lastchar+1];
lastchar=lastchar+len+1;
strcpy(symtable[lastentry].lexptr,s);
return lastentry;
}
void initialize()
{
struct entry *ptr;
for(ptr=keywords;ptr->token;ptr++)
insert(ptr->lexptr,ptr->token);
}
int lexer()
{
int t,val,i=0;
while(1)
{
t=getchar();
if(t==' '||t=='\t')
;
else if(t=='\n')
lineno=lineno+1;
else if(t=='('||t==')'||t==','||t==';'||t=='{'||t=='}')
return punt;
else if(t=='<'||t=='>')
return rel_op;
else if(t=='+'||t=='-'||t=='*'||t=='/')
return arith;
else if(t=='=')
return assign;
else if(isdigit(t))
{
ungetc(t,stdin);
scanf("%d",&tokenval);
return NUM;
}
else if(isalpha(t))
{
while(isalnum(t))
{
buffer[i]=t;
t=getchar();
i=i+1;
}
buffer[i]=EOS;
if(t!=EOF)
ungetc(t,stdin);
val=look_up(buffer);
if(val==0)
val=insert(buffer,id);
tokenval=val;
return symtable[val].token;
}
else if(t==EOF)
return DONE;
else
{
tokenval=NONE;
return t;
}
}
}
void main()
{
int lookahead;
char ans;
clrscr();
initialize();
printf("\n Enter the expression \n Press ctrl z to terminate...\n");
lookahead=lexer();
while(lookahead!=DONE)
{
if(lookahead==NUM)
printf("\n Number:%d",tokenval);
if(lookahead==arith)
printf("\n Arithmetic Operator");
if(lookahead==punt)
printf("\n Punctuation Symbol");
if(lookahead==id)
printf("\n Identifier:%s",symtable[tokenval].lexptr);
if(lookahead==keyword)
printf("\n Keyword");
if(lookahead==assign)
printf("\n Assignment operator");
if(lookahead==rel_op)
printf("\n Relational operator");
lookahead=lexer();
}
}

Implementation of lexical analyzer using LEX Algorithm CS342 Compiler Lab

%{
#include<stdio.h>
%}
%%
[0-9]+ {printf("\n %s is a number \n",yytext);}
int |
float |
long |
double |
case |
switch |
break |
if |
else |
exit |
continue |
struct |
const |
atof |
atoi |
typedef |
return |
printf |
scanf |
void {printf("\n %s is the keyword \n",yytext);}
[a-zA-Z][a-zA-Z0-9]* {printf("\n %s is an identifier \n",yytext);}
= {printf("\n %s is an assignment operator\n",yytext);}
\>= |
\> |
\< |
== {printf("\n %s is a relational operator\n",yytext);}
\+ |
\- |
\* |
\/ {printf("\n %s is an arithmetic operator\n",yytext);}
; |
"(" |
")" |
"{" |
"}" {printf("\n %s is a symbol\n",yytext);}
"/*" {printf("\n %s is a comment line\n",yytext);}
%%
int main(int argc,char **argv)
{
if(argc>1)
{
FILE *file;
file=fopen(argv[1],"r");
if(!file)
{
printf("Could not open %s",argv[1]);
exit(0);
}
yyin=file;
}
yylex();
printf("\n\n");
return 0;
}


Post a Comment

0 Comments