Compiler Design Lab Manual
Compiler Design Lab Manual
EXPERIMENT 1
1. Problem Statement: Implement the Lexical analyzer using JLex, flex or other lexical
Analyzer generating tools.
AIM: Write a LEX Program to scan reserved word & Identifiers of C Language
ALGORITHM / PROCEDURE:
Input : LEX specification files for the token
Output : Produces the source code for the Lexical Analyzer with the name lex.yy.c
and displays reserved word & Identifiers from an input file.
1. Start
2. Open a file in texteditor
3. Create a Lex specifications file to accept keywords, identifiers, constants, operators
and relational operators in the followingformat.
a) %{
Definition of constant /header files
%}
b) RegularExpressions
%%
Transition rules
%%
c) Auxiliary Procedure (main( )function)
4. Save file with .l extension e.g.mylex.l
5. Calllextoolontheterminale.g.[root@localhost]#lexmylex.l.Thislextoolwill
convert
9. Stop
1
Compiler Design Lab Manual MRITS
%{
#include<stdio.h>
%}
letter [a-zA-Z_]
digit [0-9]
special [;:./\,&'$#^@~?]
%%
(auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|
int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|
void|volatile|while)* {printf("\n%s is not an Identifier\nReason: Cannot be a
Keyword!",yytext);}
%%
int main()
{
yylex();
}
int yywrap()
{
return 0;
}
Output:
2
Compiler Design Lab Manual MRITS
%{
#include<stdio.h>
%}
letter[a-zA-Z_]
digit[0-9]
%%
(auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|
goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|
union|unsigned|void|volatile|while)* {printf("\n%s is a keyword!",yytext);}
%%
int main()
{
yylex();
}
int yywrap()
{
return 0;
}
Output:
Viva Questions:
1. List the different sections available in LEX compiler?
2. What is an auxiliary definition?
3. How can we define the translation rules?
4. What is regular expression?
5. What is finite automaton?
6. What is Jlex?
7. What is Flex?
3
Compiler Design Lab Manual MRITS
ALGORITHM / PROCEDURE :
Step1: Start
Step2: Declare a character array w[10] and Z asan array
Step3: Enter the string with $ at theend
Step4:
if(A(w[z])thenincrementzandcheckfor(B(w[z]))andifsatisfiesincrementzandch
eckfor ‘d’ifdispresentthenincrementandcheckfor(D(w[z]))
Step5: if step 4 is satisfied then the string isaccepted Else string isnot
Step6: Exit
SOURCE CODE :
if(top>=20)
{
printf("stack overflow");
}
else
{
stk[++top]=c;
}
}
void pop()
{
if(top==-1)
{
printf("stack underflow");
}
else
top--;
}
void display()
{
int i;
for(i=0;i<=top;i++)
printf("%c",stk[i]);
}
void inputdisplay()
{
int i;
i=ip;
while(input[i]!='\0')
printf("%c",input[i++]);
}
void main()
{
int flag=0;
clrscr();
printf("\nEnter the input to be parsed with the delimiter $");
scanf("%s",input);
push('$');
push('E');
printf("STACK INPUTDATA");
while(1)
{
printf("\n");
display();
printf(" ------------------------------------- ");
inputdisplay();
if(stk[top]=='$'&&input[ip]=='$')
{
flag=1;
printf("\nSuccessful parser");
break;
} 5
Compiler Design Lab Manual MRITS
else if(stk[top]==input[ip])
{
ip++;
pop();
}
else if(stk[top]=='E')
{
if(input[ip]=='i'|| '(')
{
pop();
push('A');
push('T');
}
else
break;
}
else if(stk[top]=='A')
{
if(input[ip]=='+')
{
pop();
push('A');
push('T');
push('+');
}
else if(input[ip]==')'||'$')
{
pop();
}
else
break;
}
else if(stk[top]=='T')
{
if(input[ip]=='i'||'(')
{
pop();
push('B');
push('F');
}
else
break;
}
else if(stk[top]=='B')
{
if(input[ip]=='*')
{
pop();
push('B');
push('F');
push('*');
}
else if(input[ip]=='+'||')'||'$')
6
Compiler Design Lab Manual MRITS
{
pop();
}
else
break;
}
else if(stk[top]=='F')
{
if(input[ip]=='i')
{
pop();
push('i');
}
else if(input[ip]=='(')
{
pop();
push(')');
push('E');
push('(');
}
else
break;
}
else
break;
}
if(flag==0)
printf("\nIt is an invalid input");
getch();
}
INPUT: i+i$
OUTPUT:
-----------------------
Stack Input
-----------------------
$E i+i$
$AT i+i$
$ABF i+i$
$ABi i+i$
$AB +i$
$A +i$
$AT+ +i$
$AT i$
$ABF i$
$ABi i$
$A $
$ $
SUCCESSFULLY PARSED
Viva Questions:
1. What is top-down parsing?
2. What are the disadvantages of brute force method?
3. What is context free grammar? 7
Compiler Design Lab Manual MRITS
8
Compiler Design Lab Manual MRITS
EXPERIMENT 3
3.Problem Statement: Write a C program to generate three address code.
#include<stdio.h>
#include<string.h>
void pm();
void plus();
void div();
int i,ch,j,l,addr=100;
char ex[10], exp[10] ,exp1[10],exp2[10],id1[5],op[5],id2[5];
void main()
{
clrscr();
while(1)
{
printf("\n1.assignment\n2.arithmetic\n3.relational\n4.Exit\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the expression with assignment operator:");
scanf("%s",exp);
l=strlen(exp);
exp2[0]='\0';
i=0;
while(exp[i]!='=')
{
i++;
}
strncat(exp2,exp,i);
strrev(exp);
exp1[0]='\0';
strncat(exp1,exp,l-(i+1));
strrev(exp1);
printf("Three address code:\ntemp=%s\n%s=temp\n",exp1,exp2);
break;
case 2:
printf("\nEnter the expression with arithmetic operator:");
scanf("%s",ex);
strcpy(exp,ex);
l=strlen(exp);
exp1[0]='\0';
for(i=0;i<l;i++)
{
if(exp[i]=='+'||exp[i]=='-')
{
9
Compiler Design Lab Manual MRITS
if(exp[i+2]=='/'||exp[i+2]=='*')
{
pm();
break;
}
else
{
plus();
break;
}
}
else if(exp[i]=='/'||exp[i]=='*')
{
div();
break;
}
}
break;
case 3:
printf("Enter the expression with relational operator");
scanf("%s%s%s",&id1,&op,&id2);
if(((strcmp(op,"<")==0)||(strcmp(op,">")==0)||(strcmp(op,"<=")==0)||
(strcmp(op,">=")==0)||(strcmp(op,"==")==0)||(strcmp(op,"!=")==0))==0)
printf("Expression is error");
else
{
printf("\n%d\tif %s%s%s goto %d",addr,id1,op,id2,addr+3);
addr++;
printf("\n%d\t T:=0",addr);
addr++;
printf("\n%d\t goto %d",addr,addr+2);
addr++;
printf("\n%d\t T:=1",addr);
}
break;
case 4:
exit(0);
}
}
}
void pm()
{
strrev(exp);
j=l-i-1;
strncat(exp1,exp,j);
strrev(exp1);
printf("Three address code:\ntemp=%s\ntemp1=%c%ctemp\n",exp1,exp[j+1],exp[j]);
}
void div()
{
strncat(exp1,exp,i+2);
10
Compiler Design Lab Manual MRITS
OUTPUT:
1. assignment
2. arithmetic
3. relational
4. Exit
Enter the choice:1
Enter the expression with assignment operator:
a=b
Three address code:
temp=b
a=temp
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a+b-c
Three address code:
temp=a+b
temp1=temp-c
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a-b/c
Three address code:
temp=b/c
temp1=a-temp
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a*b-c
Three address code:
temp=a*b
temp1=temp-c
11
Compiler Design Lab Manual MRITS
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:2
Enter the expression with arithmetic operator:
a/b*c
Three address code:
temp=a/b
temp1=temp*c
temp=a/b
temp1=temp*c
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:3
Enter the expression with relational operator
a<=b
Three address code:
100 if a<=b goto 103
101 T:=0
102 goto 104
103 T:=1
1.assignment
2.arithmetic
3.relational
4.Exit
Enter the choice:4
Viva Questions:
12
Compiler Design Lab Manual MRITS
EXPERIMENT 4
4. Problem Statement: Design a SLR(1) Bottom Up Parser
ALGORITHM:
Step1: Start
Step2: Initially the parser has s0 on the stack where s0 is the initial state and w$ is in buffer
Step3: Set ip point to the first symbol of w$
Step4: repeat forever, begin
Step5: Let S be the state on top of the stack and a symbol pointed to by ip
Step6: if action [S, a] =shift S then begin
Push S1 on to the top of the stack
Advance ip to next input symbol
Step7:else if action [S, a], reduce AB then begin Pop 2* |B| symbols of the stack
Let S1 be the state now on the top of the stack
Step8: Output the production AB
End
Step9: else if action [S, a]=accepted, then return else error()
End
Step10: Stop
#include<stdio.h>
#include<stdlib.h>
void push(char);
void pop();
char input[10];
char stk[30];
void error();
int top=-1;
int ip=0;
void push(char c)
{
if(top>=30)
{
printf("stack overflow");
}
else
{
stk[++top]=c;
}
}
void pop()
{
if(top==-1)
{
printf("stack underflow");
}
else
13
Compiler Design Lab Manual MRITS
top--;
}
void display()
{
int i;
for(i=0;i<=top;i++)
printf("%c",stk[i]);
}
void inputdisplay()
{
int i;
i=ip;
while(input[i]!='\0')
printf("%c",input[i++]);
}
void main()
{
int flag=0;
clrscr();
printf("\nEnter the input to be parsed with the delimiter $");
scanf("%s",input);
push('0');
printf("STACK INPUT");
while(1)
{
printf("\n");
display();
printf(" --------------------------- ");
inputdisplay();
switch(stk[top])
{
case '0':
if(input[ip]=='i')
{
ip++;
push('i');
push('5');
}
else if(input[ip]=='(')
{
ip++;
push('(');
push('4');
}
else
error();
break;
case '1':
if(input[ip]=='+')
{
ip++;
push('+');
14
Compiler Design Lab Manual MRITS
push('6');
}
else if(input[ip]=='$')
{
printf("successful parsed");
exit(1);
}
else
error();
break;
case '2':
if(input[ip]=='*')
{
ip++;
push('*');
push('7');
}
else if(input[ip]=='+'||')'||'$')
{
pop();
pop();
push('E');
if(stk[top-1]=='0')
push('1');
else if(stk[top-1]=='4')
push('8');
else
error();
}
else
error();
break;
case '3':
if(input[ip]=='+'||'*'||')'||'$')
{
pop();
pop();
push('T');
if(stk[top-1]=='0')
push('2');
else if(stk[top-1]=='4')
push('2');
else if(stk[top-1]=='6')
push('9');
else
error();
}
else
error();
break;
case '4':
if(input[ip]=='i')
15
Compiler Design Lab Manual MRITS
{
ip++;
push('i');
push('5');
}
else if(input[ip]=='(')
{
ip++;
push('(');
push('4');
}
else
error();
break;
case '5':
if(input[ip]=='+'||'*'||')'||'$')
{
pop();
pop();
push('F');
if(stk[top-1]=='0')
push('3');
else if(stk[top-1]=='4')
push('3');
else if(stk[top-1]=='6')
push('3');
else if(stk[top-1]=='7')
push('@');
else
error();
}
else
error();
break;
case '6':
if(input[ip]=='i')
{
ip++;
push('i');
push('5');
}
else if(input[ip]=='(')
{
ip++;
push('(');
push('4');
}
else
error();
break;
case '7':
if(input[ip]=='i')
16
Compiler Design Lab Manual MRITS
{
ip++;
push('i');
push('5');
}
else if(input[ip]=='(')
{
ip++;
push('(');
push('4');
}
else
error();
break;
case '8':
if(input[ip]=='+')
{
ip++;
push('+');
push('6');
}
else if(input[ip]==')')
{
ip++;
push(')');
push('#');
}
else
error();
break;
case '9':
if(input[ip]=='*')
{
ip++;
push('*');
push('7');
}
else if(input[ip]=='+'||')'||'$')
{
pop();
pop();
pop();
pop();
pop();
pop();
push('E');
if(stk[top-1]=='0')
push('1');
else if(stk[top-1]=='4')
push('8');
else
error();
17
Compiler Design Lab Manual MRITS
}
else
error();
break;
case '@':
if(input[ip]=='+'||'*'||')'||'$')
{
pop();
pop();
pop();
pop();
pop();
pop();
push('T');
if(stk[top-1]=='0')
push('2');
else if(stk[top-1]=='4')
push('2');
else if(stk[top-1]=='6')
push('9');
else
error();
}
else
error();
break;
case '#':
if(input[ip]=='+'||'*'||')'||'$')
{
pop();
pop();
pop();
pop();
pop();
pop();
push('F');
if(stk[top-1]=='0')
push('3');
else if(stk[top-1]=='4')
push('3');
else if(stk[top-1]=='6')
push('3');
else if(stk[top-1]=='7')
push('@');
else
error();
}
else
error();
break;
default:
error();
18
Compiler Design Lab Manual MRITS
break;
}
}
}
void error()
{
printf("invalid input");
exit(1);
}
INPUT: i*i+i$
OUTPUT:
----------------------------
STACK INPUT
----------------------------
0 i*i+i$
0i5 *i+i$
0F3 *i+i$
0T2 *i+i$
0T2*7 i+i$
0T2*7i5 +i$
0T2*7F@ +i$
0T2 +i$
0E1 +i$
0E1+6 i$
0E1+6i5 $
0E1+6F3 $
0E1+6T $
0E1 $
SUCCESSFULLY PARSED
Viva Questions:
19
Compiler Design Lab Manual MRITS
EXPERIMENT 5
5. Problem Statement: Design a LALR Bottom Up Parser for the given grammar.
Aim:To Design and implement an LALR bottom up Parser for checking the
syntax of the Statements in the given language.
20
Compiler Design Lab Manual MRITS
if t2>5 then
print(t2);
else
{
int t3;
t3=99
;
t2=25
;
print(-11+t2*t3); /* this is not a comment on two lines */
}
endif
}
}
void main()
{
int flag=0;
printf("\nEnter the input to be parsed with the delimiter $");
scanf("%s",input);
push('0');
printf("stack contents input data");
while(1)
{
printf("\n");
display();
printf("---------------------------------------------------");
inputdisplay();
switch(stk[top])
{
case '0':
if (input[ip]=='c')
{
ip++;
push('c');
push('@');
}
else if(input[ip]=='d')
{
ip++;
push('d');
push('#');
}
else
{
printf("Could not be PARSED");
error();
}
break;
case '1':
if(input[ip]=='$')
{
printf("SUCCESSFULLY PARSED");
exit(1);
}
else
{
printf("Could not be PARSED");
error();
}
break;
case '2':
if (input[ip]=='c')
{
ip++;
push('c');
push('@');
22
Compiler Design Lab Manual MRITS
}
else if(input[ip]=='d')
{
ip++;
push('d');
push('#');
}
else
{
printf("Could not be PARSED");
error();
}
break;
case '@':
if (input[ip]=='c')
{
ip++;
push('c');
push('@');
}
else if(input[ip]=='d')
{
ip++;
push('d');
push('#');
}
else
{
printf("Could not be PARSED");
error();
}
break;
case'#':
if(input[ip]=='c'||'d'||'$')
{
pop();
pop();
push('C');
if(stk[top-1]=='0')
push('2');
else if(stk[top-1]=='2')
push('5');
else if(stk[top-1]=='@')
push('&');
else
{
printf("Could not be PARSED");
error();
}
}
else
{
printf("Could not be PARSED");
23
Compiler Design Lab Manual MRITS
error();
}
break;
case'5':
if(input[ip]=='$')
{
pop();
pop();
pop();
pop();
push('S');
if(stk[top-1]=='0')
push('1');
else
{
printf("Could not be PARSED");
error();
}
}
else
{
printf("Could not be PARSED");
error();
}
break;
case'&':
if(input[ip]=='c'||'d'||'$')
{
pop();
pop();
pop();
pop();
push('C');
if(stk[top-1]=='0')
push('2');
else if(stk[top-1]=='2')
push('5');
else if(stk[top-1]=='@')
push('&');
else
{
printf("Could not be PARSED");
error();
}
}
else
{
printf("Could not be PARSED");
error();
}
break;
24
Compiler Design Lab Manual MRITS
}
}
}
OUTPUT:
Viva Questions:
25
Compiler Design Lab Manual MRITS
Additional Programs
ALGORITHM/ PROCEDURE:
Step1: Start
Step2: declare w[10] as char and Z asan array
Step3: enter the string with $ at theend
Step4: if(A(w[z])thenincrementzandcheckfor(B(w[z]))and
ifsatisfiesincrementzandcheckfor
‘d’ifdispresentthenincrementandcheckfor(D(w[z]))
Step5: if step 4 is satisfied then the string isaccepted else string isnot
Step 6: For the productionScAdwrite the function S()
Step 7: For the production Aab/a write the function A()
Step 8: ifsteps7,8,9aresatisfiedaccordinglystringisaccepted else string is
notaccepted
Step 9: Stop
SOURCECODE:
#include<stdio.h>
char input[15];
int ip=0;
int S();
int A();
int S()
{
if(input[ip]=='c')
{
ip++;
if(A())
{
if(input[ip]=='d')
{
ip++;
return 1;
}
}
}
return 0;
}
int A()
{
26
Compiler Design Lab Manual MRITS
int isave=ip;
if(input[ip]=='a')
{
ip++;
if(input[ip]=='b')
{
ip++;
return 1;
}
}
ip=isave;
if(input[ip]=='a')
{
ip++;
return 1;
}
else
return 0;
}
int main()
{
int flag;
printf("Enter the input of the grammar S->cAd, A->ab/a ending with $");
scanf("%s",input);
flag=S();
if(flag==1 && input[ip]=='$')
{
printf("valid input");
}
else
printf("invalid input");
return 0;
}
INPUT:
Enter the input of the grammar S->cAd, A->ab/a ending with $
cad$
OUTPUT:
valid input
27
Compiler Design Lab Manual MRITS
The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters
and produce as output a sequence of tokens. Up on receiving a “get next token” command from the parser,
the lexical analyzer reads input characters until it can identify the next token. Token is a group of
characters having a collective meaning. Tokens are constants, identifier, keywords, operators, special
symbols.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
int i,j; clrscr();
char keywords[10][10]={"BEGIN","IF","THEN","ELSE","END"};
charoperators[10][10]={"<","<=",">","=","!="};
char string[10];
int ch,l,found=0;
printf("/n enter any data of your choice");
scanf("%s",string);
l=strlen(string);
for(i=0;i<l;i++)
string[i]=toupper(string[i]);
for(i=0;i<5;i++)
{
if(strcmp(&keywords[i][0],string)==0)
{
found=1;
ch=1;
break;
}
28
Compiler Design Lab Manual MRITS
}
for(i=0;i<5;i++)
{
if(strcmp(&operators[i][0],string)==0)
{
found=1;
ch=2;
break;
}
}
switch(ch)
{
case 1: if(found==1)
printf("\n you have entered keyword"); break;
case 2: if(found==1)
printf("\n you have entered relational operator"); break;
default:printf("\n you have entered default choice");
}
getch();
}
Input:
enter any data of your choice else
Output:
VIVA QUESTIONS
29
Compiler Design Lab Manual MRITS
%{
#include<stdio.h>
#undef yywrap
#define yywrap() 1
%}
vowels[aeiouAEIOU]
letter[a-zA-Z]
%%
%%
int main()
yylex();
INPUT 1:
OUTPUT:
a is a vowel!
INPUT 2:
OUTPUT:
zis a consonant!
30
Compiler Design Lab Manual MRITS
%{
#include<stdio.h>
%}
assg =
aop[+-/*%]
rop(>|<|">="|"<="|"=="|"!=")
bop[&^~|"|"]
lop(!|&&|"||")
shift ("<<"|">>")
inc("++")
dec("--")
other[a-zA-Z0-9_@#$.?;]
%%
%%
31
Compiler Design Lab Manual MRITS
int main()
yylex();
}
INPUT 1:
==
OUTPUT:
= = is a relational operator
INPUT 2:
OUTPUT:
32