0% found this document useful (0 votes)
19 views

LP manual Programs

The document contains multiple code snippets related to compiler design, including implementations for computing FIRST and FOLLOW sets, predictive parsing tables, and generating three-address code. Each section demonstrates different aspects of parsing and grammar analysis in programming languages. The code is written in C and C++ and showcases various algorithms for handling productions and parsing strings.

Uploaded by

tusharw.cseb19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

LP manual Programs

The document contains multiple code snippets related to compiler design, including implementations for computing FIRST and FOLLOW sets, predictive parsing tables, and generating three-address code. Each section demonstrates different aspects of parsing and grammar analysis in programming languages. The code is written in C and C++ and showcases various algorithms for handling productions and parsing strings.

Uploaded by

tusharw.cseb19
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Prac 4

Code:
Prac 5
Code:

#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; char c;
char 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("\n Find the FIRST of :"); scanf(" %c",&c);
FIRST(result,c); //Compute FIRST; Get Answer in 'result' array printf("\n FIRST(%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');
}
void FIRST(char* Result,char c)
{
int i,j,k;
char subResult[20]; int foundEpsilon; subResult[0]='\0'; Result[0]='\0';
if(!(isupper(c)))
{
addToResultSet(Result,c); return ;
}
for(i=0;i<numOfProductions;i++)
{
if(productionSet[i][0]==c)
{
if(productionSet[i][2]=='$') addToResultSet(Result,'$'); 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;
}
if(!foundEpsilon) break;
j++;
}
}
}
}
return ;
}
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';
}
Prac 6
Code:

#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)
{
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;
}
}
Prac 7
#include<stdio.h> #include<string.h>
char prol[7][10]={"S","A","A","B","B","C","C"};
char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"};
char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@","C->Cc","C->@"};
char first[7][10]={"abcd","ab","cd","a@","@","c@","@"};
char follow[7][10]={"$","$","$","a$","b$","c$","d$"}; char table[5][6][10];
numr(char c)
{
switch(c)
{
case 'S': return 0; case 'A': return 1; case 'B': return 2; case 'C': return 3; case 'a': return 0; case 'b':
return 1; case 'c': return 2; case 'd': return 3; case '$': return 4;
}
return(2);
}
void main()
{
int i,j,k; for(i=0;i<5;i++) for(j=0;j<6;j++) strcpy(table[i][j]," ");
printf("\nThe following is the predictive parsing table for the following grammar:\n");
for(i=0;i<7;i++)
printf("%s\n",prod[i]); printf("\nPredictive parsing table is\n"); fflush(stdin);
for(i=0;i<7;i++)
{
k=strlen(first[i]); for(j=0;j<10;j++) if(first[i][j]!='@')
strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]);
}
for(i=0;i<7;i++)
{
if(strlen(pror[i])==1)
{
if(pror[i][0]=='@')
{
k=strlen(follow[i]); for(j=0;j<k;j++)
strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])+1],prod[i]);
}
}
}
strcpy(table[0][0]," ");
strcpy(table[0][1],"a");
strcpy(table[0][2],"b");
strcpy(table[0][3],"c");
strcpy(table[0][4],"d");
strcpy(table[0][5],"$");
strcpy(table[1][0],"S");
strcpy(table[2][0],"A");
strcpy(table[3][0],"B");
strcpy(table[4][0],"C");
printf("\n \n"); for(i=0;i<5;i++)
for(j=0;j<6;j++)
{
printf("%-10s",table[i][j]); if(j==5)
printf("\n \n");
}
}
Prac 8
Code:

#include<iostream> #include<string> #include<deque> using namespace std; int n,n1,n2;


