You are on page 1of 23

MARWADI UNIVERSITY

DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

COMPILER DESIGN
(01CE0601)

2021-2022

STUDENT LAB MANUAL

Sudarshan 919001031 Page | 1


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

INDEX
ExN Page
Title Date Grade Sign
o No
Write a Program to remove Left Recursion from
1 the grammar.
Write a Program to remove Left Factoring from the
2 grammar.
Write a Program to compute FIRST Set of the
3 grammar.
Write a Program to compute FOLLOW Set of the
4
grammar.
Write a Program to implement Operator
5 precedence parser.
Implementation of Finite Automata and string
6 validation.
Working and Installation of Lex tool in
7
windows/Ubuntu.
Write a Lex Program to count words, characters,
8 lines, Vowels and consonants from given input.
Implement Following Programs using lex.
Write a Lex Program to generate string
9 which is ending with zeros.
Write a Lex Program to check given string
is simple or compound string.
Implement Following Programs using lex.
Write a Lex Program to check given
10 number is positive negative or zero.
Write a Lex Program to print HTML tags
of given file.
Write a Lex Program to count the total number of
printf and scanf statement in given C file. Also
11
convert it into readf and writeout respectively to
another file.
12 Write a YACC Program to generate Calculator.
Write a Program with the help of Lex and Yacc file
to implement Calculator which performs basic
13
operations like addition, subtraction, multiplication
and division.
Generate three tuple intermediate code for given
14 infix expression.

Sudarshan 919001031 Page | 2


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

Experiment 1
Title: Write a Program to remove Left Recursion from the grammar.

Program:

#include<stdio.h>
int lengthofstring(char str[])
{
int i=0;
while(str[i]!='\0') {
i++;
}
return i;
}

int main() {

char a,b,str[10];
int j;
printf("Enter the string : ");
scanf("%s", &str);

int istate=1,cstate=1;
int len = lengthofstring(str);
if(len==3)
{
if(istate==1 && str[0]=='a')
{
cstate=2;
}
if(cstate==2 && str[1]=='b')
{
cstate=3;
}
if(cstate==3 && str[2]=='b')
{
cstate=4;
}

Sudarshan 919001031 Page | 3


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

if(cstate==4)
{
printf("String has been accpeted");
}
else
{
printf("String cannot be accpeted");
}
}

else
{
printf("String cannnot be accpeted ");
}

Output:

Experiment 2
Title: Write a Program to remove Left Factoring from the grammar.

Program:
#include<stdio.h>
#include<string.h>
int main()
{

Sudarshan 919001031 Page | 4


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

char gram[20],part1[20],part2[20],modifiedGram[20],newGram[20],tempGram[20];
int i,j=0,k=0,l=0,pos;
printf("Enter Production : A->");
gets(gram); for(i=0;gram[i]!
='|';i++,j++)
part1[j]=gram[i];
part1[j]='\0';
for(j=++i,i=0;gram[j]!='\0';j++,i++)
part2[i]=gram[j];
part2[i]='\0'; for(i=0;i<strlen(part1)||
i<strlen(part2);i++)

{
if(part1[i]==part2[i])
{
modifiedGram[k]=part1[i];
k++;
pos=i+1;
}
}
for(i=pos,j=0;part1[i]!='\0';i++,j++){
newGram[j]=part1[i];
}
newGram[j++]='|';
for(i=pos;part2[i]!='\0';i++,j++){
newGram[j]=part2[i];
}
modifiedGram[k]='X';
modifiedGram[++k]='\0';
newGram[j]='\0';
printf("\n A->%s",modifiedGram);
printf("\n X->%s\n",newGram);
}

Output:

Sudarshan 919001031 Page | 5


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

Experiment 3
Title: Write a Program to compute FIRST Set of the grammar.

Program:
#include<stdio.h>
#include<ctype.h>
void FIRST(char );
int count,n=0;
char prodn[10][10], first[10];
int main()
{
int i,choice;
char c,ch;
printf("How many productions do you want? :");
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++)

Sudarshan 919001031 Page | 6


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

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:

Sudarshan 919001031 Page | 7


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

Experiment 4
Title: Write a Program to compute FOLLOW Set of the grammar.

Program:
#include<stdio.h>
#include<string.h>
int n,m=0,p,i=0,j=0;
char a[10][10],followResult[10];
void follow(char c);
void first(char c);
void addToResult(char);
int main()
{
int i;
int choice;
char c,ch;
printf("Enter the no.of productions: ");
scanf("%d", &n);
printf(" Enter %d productions\nProduction with multiple terms should be give as separate
productions \n", n);
for(i=0;i<n;i++) scanf("%s
%c",a[i],&ch);
// gets(a[i]);
do
{ m=
0;
printf("Find FOLLOW of -->");
scanf(" %c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",followResult[i]);
printf(" }\n");
printf("Do you want to continue(Press 1 to continue. . .)?");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
}
void follow(char c)
{

Sudarshan 919001031 Page | 8


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

if(a[0][0]==c)addToResult('$');
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
}
}
}
}
void first(char c)
{
int k; if(!
(isupper(c)))
//f[m++]=c;
addToResult(c);
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$') follow(a[i][0]);
else if(islower(a[k][2]))
//f[m++]=a[k][2];
addToResult(a[k][2]);
else first(a[k][2]);
}
}
}
void addToResult(char c)
{
int i;
for( i=0;i<=m;i++)
if(followResult[i]==c)
return;
followResult[m++]=c;
}

