Vous êtes sur la page 1sur 12

COMPILER DESIGN(13CS401)

LIST OF PROGRAMS

SNo. Program CO
4 Write a program to eliminate Left Recursion from the grammar CO2
5 Write a program to compute FIRST set CO2
6 Write a program to compute FOLLOW set CO2
7 Write a program for constructing LL(1) parsing CO2
CO-2

4. Write a program to eliminate Left Recursion from the grammar.

Aim: Program to eliminate left recursion from the given grammar.

Algorithm:
1. Assign an ordering ​A​1,…,​An​ to the nonterminals of the grammar.
2. for ​i​:=1to ​n​ do begin
3. for ​j​:=1 to ​i​−1 do begin
4. for each production of the form ​Ai→ ​ ​Ajα​ do begin
5. remove ​Ai​→​Ajα​ from the grammar
6. ​ ​β​ do begin
for each production of the form ​Aj→
7. add ​Ai​→​βα​ to the grammar
end
end
end
8. transform the ​Ai​-productions to eliminate direct left recursion
end

Program:
#include<stdio.h>
#include<string.h>
#define SIZE 10
int main ( )
{
char non_terminal;
char beta,alpha;
int num;
char production[10][SIZE];
int index=3; /* starting of the string following "->" */
printf("Enter Number of Production : ");
scanf("%d",&num);
printf("Enter the grammar as E->E-A :\n");
for(int i=0;i<num;i++)
{
scanf("%s",production[i]);
}
for(int i=0;i<num;i++)
{
printf("\nGRAMMAR : : : %s",production[i]);
non_terminal=production[i][0];
if(non_terminal==production[i][index])
{
alpha=production[i][index+1];
printf(" is left recursive.\n");
while(production[i][index]!=0 && production[i][index]!='|')
index++;
if(production[i][index]!=0)
{
beta=production[i][index+1];
printf("Grammar without left recursion:\n");
printf("%c->%c%c\'",non_terminal,beta,non_terminal);
printf("\n%c\'->%c%c\'|E\n",non_terminal,alpha,non_terminal);
}
else
printf(" can't be reduced\n");
}
else
printf(" is not left recursive.\n");
index=3;
}
}

Output:
Enter Number of Production: 3
Enter the grammar as E ->E-A :
E ->EA | A
A ->AT | a
T -> a

GRAMMAR : : : E ->EA| A is left recursive. 


Grammar without left recursion: 
E ->AE ​  
E ​ ​ ->AE ​ ​|​ϵ 
 
GRAMMAR : : : A ->AT| a is left recursive. 
Grammar without left recursion: 
A ->aA ​  
A ​ ​ ->TA ​ ​|​ϵ 
 
GRAMMAR : : : T ->a is not left recursive. 
 
5. Write a program to compute FIRST set.

Aim: ​Program to compute FIRST set for a given grammar of non-terminals.

Algorithm:
The rules for finding FIRST of a given grammar is:

1. If X is terminal, FIRST(X) = {X}.


2. If X → ε is a production, then add ε to FIRST(X).
3. If X is a non-terminal, and X → Y​1​ Y​2​ … Y​k​ is a production, and ε is in all of FIRST(Y​1​), …,
FIRST(Y​k​), then add ε to FIRST(X).
4. If X is a non-terminal, and X → Y​1​ Y​2​ … Y​k​ is a production, then add a to FIRST(X) if for some
i, a is in FIRST(Y​i​), and ε is in all of FIRST(Y​1​), …, FIRST(Y​i-1​).

Program:
#include​<stdio.h>
#include​<ctype.h>
void ​FIRST(​char​[],​char​);
void ​addToResultSet(​char​[],​char​);
int ​numOfProductions;
char ​productionSet[​10​][​10​];
main( )
{
int ​i;
char ​choice,c,result[20];
printf(​"How many number of productions?:"​);
scanf(​"%d"​,&numOfProductions);
for​(i=​0​;i<numOfProductions;i++​) //read production string eg:E=E+T
{
printf(​"Enter productions Number %d:"​,i+​1​);
scanf(​"%s"​,productionSet[i]);
}
do
{
printf(​"\nFind the FIRST of:"​);
scanf(​"%c"​,&c);
FIRST(result,c); //Compute FIRST; Get Answer in 'result' array
printf(​"\nFIRST(%c)={"​,c);
for​(i=​0​;result[i]!=​'\0'​;i++)
printf(​"%c"​,result[i]); //Display result
printf(​"}\n"​);
printf(​"press 'y' to continue:"​);
scanf(​"%c"​,&choice);
}
while​(choice==​'y'​||choice==​'Y'​);
}
/*
*Function FIRST:
*Compute the elements in FIRST(c) and write them in Result Array.
*/
void ​FIRST(​char ​*Result, ​char ​c)
{
int ​i,j,k;
char ​subResult[​20​];
int ​foundEpsilon;
subResult[​0​]=​'\0'​;
Result[​0​]=​'\0'​;
//If X is terminal, FIRST(X)={X}.
if​(!(isupper(c)))
{
addToResultSet(Result,c);
return​;
}
//If X is nonterminal
//Read each production
for​(i=​0​;i<numOfProductions;i++)
{
//Find production with X as LHS
if​(productionSet[i][​0​]==c)
{
//If X→ε is a production, then add ε to​ FIRST(X).
​if​(productionSet[i][​2​]==​'$'​)
addToResultSet(Result,​'$'​);
//If X is a non-terminal, and X→Y​1​Y​2​…Y​k​ is a production, then add a to FIRST(X)
//if for some i, a is inFIRST(Yi),and ε is in all of FIRST(Y1),…,FIRST(Yi-1).
else
{
j=​2​;
while​(productionSet[i][j]!=​'\0'​)
{
foundEpsilon=​0​;
FIRST(subResult,productionSet[i][j]);
for​(k=​0​;subResult[k]!=​'\0'​;k++)
addToResultSet(Result,subResult[k]);
for​(k=​0​;subResult[k]!=​'\0'​;k++)
if​(subResult[k]==​'$'​)
{
foundEpsilon=​1​;
break​;
}
//Noεfound,noneedtochecknextelement
if​(!foundEpsilon)
break​;
j++;
}
}
}
}
return​;
}
/*addToResultSet adds the computed element to result set.This code avoids multiple inclusion of
elements */
void ​addToResultSet(​char ​Result[], ​char ​val)
{
Int ​k;
for​(k=​0​;Result[k]!=​'\0'​;k++)
if​(Result[k]==val)
return​;
Result[k]=val;
Result[k+​1​]=​'\0'​;
}

Output:
6. Write a program to compute FOLLOW set.

Aim: ​Program to compute FOLLOW set for a given grammar of non-terminals.

Algorithm:
Rules to compute FOLLOW set:
1. FOLLOW(S) = { $ } // where S is the starting Non-Terminal
2. If A -> pBq is a production, where p, B and q are any grammar symbols,then everything in
FIRST(q) except Є is in FOLLOW(B.
3. If A->pB is a production, then everything in FOLLOW(A) is in FOLLOW(B).
4. If A->pBq is a production and FIRST(q) contains Є, then FOLLOW(B) contains
{ FIRST(q) – Є } U FOLLOW(A)

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,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);
​}
​ hile​(choice==​1​);
w
}

void​ ​follow(​char​ ​c)


{
​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​;
f​ ollowResult[m++]=c;
}

Output:
7. Write a program for constructing LL(1) parsing.

Aim: ​Program to construct LL(1) parsing.

Algorithm:
1. If X=a=$ , parser halts, string accepted.

2. If X=a !=$ , parser pops X, and advances the input pointer to point to next input symbol.

3. If X is a nonterminal, the program consults entry M[X,a] of the parsing table M. Replace the top
of stack(X) with production rule cossesponding to entry in table.If entry = ERROR, call error
recovery routine.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char s[20],stack[20];
void main()
{
char m[5][6][3]={"tb"," "," ","tb"," "," "," ","+tb"," "," ","n","n","fc"," "," ","fc"," "," "," ","n","*fc","
a","n","n","i"," "," ","(e)"," "," "};
int size[5][6]={2,0,0,2,0,0,0,3,0,0,1,1,2,0,0,2,0,0,0,1,3,0,1,1,1,0,0,3,0,0};
int i,j,k,n,str1,str2;
clrscr();
printf("\n Enter the input string: ");
scanf("%s",s);
strcat(s,"$");
n=strlen(s);
stack[0]='$';
stack[1]='e';
i=1;
j=0;
printf("\nStack Input\n");
printf("__________________\n");
while((stack[i]!='$')&&(s[j]!='$'))
{
if(stack[i]==s[j])
{
i--;
j++;
}
switch(stack[i])
{
case 'e': str1=0;
break;
case 'b': str1=1;
break;
case 't': str1=2;
break;
case 'c': str1=3;
break;
case 'f': str1=4;
break;
}
switch(s[j])
{
case 'i': str2=0;
break;
case '+': str2=1;
break;
case '*': str2=2;
break;
case '(': str2=3;
break;
case ')': str2=4;
break;
case '$': str2=5;
break;
}
if(m[str1][str2][0]=='\0')
{
printf("\nERROR");
exit(0);
}
else if(m[str1][str2][0]=='n')
i--;
else
if(m[str1][str2][0]=='i')
stack[i]='i';
else
{
for(k=size[str1][str2]-1;k>=0;k--)
{
stack[i]=m[str1][str2][k];
i++;
}
i--;
}
for(k=0;k<=i;k++)
printf(" %c",stack[k]);
printf(" ");
for(k=j;k<=n;k++)
printf("%c",s[k]);
printf(" \n ");
}
printf("\n SUCCESS");
getch();
}

Output:

Enter the input string: i*i+i

Stack INPUT
$bt i*i+i$
$bcf i*i+i$
$bci i*i+i$
$bc *i+i$
$bcf* *i+i$
$bcf i+i$
$bci i+i$
$bc +i$
$b +i$
$bt+ +i$
$bt i$
$bcf i$
$ bci i$
$bc $
$b $
$ $
success

Vous aimerez peut-être aussi