int getPosition(string arr[], string q, int size)
{
for(int i=0;i<size;i++)
{
if(q == arr[i]) return i;
}
return -1;
}
int main()
{
string prods[10],first[10],follow[10],nonterms[10],terms[10]; string pp_table[20][20] = {};
cout<<"Enter the number of productions : "; cin>>n;
cin.ignore();
cout<<"Enter the productions"<<endl; for(int i=0;i<n;i++)
{
getline(cin,prods[i]);
cout<<"Enter first for "<<prods[i].substr(3)<<" : "; getline(cin,first[i]);
}
cout<<"Enter the number of Terminals : "; cin>>n2;
cin.ignore();
cout<<"Enter the Terminals"<<endl; for(int i=0;i<n2;i++)
{
cin>>terms[i];
}
terms[n2] = "$"; n2++;
cout<<"Enter the number of Non-Terminals : "; cin>>n1;
cin.ignore();
for(int i=0;i<n1;i++)
{
cout<<"Enter Non-Terminal : "; getline(cin,nonterms[i]);
cout<<"Enter follow of "<<nonterms[i]<<" : ";
getline(cin,follow[i]);
}
cout<<endl; cout<<"Grammar"<<endl; for(int i=0;i<n;i++)
{
cout<<prods[i]<<endl;
}
for(int j=0;j<n;j++)
{
int row = getPosition(nonterms,prods[j].substr(0,1),n1); if(prods[j].at(3)!='#')
{
for(int i=0;i<first[j].length();i++)
{
int col = getPosition(terms,first[j].substr(i,1),n2); pp_table[row][col] = prods[j];
}
}
else
{
for(int i=0;i<follow[row].length();i++)
{
int col = getPosition(terms,follow[row].substr(i,1),n2); pp_table[row][col] = prods[j];
}
}
}
//Display Table for(int j=0;j<n2;j++)
cout<<"\t"<<terms[j]; cout<<endl;
for(int i=0;i<n1;i++)
{
cout<<nonterms[i]<<"\t";
//Display Table for(int j=0;j<n2;j++)
{
cout<<pp_table[i][j]<<"\t";
}
cout<<endl;
}
//Parsing String char c;
do{ string ip;
deque<string> pp_stack; pp_stack.push_front("$"); pp_stack.push_front(prods[0].substr(0,1));
cout<<"Enter the string to be parsed : "; getline(cin,ip);
ip.push_back('$'); cout<<"Stack\tInput\tAction"<<endl; while(true)
{
for(int i=0;i<pp_stack.size();i++) cout<<pp_stack[i];
cout<<"\t"<<ip<<"\t";
int row1 = getPosition(nonterms,pp_stack.front(),n1); int row2 =
getPosition(terms,pp_stack.front(),n2);
int column = getPosition(terms,ip.substr(0,1),n2); if(row1 != -1 && column != -1)
{
string p = pp_table[row1][column]; if(p.empty())
{
cout<<endl<<"String cannot be Parsed."<<endl; break;
}
pp_stack.pop_front(); if(p[3] != '#')
{
for(int x=p.size()-1;x>2;x--)
{
pp_stack.push_front(p.substr(x,1));
}
}
cout<<p;
}
else
{
if(ip.substr(0,1) == pp_stack.front())
{
if(pp_stack.front() == "$")
{
cout<<endl<<"String Parsed."<<endl; break;
}
cout<<"Match "<<ip[0]; pp_stack.pop_front();
ip = ip.substr(1);
}
else
{
cout<<endl<<"String cannot be Parsed."<<endl; break;
}
}
cout<<endl;
}
cout<<"Continue?(Y/N) "; cin>>c;
cin.ignore();
}while(c=='y' || c=='Y'); return 0;
}
Prac 9
Code:

l=strlen(expe); exp1[0]='\0'; for(i=0;i<l;i++)


{
if(expe[i]=='+'||expe[i]=='-')
{
if(expe[i+2]=='/'||expe[i+2]=='*')
{
while (expe[count] != '\0')
{
count++;
}
j = count - 1;
for (i = 0; i < count; i++)
{
rev[i] = expe[j]; j--;
}
j=l-i-1; strncat(exp1,rev,j);
while (exp1[count] != '\0')
{
count++;
}
j = count - 1;
for (i = 0; i < count; i++)
{
rev1[i] = exp1[j]; j--;
}
printf("Three address code:\ntemp=%s\ntemp1=%c%ctemp\n",exp1,expe[j+1],expe[j]); break;
}
else
{
strncat(exp1,expe,i+2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,expe[i+2],expe[i+3]); break;
}
}
else if(expe[i]=='/'||expe[i]=='*')
{
strncat(exp1,expe,i+2);
printf("Three address code:\ntemp=%s\ntemp1=temp%c%c\n",exp1,expe[i+2],expe[i+3]); break;
}
}
}
Prac 10
Code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char a[20]; int x;
int i,j=0,k; i=0;
scanf("%s",a); if( strlen(a)==6)
{
i=i+3; if(islower(a[i]))
printf("lw $t%d, (%c)\n", j++,a[i]);
else
{
for(i=3;i < strlen(a);i++)
{
if(isdigit(a[i]))
{
x= a[i] - '0';
k=k*10 +x;
}
}
printf("li $t%d, %d\n", j,k);
}
i=i+2; if(islower( a[i]))
printf("lw $t%d, (%c)\n", j++,a[i]); else
{
for(i=3;i < strlen(a);i++)
{
if(isdigit(a[i]))
{
x= a[i] - '0';
k=k*10 +x;
}
}
printf("li $t%d, %d\n", j,k);
}
i=i-1;
if(a[i] == '+')
printf("add $t%d, $t%d, $t%d\n", j,j-1,j-2); else if( a[i] == '-')
printf("sub $t%d, $t%d, $t%d\n", j,j-2,j-1); else if( a[i] == '*')
printf("mul $t%d, $t%d, $t%d\n", j,j-2,j-1); else if( a[i] == '/')
printf("div $t%d, $t%d, $t%d\n", j,j-2,j-1);
}
else if(strlen(a)==4)
{
i=i+3;
if( islower(a[i]))
{
printf("lw $t%d, %c\n",j,a[i]);
printf("copy %c, $t%d\n", a[i-3],j);
}
else
printf("li $t%d, %c\n",j,a[i]);
}
j=j+1;
}

You might also like