Output:

Sudarshan 919001031 Page | 9


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

Experiment 5
Title: Write a Program to implement Operator precedence parser.

Program:
#include<stdio.h>
#include<string.h>

char *input;
int i=0;
char lasthandle[6],stack[50],handles[][5]={")E(","E*E","E+E","i","E^E"};
//(E) becomes )E( when pushed to stack

int top=0,l;
char prec[9][9]={

/*input*/

/*stack + - * / ^ i ( ) $ */

/* + */ '>', '>','<','<','<','<','<','>','>',

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

/* - */ '>', '>','<','<','<','<','<','>','>',

/* * */ '>', '>','>','>','<','<','<','>','>',

/* / */ '>', '>','>','>','<','<','<','>','>',

/* ^ */ '>', '>','>','>','<','<','<','>','>',

/* i */ '>', '>','>','>','>','e','e','>','>',

/* ( */ '<', '<','<','<','<','<','<','>','e',

/* ) */ '>', '>','>','>','>','e','e','>','>',

/* $ */ '<', '<','<','<','<','<','<','<','>',

};

int getindex(char c)
{
switch(c)
{
case '+':return 0;
case '-':return 1;
case '*':return 2;
case '/':return 3;
case '^':return 4;
case 'i':return 5;
case '(':return 6;
case ')':return 7;
case '$':return 8;
}
}

int shift()
{
stack[++top]=*(input+i++);
stack[top+1]='\0';
}

int reduce()

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

{
int i,len,found,t;
for(i=0;i<5;i++)//selecting handles
{
len=strlen(handles[i]);
if(stack[top]==handles[i][0]&&top+1>=len)
{
found=1;
for(t=0;t<len;t++)
{
if(stack[top-t]!=handles[i][t])
{
found=0;
break;
}
}
if(found==1)
{
stack[top-t+1]='E';
top=top-t+1;
strcpy(lasthandle,handles[i]);
stack[top+1]='\0';
return 1;//successful reduction
}
}
}
return 0;
}

void dispstack()
{
int j;
for(j=0;j<=top;j++)
printf("%c",stack[j]);
}

void dispinput()
{
int j;

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

for(j=i;j<l;j++)
printf("%c",*(input+j));
}

void main()
{
int j;

input=(char*)malloc(50*sizeof(char));
printf("\nEnter the string\n");
scanf("%s",input);
input=strcat(input,"$");
l=strlen(input);
strcpy(stack,"$"); printf("\nSTACK\tINPUT\
tACTION"); while(i<=l)
{
shift(); printf("\
n"); dispstack();
printf("\t");
dispinput();
printf("\tShift");
if(prec[getindex(stack[top])][getindex(input[i])]=='>')
{
while(reduce())
{
printf("\n");
dispstack();
printf("\t");
dispinput();
printf("\tReduced: E->%s",lasthandle);
}
}
}

if(strcmp(stack,"$E$")==0)
printf("\nAccepted;");
else
printf("\nNot Accepted;");
}

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

Output:

Experiment 6
Title: Implementation of Finite Automata and string validation.

Program:
#include<stdio.h>
int lengthofstring(char str[])
{
int i=0;
while(str[i]!='\0') {
i++;
}
return i;
}

