CD Lab Manual
CD Lab Manual
Prerequisites: -Nil-
Course Objectives:
1. To understand the various phases in the design of a compiler.
2. To understand the design of top-down and bottom-up parsers.
3. To understand syntax directed translation schemes.
4. To introduce lex and yacc tools.
List of Programs:
1. Write a LEX Program to scan reserved word & Identifiers of C Language. The lexical analyzer
should ignore redundant spaces, tabs and newlines. It should also ignore comments.
2. Write a C program to recognize strings under 'a*', 'a*b+', 'abb'
3. Write a C program to test whether a given identifier is valid or not
4. Write a Program to implement Recursive Descent Parser for language.
5. Write a Program to Design a Predictive Parser for the Language Accepted by the given
Grammar.
6. Write a Program for Implementation of Shift Reduce parsing
7. Write a program to calculate first function for the given grammar.
8. Write a program to implement Simple LR Parser for the given language
9. Write a program to Implement SLR(1) Parsing algorithm for the given language
10. Write a program to Design LALR bottom-up parser for the given language
11. Write a program to Design CLR bottom-up parser for the given language
12. Write a C program to generate three address codes.
REFERENCE BOOKS
1. Dick Grune, Henry E. Bal, Cariel T. H. Jacobs,” Modern Compiler Design”, Wiley
dreamtech.
2. Cooper & Linda,” Engineering a Compiler”, Elsevier.
3. Louden,” Compiler Construction”, Thomson.
Course Outcomes:
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO2
COs Programme Outcomes(POs) PSOs
CO1 3 2 2 2
CO2 3 2 2 2
CO3 2 2 2 2
1. Write a LEX Program to scan reserved word & Identifiers of C Language. The
lexical analyzer should ignore redundant spaces, tabs and newlines. It should also
ignore comments.
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
FILE *input, *output;
int l=1;
int t=0;
int j=0;
int i,flag;
char ch,str[20];
input = fopen("input.txt","r");
output = fopen("output.txt","w");
char keyword[30][30] = {"int","main","if","else","do","while"};
fprintf(output,"Line no. \t Token no. \t Token \t Lexeme\n\n");
while(!feof(input))
{
i=0;
flag=0;
ch=fgetc(input);
if( ch=='+' || ch== '-' || ch=='*' || ch=='/' )
{
fprintf(output,"%7d\t\t %7d\t\t Operator\t %7c\n",l,t,ch);
t++;
}
else if( ch==';' || ch=='{' || ch=='}' || ch=='(' || ch==')' || ch=='?' || ch=='@' || ch=='!' || ch=='%')
{
fprintf(output,"%7d\t\t %7d\t\t Special symbol\t %7c\n",l,t,ch);
t++;
}
else if(isdigit(ch))
{
fprintf(output,"%7d\t\t %7d\t\t Digit\t\t %7c\n",l,t,ch);
t++;
}
else if(isalpha(ch))
{
str[i]=ch;
i++;
ch=fgetc(input);
while(isalnum(ch) && ch!=' ')
{
str[i]=ch;
i++;
ch=fgetc(input);
}
str[i]='\0';
for(j=0;j<=30;j++)
{
if(strcmp(str,keyword[j])==0)
{
flag=1;
break;
}
}
if(flag==1)
{
fprintf(output,"%7d\t\t %7d\t\t Keyword\t %7s\n",l,t,str);
t++;
}
else
{
fprintf(output,"%7d\t\t %7d\t\t Identifier\t %7s\n",l,t,str);
t++;
}
}
else if(ch=='\n')
{
l++;
}
}
fclose(input);
fclose(output);
return 0;
}
Output:
//output.txt
Line no. Token no. Token Lexeme
1 0 Identifier include
1 1 Identifier stdio
1 2 Identifier h
2 3 Identifier void
2 4 Keyword main
2 5 Special symbol )
3 6 Special symbol {
4 7 Identifier printf
4 8 Identifier Hello
4 9 Identifier World
4 10 Special symbol )
4 11 Special symbol ;
5 12 Special symbol }
2. Write a C program to recognize strings under 'a*', 'a*b+', 'abb'
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char s[20],c;
int state=0,i=0;
printf("\n Enter a string:");
gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else if(c=='b')
state=2;
else
state=6;
break;
case 1: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;
case 2: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=5;
else
state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not recognised.",s);
exit(0);
}
}
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
}
Output:
if(flag == notfound)
keyword",str);getch();
4.Write a Program to implement Recursive Descent Parser for language given below
E -> E+T | T
T -> T*F | F
F -> (E) | id
RD parser will verify whether the syntax of the input stream is correct by checking each
character from left to right. A basic operation necessary is reading characters from the input
stream and matching then with terminals from the grammar that describes the syntax of the
input.
The given grammar can accept all arithmetic equations involving +, * and ().
eg:
a+(a*a) a+a*a , (a), a , a+a+a*a+a.... etc are accepted
a++a, a***a, +a, a*, ((a . . . etc are rejected.
First we have to avoid left recursion
E -> TE'
E' -> +TE' | ε
T -> FT'
T' -> *FT' | ε
F -> (E) | id
After eliminating Left recursion, we have to simply move from one character to next by
checking whether it follow the grammar. In this program, ε is indicated as $.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char input[10];
int i,error;
void E();
void T();
void Eprime();
void Tprime();
void F();
main()
{
i=0;
error=0;
printf("Enter an arithmetic expression : "); // Eg: a+a*a
gets(input);
E();
if(strlen(input)==i&&error==0)
printf("\nAccepted..!!!\n");
else printf("\nRejected..!!!\n");
}
void E()
{
T();
Eprime();
}
void Eprime()
{
if(input[i]=='+')
{
i++;
T();
Eprime();
}
}
void T()
{
F();
Tprime();
}
void Tprime()
{
if(input[i]=='*')
{
i++;
F();
Tprime();
}
}
void F()
{
if(isalnum(input[i]))i++;
else if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
i++;
else error=1;
}
else error=1;
}
Output:
OUTPUT:
input is parsed
Exp-6: write a program to implement shift reduce parser
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int z = 0, i = 0, j = 0, c = 0;
char a[16], ac[20], stk[15], act[10];
void check()
{
strcpy(ac,"REDUCE TO E -> ");
for(z = 0; z < c; z++)
{
if(stk[z] == '4')
{
printf("%s4", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
printf("\n$%s\t%s$\t", stk, a);
}
}
for(z = 0; z < c - 2; z++)
{
if(stk[z] == '2' && stk[z + 1] == 'E' && stk[z + 2] == '2')
{
printf("%s2E2", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}
}
for(z=0; z<c-2; z++)
{
if(stk[z] == '3' && stk[z + 1] == 'E' && stk[z + 2] == '3')
{
printf("%s3E3", ac);
stk[z]='E';
stk[z + 1]='\0';
stk[z + 1]='\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}
}
return ;
}
int main()
{
printf("GRAMMAR is -\nE->2E2 \nE->3E3 \nE->4\n");
strcpy(a,"32423");
c=strlen(a);
strcpy(act,"SHIFT");
printf("\nstack \t input \t action");
printf("\n$\t%s$\t", a);
Output:
GRAMMAR is -
E->2E2
E->3E3
E->4
$ 32423$ SHIFT
$3 2423$ SHIFT
$32 423$ SHIFT
$324 23$ REDUCE TO E -> 4
$32E 23$ SHIFT
$32E2 3$ REDUCE TO E -> 2E2
$3E 3$ SHIFT
$3E3 $ REDUCE TO E -> 3E3
$E $ Accept
Exp-7: write a program to calculate first function for given grammar
#include<stdio.h>
#include<ctype.h>
void FIRST(char );
int count,n=0;
char prodn[10][10], first[10];
main()
{
int i,choice;
char c,ch;
printf("How many productions ? :");
scanf("%d",&count);
printf("Enter %d productions epsilon= $ :\n\n",count);
for(i=0;i<count;i++)
scanf("%s%c",prodn[i],&ch);
do
{
n=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\n FIRST(%c)= { ",c);
for(i=0;i<n;i++)
printf("%c ",first[i]);
printf("}\n");
printf("press 1 to continue : ");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
}
void FIRST(char c)
{
int j;
if(!(isupper(c)))first[n++]=c;
for(j=0;j<count;j++)
{
if(prodn[j][0]==c)
{
if(prodn[j][2]=='$') first[n++]='$';
else if(islower(prodn[j][2]))first[n++]=prodn[j][2];
else FIRST(prodn[j][2]);
}
}
}
Output:
How many productions ? :8
Enter 8 productions epsilon= $ :
E=TD
D=+TD
D=$
T=FS
S=*FS
S=$
F=(E)
F=a ………..> Enter
D=+TD
D=$
T=FS
S=*FS
S=$
F=(E)
F=a……………enter element
Element :E
FIRST(E)= { ( a }
press 1 to continue : 1
Element :F
FIRST(F)= { ( a }
press 1 to continue : 1
Element :S
FIRST(S)= { * $ }
press 1 to continue : 1
Element :D
FIRST(D)= { + $ }
press 1 to continue : 1
Element :T
FIRST(T)= { ( a }
press 1 to continue :
Exp-8. Write a program to implement Simple LR Parser for the given
language
ALGORITHM:
1. Get the input expression and store it in the input buffer.
2. Read the data from the input buffer one at the time and convert in to corresponding Non
Terminal using production rules available.
3. Perform push & pop operation for LR parsing table construction.
4. Display the result with conversion of corresponding input symbols to production and
production reduction to start symbol. No operation performed on the operator.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int axn[][6][2]={
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{100,6},{-1,-1},{-1,-1},{-1,-1},{102,102}},
{{-1,-1},{101,2},{100,7},{-1,-1},{101,2},{101,2}},
{{-1,-1},{101,4},{101,4},{-1,-1},{101,4},{101,4}},
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{101,6},{101,6},{-1,-1},{101,6},{101,6}},
{{100,5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{100,5},{-1 -1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{100,6},{-1,-1},{-1,-1},{100,11},{-1,-1}},
{{-1,-1},{101,1},{100,7},{-1,-1},{101,1},{101,1}},
{{-1,-1},{101,3},{101,3},{-1,-1},{101,3},{101,3}},
{{-1,-1},{101,5},{101,5},{-1,-1},{101,5},{101,5}},
};
int gotot[12][3]={1,2,3,-1,-1,-1-1,-1,-1,-1,-1,-1,8,2,3,-1,-1,-1,-1,9,3,-1,-1,10,-1,-1,-1,-1,-1,-1,-1,-
1,-1,-1,-1,-1};
int a[10];
char b[10];
int top=-1,btop=-1,i;
void push(int k)
{
if(top<9)
a[++top]=k;
}
void pushb(char k)
{
if(btop<9)
b[++btop]=k;
}
char TOS()
{
return a[top];
}
void pop() {
if(top>=0)
top--;
}
void popb() {
if(btop>=10)
b[btop--]='\0';
}
void display() {
for(i=0;i<top;i++)
printf("%d%c",a[i],b[i]); }
void display1(char p[],int m) {
int l;
printf("\t\t");
for(l=m;p[l]!='\0';l++)
printf("%c",p[l]);
printf("\n");
}
void error()
{
printf("syntax error"); }
void reduce(int p)
{
int k,ad;
char src,*dest;
switch(p)
{
case 1:dest="E+T";
src='E';
break;
case 2:dest="T";
src='E';
break;
case 3:dest="T*F";
src='T';
break;
case 4:dest="F";
src='T';
break;
case 5:dest="(E)";
src='F';
break;
case 6:dest="i";
src='F';
break;
default :dest="\0";
src='\0';
break;
}
for(k=0;k<strlen(dest);k++)
{
pop();
popb();
}
pushb(src);
switch(src)
{
case 'E':ad=0;
break;
case 'T':ad=1;
break;
case 'F':ad=2;
break;
default:ad=-1;
break;
}
push(gotot[TOS()][ad]);
}
int main()
{
int j,st,ic;
char ip[20]="\0",an;
printf("enter any string");
scanf("%s",ip);
push(0);
display();
printf("\t%s\n",ip);
for(j=0;ip[j]!='\0';)
{
st=TOS();
an=ip[j];
if(an>='a'&&an<='z') ic=0;
else if(an=='+') ic=1;
else if(an=='*') ic=2;
else if(an=='(') ic=3;
else if(an==')') ic=4;
else if(an=='$') ic=5;
else
{
error();
break;
}
if(axn[st][ic][0]==100)
{
pushb(an);
push(axn[st][ic][1]);
display();
j++;
display1(ip,j);
}
if(axn[st][ic][0]==101)
{
reduce(axn[st][ic][1]);
display();
display1(ip,j);
}
if(axn[st][ic][1]==102)
{
printf("given string is accepted");
break;
}
}
return(0);
}
Output:
#include<stdio.h>
#include<string.h>
int axn[][6][2]={
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{100,6},{-1,-1},{-1,-1},{-1,-1},{102,102}},
{{-1,-1},{101,2},{100,7},{-1,-1},{101,2},{101,2}},
{{-1,-1},{101,4},{101,4},{-1,-1},{101,4},{101,4}},
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{101,6},{101,6},{-1,-1},{101,6},{101,6}},
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{100,5},{-1,-1},{-1,-1},{100,4},{-1,-1},{-1,-1}},
{{-1,-1},{100,6},{-1,-1},{-1,-1},{100,1},{-1,-1}},
{{-1,-1},{101,1},{100,7},{-1,-1},{101,1},{101,1}},
{{-1,-1},{101,3},{101,3},{-1,-1},{101,3},{101,3}},
{{-1,-1},{101,5},{101,5},{-1,-1},{101,5},{101,5}}
};//Axn Table
b[btop--]='\0';
}
void display()
{
for(i=0;i<=top;i++)
printf("%d%c",a[i],b[i]);
}
void display1(char p[],int m) //Displays The Present Input String
{
int l;
printf("\t\t");
for(l=m;p[l]!='\0';l++)
printf("%c",p[l]);
printf("\n");
}
void error()
{
printf("Syntax Error");
}
void reduce(int p)
{
int len,k,ad;
char src,*dest;
switch(p)
{
case 1:dest="E+T";
src='E';
break;
case 2:dest="T";
src='E';
break;
case 3:dest="T*F";
src='T';
break;
case 4:dest="F";
src='T';
break;
case 5:dest="(E)";
src='F';
break;
case 6:dest="i";
src='F';
break;
default:dest="\0";
src='\0';
break;
}
for(k=0;k<strlen(dest);k++)
{
pop();
popb();
}
pushb(src);
switch(src)
{
case 'E':ad=0;
break;
case 'T':ad=1;
break;
case 'F':ad=2;
break;
default: ad=-1;
break;
}
push(gotot[TOS()][ad]);
}
int main()
{
int j,st,ic;
char ip[20]="\0",an;
// clrscr();
printf("Enter any String\n");
scanf("%s",ip);
push(0);
display();
printf("\t%s\n",ip);
for(j=0;ip[j]!='\0';)
{
st=TOS();
an=ip[j];
if(an>='a'&&an<='z') ic=0;
else if(an=='+') ic=1;
else if(an=='*') ic=2;
else if(an=='(') ic=3;
else if(an==')') ic=4;
else if(an=='$') ic=5;
else {
error();
break;
}
if(axn[st][ic][0]==100)
{
pushb(an);
push(axn[st][ic][1]);
display();
j++;
display1(ip,j);
}
if(axn[st][ic][0]==101)
{
reduce(axn[st][ic][1]);
display();
display1(ip,j);
}
if(axn[st][ic][1]==102)
{
printf("Given String is accepted \n");
// getch();
break;
}
/* else
{
printf("Given String is rejected \n");
break;
}*/
return 0;
}
Output:
a+a*a$
0 a+a*a$
0a5 +a*a$
0F3 +a*a$
0T2 +a*a$
0E1 +a*a$
0E1+6 a*a$
0E1+6a5 *a$
0E1+6F3 *a$
0E1+6T9 *a$
0E1+6T9*7 a$
0E1+6T9*7a5 $
0E1+6T9*7F10 $
0E1+6T9 $
0E1 $
Exp-10. Write a program to Design LALR bottom-up parser for the given language
PROGRAM LOGIC:
Read the input string.
Push the input symbol with its state symbols in to the stack by referring lookaheads
We perform shift and reduce actions to parse the grammar.
Parsing is completed when we reach $ symbol.
PROCEDURE:
Go to debug -> run or press CTRL + F9 to run the program.
PROGRAM:
/*LALR PARSER
E->E+T
E->T
T->T*F
T->F
F->(E)
F->i
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void push(char *,int *,char);
char stacktop(char *);
void isproduct(char,char);
int ister(char);
int isnter(char);
int isstate(char);
void error();
void isreduce(char,char);
char pop(char *,int *);
void printt(char *,int *,char [],int);
void rep(char [],int);
struct action
{
char row[6][5];
};
const struct action A[12]={
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","emp","acc"},
{"emp","rc","sh","emp","rc","rc"},
{"emp","re","re","emp","re","re"},
{"sf","emp","emp","se","emp","emp"},
{"emp","rg","rg","emp","rg","rg"},
{"sf","emp","emp","se","emp","emp"},
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","sl","emp"},
{"emp","rb","sh","emp","rb","rb"},
{"emp","rb","rd","emp","rd","rd"},
{"emp","rf","rf","emp","rf","rf"}
};
struct gotol
{
char r[3][4];
};
const struct gotol G[12]={
{"b","c","d"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"i","c","d"},
{"emp","emp","emp"},
{"emp","j","d"},
{"emp","emp","k"},
{"emp","emp","emp"},
{"emp","emp","emp"},
};
char ter[6]={'i','+','*',')','(','$'};
char nter[3]={'E','T','F'};
char states[12]={'a','b','c','d','e','f','g','h','m','j','k','l'};
char stack[100];
int top=-1;
char temp[10];
struct grammar
{
char left;
char right[5];
};
const struct grammar rl[6]={{'E',"e+T"},{'E',"T"},{'T',"T*F"},{'T',"F"},{'F',"(E)"},{'F',"i"}, };
void main()
{
char inp[80],x,p,dl[80],y,bl='a';
int i=0,j,k,l,n,m,c,len;
printf(" Enter the input :");
scanf("%s",inp);
len=strlen(inp);
inp[len]='$';
inp[len+1]='\0';
push(stack,&top,bl);
printf("\n stack \t\t\t input");
printt(stack,&top,inp,i);
do
{
x=inp[i];
p=stacktop(stack);
isproduct(x,p);
if(strcmp(temp,"emp")==0)
error();
if(strcmp(temp,"acc")==0)
break;
else
{
if(temp[0]=='s')
{
push(stack,&top,inp[i]);
push(stack,&top,temp[1]);
i++;
}
else
{
if(temp[0]=='r')
{
j=isstate(temp[1]);
strcpy(temp,rl[j-2].right);
dl[0]=rl[j-2].left;
dl[1]='\0';
n=strlen(temp);
for(k=0;k<2*n;k++)
pop(stack,&top);
for(m=0;dl[m]!='\0';m++)
push(stack,&top,dl[m]);
l=top;
y=stack[l-1];
isreduce(y,dl[0]);
for(m=0;temp[m]!='\0';m++)
push(stack,&top,temp[m]);
}
}
}
printt(stack,&top,inp,i);
}while(inp[i]!='\0');
if(strcmp(temp,"acc")==0)
printf(" \n accept the input ");
else
printf(" \n do not accept the input ");
}
void push(char *s,int *sp,char item)
{
if(*sp==100)
printf(" stack is full ");
else
{
*sp=*sp+1;
s[*sp]=item;
}
}
Exp-11. Write a program to Design CLR bottom-up parser for the given language
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define S4 1
#define S5 2
#define S6 3
#define S7 4
#define R1 5
#define R2 6
#define R3 7
#define R4 8
#define R5 9
#define R6 10
#define S11 11
#define AC 11 /* ACCEPT */
#define ER -1 /* ERROR */
/* the parsing table */
int table[][9]= {
{S5,ER,ER,S4,ER,ER,1,2,3},
{ER,S6,ER,ER,ER,AC,ER,ER,ER},
{ER,R2,S7,ER,R2,R2,ER,ER,ER},
{ER,R4,R4,ER,R4,R4,ER,ER,ER},
{S5,ER,ER,S4,ER,ER,8,2,3},
{ER,R6,R6,ER,R6,R6,ER,ER,ER},
{S5,ER,ER,S4,ER,ER,ER,9,3},
{S5,ER,ER,S4,ER,ER,ER,ER,10},
{ER,S6,ER,ER,S11,ER,ER,ER,ER},
{ER,R1,S7,ER,R1,R1,ER,ER,ER},
{ER,R3,R3,ER,R3,R3,ER,ER,ER},
{ER,R5,R5,ER,R5,R5,ER,ER,ER}
};
#define STRING_SIZE 20
char string[STRING_SIZE];
int i=0;
int save;
#define STACK_SIZE 40
typedef struct {
int list[STACK_SIZE];
int top;
}Stack;
void initialize(Stack *s) {
s->top=-1;
}
void push(int value,Stack *s) {
s->list[++(s->top)]=value;
}
int pop(Stack *s) {
return(s->list[(s->top)--]);
}
int isempty(Stack *s) {
return(s->top==-1);
}
int peek(Stack *s) {
return(s->list[s->top]);
}
int stacksize(Stack *s) {
return((s->top)+1);
}
#define ID 0
#define ADD 1
#define MULT 2
#define OPBR 3
#define CLBR 4
#define DOLLAR 5
#define E 6
#define T 7
#define F 8
short int gettoken() {
/* ignore blanks */
while(string[i]==' ')
i++;
/* definition for identifier */
if(isalpha(string[i])) {
save=i;
i++;
while(string[i]!=0 && string[i]!='*' && string[i]!='+' && string[i]!=')' && string[i]!='(') {
if(!isalnum(string[i++]))
error();
}
return ID;
}
else if(string[i]=='+') {
save=i;
i++;
return ADD;
}
else if(string[i]=='*') {
save=i;
i++;
return MULT;
}
else if(string[i]=='(') {
save=i;
i++;
return OPBR;
}
else if(string[i]==')') {
save=i;
i++;
return CLBR;
}
else if(string[i]==0) {
return DOLLAR;
}
}
error() {
printf("Bad Bad error.\n");
exit(0);
}
int parse()
{
int token;
Stack stack;
int state;
int j;
int action;
int previous;
initialize(&stack);
push(0,&stack);
token=0;
while(1) {
token=gettoken();
action=table[peek(&stack)][token];
switch(action) {
case S4:
push(token,&stack);
push(4,&stack);
break;
case S5:
push(token,&stack);
push(5,&stack);
break;
case S6:
push(token,&stack);
push(6,&stack);
break;
case S7:
push(token,&stack);
push(7,&stack);
break;
case AC:
return 1;
case ER:
error();
}
if(action>=5 && action <=10) {
while(action>=5 && action <=10) {
action=table[peek(&stack)][token];
switch(action) {
case R1:
for(j=0;j<6;j++)
pop(&stack);
state=table[peek(&stack)][E];
if(state!=ER) {
push(E,&stack);
push(state,&stack);
}
else
error();
break;
case R2:
pop(&stack);
pop(&stack);
state=table[peek(&stack)][E];
if(state!=ER) {
push(E,&stack);
push(state,&stack);
}
else
error();
break;
case R3:
for(j=0;j<6;j++)
pop(&stack);
state=table[peek(&stack)][T];
if(state!=ER) {
push(T,&stack);
push(state,&stack);
}
else
error();
break;
case R4:
pop(&stack);
pop(&stack);
state=table[peek(&stack)][T];
if(state!=ER) {
push(T,&stack);
push(state,&stack);
}
else
error();
break;
case R5:
for(j=0;j<6;j++)
pop(&stack);
state=table[peek(&stack)][F];
if(state!=ER) {
push(F,&stack);
push(state,&stack);
}
else
error();
break;
case R6:
pop(&stack);
pop(&stack);
state=table[peek(&stack)][F];
if(state!=ER) {
push(F,&stack);
push(state,&stack);
}
else
error();
break;
case AC:
return 1;
case ER:
error();
}
}
i=save;
}
}
return 0;
}
int main()
{
printf("Enter the string: ");
scanf("%s",string);
if(parse()) {
printf("Success in parsing.\n");
}
else
error();
}
Output:
Enter the string: i+i*i
Success in parsing.
12. Write a C program to generate three address codes.
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
void small();
void dove(int i);
int p[5]={0,1,2,3,4},c=1,i,k,l,m,pi;
char sw[5]={'=','-','+','/','*'},j[20],a[5],b[5],ch[2];
void main()
{
printf("Enter the expression:");
scanf("%s",j);
printf("\tThe Intermediate code is:\n");
small();
}
if(j[i]=='*')
printf("\tt%d=%s*%s\n",c,a,b);
if(j[i]=='/')
printf("\tt%d=%s/%s\n",c,a,b);
if(j[i]=='+')
printf("\tt%d=%s+%s\n",c,a,b);
if(j[i]=='-')
printf("\tt%d=%s-%s\n",c,a,b);
if(j[i]=='=')
printf("\t%c=t%d",j[i-1],--c);
sprintf(ch,"%d",c);
j[i]=ch[0];
c++; small();
}
void small()
{
pi=0;l=0;
for(i=0;i<strlen(j);i++)
{
for(m=0;m<5;m++)
if(j[i]==sw[m])
if(pi<=p[m])
{
pi=p[m];
l=1;
k=i;
}
}
if(l==1)
dove(k);
else
exit(0);
}
Output: