0% found this document useful (0 votes)
223 views

Compiler Design Lab Manual (Compiler Design Lab File) PDF

The document describes developing a lexical analyzer to recognize patterns in PASCAL and C, such as identifiers, constants, comments, and operators. It includes code for defining token types, a symbol table, and functions for looking up, inserting, and analyzing tokens. The main function initializes the symbol table, gets user input, and uses the lexical analyzer to print the token type and value for each token.

Uploaded by

manojjangir420
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views

Compiler Design Lab Manual (Compiler Design Lab File) PDF

The document describes developing a lexical analyzer to recognize patterns in PASCAL and C, such as identifiers, constants, comments, and operators. It includes code for defining token types, a symbol table, and functions for looking up, inserting, and analyzing tokens. The main function initializes the symbol table, gets user input, and uses the lexical analyzer to print the token type and value for each token.

Uploaded by

manojjangir420
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11/15/2014

CompilerDesignLabManual(CompilerDesignLabFile)

1.DevelopalexicalanalyzertorecognizeafewpatternsinPASCALandC.
(Ex.identifiers,constants,comments,operatorsetc.)
#include
#include
#include
#include
#include
#defineSIZE128
#defineNONE1
#defineEOS\0
#defineNUM256
#defineKEYWORD257
#definePAREN258
#defineID259
#defineASSIGN260
#defineREL_OP261
#defineDONE262
#defineMAX999
charlexemes[MAX]
charbuffer[SIZE]
intlastchar=1
intlastentry=0
inttokenval=NONE
intlineno=1
structentry
{
char*lexptr
inttoken
}symtable[100]
structentrykeywords[]={if,KEYWORD,else,KEYWORD,for,KEYWORD,
data:text/htmlcharset=utf8,%3Cp%20class%3D%22Style1%22%20style%3D%22margin%3A%201em%200px%3B%20outline%3A%20none%3B%20paddin

1/6

11/15/2014

CompilerDesignLabManual(CompilerDesignLabFile)

int,KEYWORD,float,KEYWORD,double,KEYWORD,char,KEYWORD,
struct,KEYWORD,return,KEYWORD,0,0}
voidError_Message(char*m)
{
fprint(stderr,line%d:%s,lineno,m)
exit(1)
}
intlook_up(chars[])
{
intk
for(k=lastentryk>0k)
if(strcmp(symtable[k].lexptr,s)==0)
returnk
return0
}
intinsert(chars[],inttok)
{
intlen
len=strlen(s)
if(lastentry+1>=MAX)
Error_Message(SymbolTableisFull)
if(lastchar+len+1>=MAX)
Error_Message(LexemesArrayisFull)
lastentry++
symtable[lastentry].token=tok
symtable[lastentry].lexptr=&lexemes[lastcher+1]
lastchar=lastchar+len+1
strcpy(smtable[lastentry].lexptr,s)
returnlastentry
}
voidInitialize()
data:text/htmlcharset=utf8,%3Cp%20class%3D%22Style1%22%20style%3D%22margin%3A%201em%200px%3B%20outline%3A%20none%3B%20paddin

2/6

11/15/2014

CompilerDesignLabManual(CompilerDesignLabFile)

{
structentry*ptr
for(ptr=keywordsptr>tokenptr++)
insert(ptr>lexptr,ptr>token)
}
intlexer()
{
intt
intval,i=0
while(1)
{
t=getchar()
if(t==||t==\t)
elseif(t==\n)
lineno++
elseif(t==(||t==))
returnPAREN
elseif(t==<||t==>||t==<=||t==>=||t==!=)
returnREL_OP
elseif(t===)
returnASSIGN
elseif(isdigit(t))
{
ungetc(t,stdin)
scanf(%d,&tokenval)
returnNUM
}
elseif(isalpha(t))
{
while(isalnum(t))
{
data:text/htmlcharset=utf8,%3Cp%20class%3D%22Style1%22%20style%3D%22margin%3A%201em%200px%3B%20outline%3A%20none%3B%20paddin

3/6

11/15/2014

CompilerDesignLabManual(CompilerDesignLabFile)

buffer[i]=t
t=getchar()
i++
if(i>=SIZE)
Error_Message(compilererror)
}
buffer[i]=EOS
if(t!=EOF)
ungetc(t,stdin)
val=look_up(buffer)
if(val==0)
val=insert(buffer,ID)
tokenval=val
returnsymtable[val].token
}
elseif(t==EOF)
returnDONE
else
{
tokenval=NONE
returnt
}
}
}
voidmain()
{
intlookahead
charans
clrscr()
printf(\n]t]tProgramforLexicalAnalysis\n)
data:text/htmlcharset=utf8,%3Cp%20class%3D%22Style1%22%20style%3D%22margin%3A%201em%200px%3B%20outline%3A%20none%3B%20paddin

4/6

11/15/2014

CompilerDesignLabManual(CompilerDesignLabFile)

Initialize()
printf(\nEntertheexpressionandputattheend)
printf(\nPressCtrl+Ztoterminate...\n)
lookahead=lexer()
while(lookahead!=DONE)
{
if(lookahead==NUM)
printf(\nNumber:%d,tokenval)
if(lookahead==+||lookahead==||lookahead==*||lookahead==/)
printf(\nOperator)
if(lookahead==PAREN)
printf(\nParentesis)
if(lookahead==ID)
printf(\nIdentifier:%s,
symtable[tokenval].lexptr)
if(lookahead==KEYWORD)
printf(\nKeyword)
if(lookahead==ASSIGN)
printf(\nAssignmentOperator)
if(lookahead==REL_OP)
printf(\nRelataionalOperator)
lookahead=lexer()
}
}

OUTPUT:
ProgramforLexicalAnalysis
Entertheexpressionandputattheend
PressCtrl+Ztoterminate...
2+3
Number:2
data:text/htmlcharset=utf8,%3Cp%20class%3D%22Style1%22%20style%3D%22margin%3A%201em%200px%3B%20outline%3A%20none%3B%20paddin

5/6

11/15/2014

CompilerDesignLabManual(CompilerDesignLabFile)

Operator
Number:3
if(a
Keyword
Parenthesis
Identifier:a
RelationalOperator
Identifier:b
Parenthesis
Identifier:a
AssigmentOperator
Identifier:a
Operator
Identifier:b
^Z

data:text/htmlcharset=utf8,%3Cp%20class%3D%22Style1%22%20style%3D%22margin%3A%201em%200px%3B%20outline%3A%20none%3B%20paddin

6/6

You might also like