Compiler-Design Lab Assignment Solution
Compiler-Design Lab Assignment Solution
Compiler-Design Lab Assignment Solution
com
LIST OF EXPERIMENTS
LIST OF EXPERIMENTS
1
Write a program to check the identifier in the given source program
2
Write a program to convert an Expression form Infix to Postfix
3
Write a program to find the FIRST of the given grammar
4
Write a program FOLLOW of the given grammar
5
Write a program to implement the parser PREDICTIVE PARSER
10
Implement Strength Reduction using Induction Variable technique of code
optimization
www.rishabhdua.com
www.rishabhdua.com
Experiment - 1
Program Name : Write a program to check the identifier in the given source program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10];
int i,j,p,flag=0;
a[]={"if","else","main","void","char","float","goto","for","includ
e","int"};
clrscr();
gets(b);
for(i=0;i<10;i++)
{
p=strcmp(b,a[i]);
if(p==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("keyword matched");
}
else
{
printf("keyword not matched");
}
getch();}
www.rishabhdua.com
www.rishabhdua.com
Experiment - 2
#include<conio.h>
#include<stdio.h>
void main()
{
char pre[20];
char stack1[40];
char stack2[40];
int top1=-1,top2=-1,i;
clrscr();
printf("Enter the prefix expression :\n");
pre[0]='(';
for(i=1;;i++)
{
scanf("%c",&pre[i]);
if(pre[i]=='\r'||pre[i]=='\n')
{
pre[i]=')';
pre[++i]='\0';
break;
}
}
for(i=0;pre[i]!='\0';i++)
{ up:
if(pre[i]=='+'||pre[i]=='-
'||pre[i]=='*'||pre[i]=='/'||pre[i]=='('||pre[i]==')')
{
if(pre[i]=='(')
stack1[++top1]='(';
if(pre[i]=='+')
{
if(stack1[top1]!='+'&&stack1[top1]!='*'&&stack1[top1]!='/')
stack1[++top1]=pre[i];
else {stack2[++top2]=stack1[top1--];goto up;}
}
if(pre[i]=='-')
{
if(stack1[top1]!='+'&&stack1[top1]!='-
'&&stack1[top1]!='*'&&stack1[top1]!='/')
stack1[++top1]=pre[i];
else {stack2[++top2]=stack1[top1--];goto up;}
www.rishabhdua.com
www.rishabhdua.com
}
if(pre[i]=='*')
{
if(stack1[top1]!='*'&&stack1[top1]!='/')
stack1[++top1]=pre[i];
else {stack2[++top2]=stack1[top1--];goto up;}
}
if(pre[i]=='/')
{
if(stack1[top1]!='/')
stack1[++top1]=pre[i];
else {stack2[++top2]=stack1[top1--];goto up;}
}
if(pre[i]==')')
{
for(;;)
{
stack2[++top2]=stack1[top1--];
if(stack1[top1]=='(')
{top1--;break;}
}
}
}
else stack2[++top2]=pre[i];
}
stack2[++top2]='\0';
printf("the postfix is :%s",stack2);
getch();
}
www.rishabhdua.com
www.rishabhdua.com
Experiment - 3
Program Name : Write a program to find FIRST of NON TERNINALS of the given grammar
#include"stdio.h"
#include<conio.h>
char array[10][20],temp[10];
int c,n;void fun(int,int[]);
int fun2(int i,int j,int p[],int key)
{
int k;
if(!key)
{
for(k=0;k<n;k++)
if(array[i][j]==array[k][0])
break;
p[0]=i;p[1]=j+1;
fun(k,p);
return 0;
}
else
{
for(k=0;k<=c;k++)
{
if(array[i][j]==temp[k])
break;
}
if(k>c)return 1;
else return 0;
}
}
void fun(int i,int p[])
{
int j,k,key;
for(j=2;array[i][j]!='\0';j++)
{
if(array[i][j-1]=='/')
{
if(array[i][j]>='A'&&array[i][j]<='Z')
{
key=0;
fun2(i,j,p,key);
}
else
{
www.rishabhdua.com
www.rishabhdua.com
key=1;
if(fun2(i,j,p,key))
temp[++c]=array[i][j];
if(array[i][j]=='@'&&p[0]!=-1)
{ //taking ,@, as null symbol.
if(array[p[0]][p[1]]>='A'&&array[p[0]][p[1]]<='Z')
{
key=0;
fun2(p[0],p[1],p,key);
}
else
if(array[p[0]][p[1]]!='/'&&array[p[0]][p[1]]!='\0')
{
if(fun2(p[0],p[1],p,key))
temp[++c]=array[p[0]][p[1]];
}
}
}
}
}
}
void main()
{
int p[2],i,j;
clrscr();
printf("Enter the no. of productions :");
scanf("%d",&n);
printf("Enter the productions :\n");
for(i=0;i<n;i++)
scanf("%s",array[i]);
for(i=0;i<n;i++)
{
c=-1,p[0]=-1,p[1]=-1;
fun(i,p);
printf("First(%c) : [ ",array[i][0]);
for(j=0;j<=c;j++)
printf("%c,",temp[j]);
printf("\b ].\n");
getch();
}
}
Output / Conclusion : Enter the no. of productions :6
Enter the productions :
S/aBDh
B/cC
C/bC/@
D/E/F
E/g/@
F/f/@
First(S) : [ a ].
First(B) : [ c ].
First(C) : [ b,@ ].
First(D) : [ g,@,f ].
First(E) : [ g,@ ].
First(F) : [ f,@ ].
www.rishabhdua.com
www.rishabhdua.com
Experiment - 4
#include"stdio.h"
#include<conio.h>
#define max 10
#define MAX 15
char array[max][MAX],temp[max][MAX];
int c,n,t;void fun(int,int[]);
int fun2(int i,int j,int p[],int key)
{
int k;
if(!key)
{
for(k=0;k<n;k++)
if(array[i][j]==array[k][0])
break;
p[0]=i;p[1]=j+1;
fun(k,p);
return 0;
}
else
{
for(k=0;k<=c;k++)
{
if(array[i][j]==temp[t][k])
break;
}
if(k>c)return 1;
else return 0;
}
}
void fun(int i,int p[])
{
int j,k,key;
for(j=2;array[i][j]!='\0';j++)
{
if(array[i][j-1]=='/')
{
if(array[i][j]>='A'&&array[i][j]<='Z')
{
key=0;
fun2(i,j,p,key);
}
else
{
www.rishabhdua.com
www.rishabhdua.com
key=1;
if(fun2(i,j,p,key))
temp[t][++c]=array[i][j];
if(array[i][j]=='@'&&p[0]!=-1)
{ //taking ,@, as null symbol.
if(array[p[0]][p[1]]>='A'&&array[p[0]][p[1]]<='Z')
{
key=0;
fun2(p[0],p[1],p,key);
}
else
if(array[p[0]][p[1]]!='/'&&array[p[0]][p[1]]!='\0')
{
if(fun2(p[0],p[1],p,key))
temp[t][++c]=array[p[0]][p[1]];
}
}
}
}
}
}
char fol[max][MAX],ff[max];int f,l,ff0;
void ffun(int,int);
void follow(int i)
{
int j,k;
for(j=0;j<=ff0;j++)
if(array[i][0]==ff[j])
return 0;
if(j>ff0)ff[++ff0]=array[i][0];
if(i==0)fol[l][++f]='$';
for(j=0;j<n;j++)
for(k=2;array[j][k]!='\0';k++)
if(array[j][k]==array[i][0])
ffun(j,k);
}
void ffun(int j,int k)
{
int ii,null=0,tt,cc;
if(array[j][k+1]=='/'||array[j][k+1]=='\0')
null=1;
for(ii=k+1;array[j][ii]!='/'&&array[j][ii]!='\0';ii++)
{
if(array[j][ii]<='Z'&&array[j][ii]>='A')
{
for(tt=0;tt<n;tt++)
if(temp[tt][0]==array[j][ii])break;
for(cc=1;temp[tt][cc]!='\0';cc++)
{
if(temp[tt][cc]=='@')null=1;
else fol[l][++f]=temp[tt][cc];
}
}
else fol[l][++f]=array[j][ii];
}
if(null)follow(j);
}
www.rishabhdua.com
www.rishabhdua.com
void main()
{
int p[2],i,j;
clrscr();
printf("Enter the no. of productions :");
scanf("%d",&n);
printf("Enter the productions :\n");
for(i=0;i<n;i++)
scanf("%s",array[i]);
for(i=0,t=0;i<n;i++,t++)
{
c=0,p[0]=-1,p[1]=-1;
temp[t][0]=array[i][0];
fun(i,p);
temp[t][++c]='\0';
printf("First(%c) : [ ",temp[t][0]);
for(j=1;j<c;j++)
printf("%c,",temp[t][j]);
printf("\b ].\n");
getch();
}
/* Follow Finding */
for(i=0,l=0;i<n;i++,l++)
{
f=-1;ff0=-1;
fol[l][++f]=array[i][0];
follow(i);
fol[l][++f]='\0';
}
for(i=0;i<n;i++)
{
printf("\nFollow[%c] : [ ",fol[i][0]);
for(j=1;fol[i][j]!='\0';j++)
printf("%c,",fol[i][j]);
printf("\b ]");
getch();
}
}
www.rishabhdua.com
www.rishabhdua.com
Follow[S] : [ $ ]
Follow[B] : [ g,f,h,$ ]
Follow[C] : [ g,f,h,$ ]
Follow[D] : [ h ]
Follow[E] : [ h ]
Follow[F] : [ h ]
www.rishabhdua.com
www.rishabhdua.com
Experiment - 5
www.rishabhdua.com
www.rishabhdua.com
for(k=lastentry;k>0;k=k-1)
if(strcmp(symtable[k].lexptr,s)==0)
return k;
return 0;
}
void parser(){
lookahead =lexer();
while(lookahead!=DONE)
{
E();
Match(';');
}}
void main(){
char ans;
clrscr();
printf("program for recursive descent parsing \n");
Initialize();
printf("enter the expression");
printf("and place; at the end");
printf("press ctrl to terminate");
parser();
}
www.rishabhdua.com
www.rishabhdua.com
Experiment - 6
#include<stdio.h>
#include<conio.h>
void push(char,char[],int);
void pop(int);
void main()
{
char a[4][10]={
{'E','=','E','+','E'},
{'E','=','E','*','E'},
{'E','=','(','E',')'},
{'E','=','id'}};
char st[20],item;
int top=0;
st[top]='$';
char str[10]={'id','+','(','id','*','id',')'};
clrscr();
for(int j=0;j<=7;j++)
{
push(str[j],st,top);
if(st[top]==a[3][2])
{
pop(top);
push('E',st,top);
}
else
{
for(int i=0;i<4;i++)
{
if(st[top]==a[i][2])
{
if(st[top--]==a[i][3])
if(st[top--]==a[i][4])
{
pop(top);
push(a[i][1],st,top);
}
}
}}
}
if(st[top]=='$')
printf("accept");
getch();
}
void push(char item,char st[],int top)
{
top=top+1;
st[top]=item;
www.rishabhdua.com
www.rishabhdua.com
}
void pop(int top)
{ top=top-1;
}
www.rishabhdua.com
www.rishabhdua.com
Experiment - 7
Theory Concepts : Every compiler has intermediate code representation phase from which
code is optimized in code optimization phase
Output / Conclusion :
www.rishabhdua.com
www.rishabhdua.com
Experiment - 8
Theory Concepts : Constant folding is the simplest code optimization to understand. Let us
suppose that you write the statement x = 45 * 88; in your C program. A non-optimizing compiler
will generate code to multiply 45 by 88 and store the value in x. An optimizing compiler will detect
that both 45 and 88 are constants, so their product will also be a constant. Hence it will find 45 * 88
= 3960 and generate code that simply copies 3960 into x. This is constant folding, and means the
calculation is done just once, at compile time, rather than every time the program is run. To
illustrate this, create the file test2.c as shown below.
Implementation :
/* Demonstration of constant folding */
#include <stdio.h>
int main()
{
int x, y, z;
x = 10;
y = x + 45;
z = y + 4;
printf("The value of z = %d", z);
return 0;
}
Output / Conclusion : constant folding, the compiler evaluates the various expressions in the
program only once and plugs the final value into the generated code. One more interesting thing to
observe is that after constant folding, there is no need of the variables x, y and z. Therefore no
space for them is allocated on the stack, thus reducing the memory requirement of the program.
This brings out the fact that one optimization may lead to another one. In the above case constant
folding lead to a decrease in run time (since all the computations are done at compile time) and also
to a decrease in the space requirements. Optimization is done.
www.rishabhdua.com
www.rishabhdua.com
Experiment - 9
Theory Concepts : Many a times, it happens that the same expression is evaluated at different
places in the same program and the values of the operands in the expression do
not change in between the two evaluations of that expression. For example, the
program may evaluate say a * b at the beginning and at the end. If the values of
a and b do not change in between these two evaluations of a * b, then instead of
evaluating a * b again at the end, we can save the result of the evaluation of a
* b at the beginning in some temporary variable and use it at the end. This will
help eliminate redundant computations in the program. This optimization is
called as common subexpression elimination. Consider the following program that
demonstrates it.
Implementation :
/* common subexpression elimination */
#include <stdio.h>
int main()
{
int a, b;
int x, y, z;
scanf("%d %d", &a, &b);
x = a * b;
if(b >= 4)
{
y = a * b;
z = 0;
}
else
{
z = a * b * 4;
y = 0;
}
printf("x = %d, y = %d, z = %d\n", x, y, z);
return 0;
}
www.rishabhdua.com
www.rishabhdua.com
Experiment - 10
Theory Concepts : One type of code optimization is strength reduction in which a "costly"
operation is replaced by a less expensive one. For example, the evaluation of x2
is much more efficient if we multiply x by x rather than call the exponentiation
routine. One place where this optimization can be applied is in loops. Many
times in a loop, one variable changes in synchronization with the loop variable.
Such a variable is called as induction variable. Induction variables give the
compiler a chance to apply strength reduction, as can be seen from the following
program.
Implementation :
/* demonstration of induction variable elimination */
int main()
{
int i, j;
www.rishabhdua.com