int main() {

char a,b,str[10];
int j;
printf("Enter the string : ");
scanf("%s", &str);

int istate=1,cstate=1;
int len = lengthofstring(str);
if(len==3)

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

{
if(istate==1 && str[0]=='a')
{
cstate=2;
}
if(cstate==2 && str[1]=='b')
{
cstate=3;
}
if(cstate==3 && str[2]=='b')
{
cstate=4;
}
if(cstate==4)
{
printf("String has been accpeted");
}
else
{
printf("String cannot be accpeted");
}
}

else
{
printf("String cannnot be accpeted ");
}

Output:

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

Experiment 7
Title: Working and Installation of Lex tool in windows/Ubuntu

Program:

Steps of Working :
1. A source program written in Lex is given as input to the Lex Compiler
2. The Lex compiler produces a lex.yy.c file which acts as the input for C compiler
3. The C compiler always produces a.out exe file
4. The a.exe takes input stream and produces the desired output.

Installation on Windows :
1. Visit the site :https://techapple.net/2014/07/flex-windows-lex-and-yacc-flex-and-bison-
installer-for-windows-xp788-1/
2. Go at the bottom of the site and download the flex for windows.
3. Double click on the downloaded software and just follow the windows installation wizard
4. Open command prompt and then type lex , to see the details and confirm the
installation of the lex

Output:

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

Experiment 8
Title: Write a Lex Program to count words, characters, lines, Vowels and
consonants from given input.

Program:

%{
int l=1,w=1,ch=0,v=0,c=0;
%}
%%
\n {l++;w++;}
[\t ""] {w++;}
[aeiouAEIOU] {v++;ch++;}
[a-zA-Z] {c++;ch++;}
%%
void main()
{
yyin = fopen("abc.txt","r");
yylex();
printf("Total number of words = %d\n", w);
printf("Total number of characters = %d\n", ch);

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

printf("Total number of lines = %d\n", l);


printf("Total number of vowels = %d\n", v);
printf("Total number of consonant = %d", c);
}
int yywrap()
{
return 1;
}

Output:

Experiment 9
Title: Implement Following Programs using lex.
Write a Lex Program to generate string which is ending with zeros.

Program:
%{
#include<stdio.h>
%}
%%
[0+1]*0 {printf("String accepetd");}
.* {printf("String has been rejected ");}
%%
void main()
{
printf("Enter input : ");
yylex();

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

}
int yywrap()
{
return 1;
}

Output:

Title: Implement Following Programs using lex.


Write a Lex Program to check given string is simple or compound string.

Program:
%{
#include<stdio.h>
int simple=1;
%}
%%
[ \t]+[aA][nN][dD][ \t]+ {simple=0;}
[ \t]+[oO][rR][ \t]+ {simple=0;}
[ \t]+[bB][uU][tT][ \t]+ {simple=0;}
. {;}
%%
void main()
{
printf("Enter the string : ");
yylex();
if(simple==1)
{

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

printf("Simple string");
}
else
{
printf("Compound String");
}}
int yywrap()
{
return 1;
}
Output:

Experiment 10
Title:
Implement Following Programs using lex.
Write a Lex Program to check given number is positive negative or zero.

Program:
%{
%}
%%
0* {printf("given number is 0");}
^[-][0-9]+ {printf("its negative");}
^[+][0-9]+ {printf("its positive");}
[0-9]+ {printf("its positive");}
.* {printf("Not a valid number");}
%%
int main()
{
printf("Enter the number : ");
yylex();

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

}
int yywrap()
{
return 1;
}

Output:

Title:
Implement Following Programs using lex.
Write a Lex Program to print HTML tags of given file.

Program:
%{
#include<stdio.h>
%}
%%
\<[^>]*\> {fprintf(yyout,"%s",yytext);}
.|\n;
%%
int main()
{
yyin = fopen("test.html", "r");
yyout= fopen("output.txt", "w");
yylex();
return 0;
}
int yywrap()
{
return 1;

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

Output:

Experiment 11
Title: Write a Lex Program to count the total number of printf and scanf statement
in given C file. Also convert it into readf and writeout respectively to another file.

Program:
%{
#include<stdio.h>
int sf=0, pf=0;
%}

%%
"scanf" {sf++;fprintf(yyout, "readf");}
"printf" {pf++;fprintf(yyout, "writeout");}
%%

int main()
{
yyin = fopen("test.c", "r");
yyout = fopen("result.c", "w");
yylex();
printf("the nummber of printf is %d and scanf is %d ", pf, sf);
}
int yywrap()

Sudarshan 919001031 Page |


MARWADI UNIVERSITY
DEPARTMENT OF COMPUTER
ENGINEERING CLASS: TC3 BATCH:

{
return 1;
}

Output:

Sudarshan 919001031 Page |

You might also like