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

Compiler Lab Program

This program implements a shift-reduce parser to determine if a string is accepted by a given grammar. The grammar has start symbol S and productions S->ABC, A->abA | ab, B->b | BC, C->c | Cc. The program takes the string as input, initializes stacks and arrays, then enters a loop where it shifts input symbols and reduces using the grammar rules until the string is either fully parsed or not. It returns whether the string is accepted by the given grammar.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Compiler Lab Program

This program implements a shift-reduce parser to determine if a string is accepted by a given grammar. The grammar has start symbol S and productions S->ABC, A->abA | ab, B->b | BC, C->c | Cc. The program takes the string as input, initializes stacks and arrays, then enters a loop where it shifts input symbols and reduces using the grammar rules until the string is either fully parsed or not. It returns whether the string is accepted by the given grammar.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Ex.

No: 1

DFA

Date:

Aim:
To Write a C program to search for a pattern for a given regular expressions.
Program:
#include <stdio.h>
#include <string.h>
#define MATCH printf("\nThe Text Matches The Regular Expression");
#define NOTMATCH printf("\nThe Text Doesn't match the Regular Expression");
char reg[20], text[20];
int main()
{
int i, rlen, tlen, f = 0;
char ans;
clrscr();
do {
printf("\nEnter the Regular Expression\n");
scanf(" %[^\n]s", reg);
for (rlen = 0; reg[rlen] != '\0';rlen++);
printf("\nEnter the text\n");
scanf(" %[^\n]s", text);
for (tlen = 0;text[tlen] != '\0' ; tlen++);
if (reg[0] == '*')
{
printf("\nInvalid regular expression");
}
/* If the regular expression starts with Alphabet */
if ((reg[0] >= 65 && reg[0] <= 90) || (reg[0] >= 97 && reg[0] <=122))
{
if (reg[0] == text [0])
{
switch (reg[1])
{
case '.' :
switch (reg[2])
{
case '*':
if (tlen != 1)
{

if (reg[3] == text[tlen-1])
{
MATCH;
}
else
{
NOTMATCH;
}
}
else
{
NOTMATCH;
}
break;
case '+':
if (text[1] != reg[3])
{
if (reg[3] == text[tlen - 1])
{
MATCH;
}
else
{
NOTMATCH;
}
}
break;
case '?':
if (text[1] == reg[3] || text[2] == reg[3])
{
if (text[1] == reg[3] || text[2] == reg[3])
{
MATCH;
}
else
{
NOTMATCH;
}
}
else
{
NOTMATCH;
}
break;

}
break;
case '*':
if (reg[rlen-1] == text[tlen-1])
{
for (i = 0;i <= tlen-2;i++)
{
if(text[i] == reg[0])
{
f = 1;
}
else
{
f = 0;
}
}
if ( f == 1)
{
MATCH;
}
else
{
NOTMATCH;
}
}
else
{
NOTMATCH;
}
break;
case '+' :
if (tlen <= 2)
{
NOTMATCH;
}
else if (reg[rlen-1] == text[tlen-1])
{
for (i = 0;i < tlen-2;i++)
{
if (text[i] == reg[0])
{
f = 1;
}
else

{
f = 0;
}
}
if (f == 1)
{
MATCH;
}
else
{
NOTMATCH;
}
}
break;
case '?':
if (reg[rlen -1] == text[tlen-1])
{
MATCH;
}
else
{
NOTMATCH;
}
break;
}
}
else
printf("Does not match");
}
/* If Regular Expression starts with '^'
else if (reg[0] == '^')
{
if (reg[1] == text[0])
{
MATCH;
}
else
{
NOTMATCH;
}

*/

}
/* If Regular Expression Ends with '$'
else if (reg[rlen-1] == '$')
{
if (reg[rlen-2] == text[rlen-1])
{
MATCH;
}
else
{
NOTMATCH;
}
}

*/

else
printf("Not Implemented");
printf("\nDo you want to continue?(Y/N)");
scanf(" %c", &ans);
} while (ans == 'Y' || ans == 'y');
return 0;
}
Output:

Ex.No:2a
Aim:

DFA

Date:

To Write a C program for DFA(Deterministic Finite Automata).


Program:
#include<stdio.h>
#include<conio.h>
int ninputs;
int check(char,int ); //function declaration
int dfa[10][10];
char c[10], string[10];
int main()
{
int nstates, nfinals;
int f[10];
int i,j,s=0,final=0;
printf("enter the number of states that your dfa consist of \n");
scanf("%d",&nstates);
printf("enter the number of input symbol that dfa have \n");
scanf("%d",&ninputs);
printf("\nenter input symbols\t");
for(i=0; i<ninputs; i++)
{
printf("\n\n %d input\t", i+1);
printf("%c",c[i]=getch());
}
printf("\n\nenter number of final states\t");
scanf("%d",&nfinals);
for(i=0;i<nfinals;i++)
{
printf("\n\nFinal state %d : q",i+1);
scanf("%d",&f[i]);
}
printf("-----------------------------------------------------------------------");
printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");
for(i=0; i<ninputs; i++)
{

for(j=0; j<nstates; j++)


{
printf("\n(q%d , %c ) = q",j,c[i]);
scanf("%d",&dfa[i][j]);
}
}
do
{
i=0;
printf("\n\nEnter Input String.. ");
scanf("%s",string);
while(string[i]!='\0')
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<nfinals ;i++)
if(f[i] ==s )
final=1;
if(final==1)
printf("\n valid string");
else
printf("invalid string");
getch();
printf("\nDo you want to continue.? \n(y/n) ");
}
while(getch()=='y');
getch();
}
int check(char b,int d)
{
int j;
for(j=0; j<ninputs; j++)
if(b==c[j])
return(dfa[d][j]);
return -1;
}

Output:

Ex.No:2b

NFA

Date:

Aim:
To Write a C program for NFA(NonDeterministic Finite Automata).
Program:
#include<stdio.h>
#include<conio.h>
int check(char* s,int state);
int a[10][10][10], fs[10],n;
int main()
{
int initial,i,j,k,l,fn;
char ch,str[25];
clrscr();
printf("Enter no.of states \n");
scanf("%d",&n);
printf("enter initial state \n");
scanf("%d",&initial);
printf("Enter how many final states");
scanf("%d",&fn);
printf("Enter final states \n");
for(i=0;i<fn;i++)
{
printf("\nq");
scanf("%d",&fs[i]);
if(fs[i]<0)
break;
}
for(i=0;i<=n;i++)
for(j=0;j<2;j++)
for(k=0;k<n;k++)
{
printf("(q%d,%d)=q",i,j);
scanf("%d",&a[i][j][k]);
if(a[i][j][k]<0)
break;
}do {printf("enter the string\n");
scanf("%s",str);
//r(l=0;l<strlen(str);l++)
if(check(str,initial))
printf("string is accepted");

else
printf("not accepted");
printf("\n\n input another (y/n)");
scanf("%s",&ch);
}
while(ch!='n');
return 0; //tch();
}
int check(char* s,int state)
{
int i,j;
for(i=0;i<n;i++)
{
if(*s=='\0')
{
if(fs[i]<0)
break;
if(fs[i]==state)
return 1;
}else{j=a[state][(int)(*s-'0')][i];
if(j<0)
break;
if(check(s+1,j))
return 1;
}}
return 0;
}
Output:

Ex.No:6

Lexical Analyzer

Date:

Aim:
To Design a Lexical analyzer for identifying different types of tokens using C language.
Program:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void removeduplicate();
void final();
int Isiden(char ch);
int Isop(char ch);
int Isdel(char ch);
int Iskey(char * str);
void removeduplicate();
char op[8]={'+','-','*','/','=','<','>','%'};
char del[8]={'}','{',';','(',')','[',']',','};
char *key[]={"int","void","main","char","float"};
//char *operato[]={"+","-","/","*","<",">","=","%","<=",">=","++"};
int idi=0,idj=0,k,opi=0,opj=0,deli=0,uqdi=0,uqidi=0,uqoperi=0,kdi=0,liti=0,ci=0;
int uqdeli[20],uqopi[20],uqideni[20],l=0,j;
char uqdel[20],uqiden[20][20],uqop[20][20],keyword[20][20];
char iden[20][20],oper[20][20],delem[20],litral[20][20],lit[20],constant[20][20];
void lexanalysis(char *str)
{
int i=0;
while(str[i]!='\0')
{
if(Isiden(str[i])) //for identifiers
{
while(Isiden(str[i]))

{
iden[idi][idj++]=str[i++];
}
iden[idi][idj]='\0';
idi++;idj=0;
}
else
if(str[i]=='"')
//for literals
{
lit[l++]=str[i];
for(j=i+1;str[j]!='"';j++)
{
lit[l++]=str[j];
}
lit[l++]=str[j];lit[l]='\0';
strcpy(litral[liti++],lit);
i=j+1;
}
else
if(Isop(str[i]))
// for operators
{
while(Isop(str[i]))
{
oper[opi][opj++]=str[i++];
}
oper[opi][opj]='\0';
opi++;opj=0;
}
else
if(Isdel(str[i])) //for delemeters
{
while(Isdel(str[i]))
{
delem[deli++]=str[i++];
}
}
else
{
i++;
}
}
removeduplicate();
final();

}
int Isiden(char ch)
{
if(isalpha(ch)||ch=='_'||isdigit(ch)||ch=='.')
return 1;
else
return 0;
}
int Isop(char ch)
{
int f=0,i;
for(i=0;i<8&&!f;i++)
{
if(ch==op[i])
f=1;
}
return f;
}
int Isdel(char ch)
{
int f=0,i;
for(i=0;i<8&&!f;i++)
{
if(ch==del[i])
f=1;
}
return f;
}
int Iskey(char * str)
{
int i,f=0;
for(i=0;i<5;i++)
{
if(!strcmp(key[i],str))
f=1;
}
return f;
}
void removeduplicate()

{
int i,j;
for(i=0;i<20;i++)
{
uqdeli[i]=0;
uqopi[i]=0;
uqideni[i]=0;
}
for(i=1;i<deli+1;i++) //removing duplicate delemeters
{
if(uqdeli[i-1]==0)
{
uqdel[uqdi++]=delem[i-1];
for(j=i;j<deli;j++)
{
if(delem[i-1]==delem[j])
uqdeli[j]=1;
}
}
}
for(i=1;i<idi+1;i++) //removing duplicate identifiers
{
if(uqideni[i-1]==0)
{
strcpy(uqiden[uqidi++],iden[i-1]);
for(j=i;j<idi;j++)
{
if(!strcmp(iden[i-1],iden[j]))
uqideni[j]=1;
}
}
}
for(i=1;i<opi+1;i++) //removing duplicate operators
{
if(uqopi[i-1]==0)
{
strcpy(uqop[uqoperi++],oper[i-1]);
for(j=i;j<opi;j++)
{
if(!strcmp(oper[i-1],oper[j]))
uqopi[j]=1;
}

}
}
}
void final()
{
int i=0;
idi=0;
for(i=0;i<uqidi;i++)
{
if(Iskey(uqiden[i]))
//identifying keywords
strcpy(keyword[kdi++],uqiden[i]);
else
if(isdigit(uqiden[i][0])) //identifying constants
strcpy(constant[ci++],uqiden[i]);
else
strcpy(iden[idi++],uqiden[i]);
}
// printing the outputs
printf("\n\tDelemeter are : \n");
for(i=0;i<uqdi;i++)
printf("\t%c\n",uqdel[i]);
printf("\n\tOperators are : \n");
for(i=0;i<uqoperi;i++)
{
printf("\t");
puts(uqop[i]);
}
printf("\n\tIdentifiers are : \n");
for(i=0;i<idi;i++)
{
printf("\t");
puts(iden[i]);
}
printf("\n\tKeywords are : \n");
for(i=0;i<kdi;i++)
{
printf("\t");
puts(keyword[i]);

}
printf("\n\tConstants are :\n");
for(i=0;i<ci;i++)
{
printf("\t");
puts(constant[i]);
}
printf("\n\tLiterals are :\n");
for(i=0;i<liti;i++)
{
printf("\t");
puts(litral[i]);
}
}
void main()
{
char str[50];
//clrscr();
printf("\nEnter the string : ");
scanf("%[^\n]c",str);
lexanalysis(str);
//getch();
}
Output:

Ex.No:7

Simple Desktop Calculator

Date:

Aim:
To simulate a simple desktop calculator using any lexical analyzer generator tool (LEX or
FLEX).

Program:
// Lex file: calculator.l
DIGIT [0-9]+\.?|[0-9]*\.[0-9]+
%%
[]
{DIGIT} {yylval=atof(yytext);return NUM;}
\n|.
{return yytext[0];}

// Yacc file: caculator.y


%{
#include<ctype.h>
#include<stdio.h>
#define YYSTYPE double
%}
%token NUM
%left '+' '-'
%left '*' '/'
%right UMINUS
%%
S

: S E '\n' { printf("Answer: %g \nEnter:\n", $2); }


| S '\n'
|
| error '\n' { yyerror("Error: Enter once more...\n" );yyerrok; }
;
: E '+' E { $$ = $1 + $3; }
| E'-'E { $$=$1-$3; }
| E'*'E { $$=$1*$3; }
| E'/'E { $$=$1/$3; }

| '('E')' { $$=$2; }
| '-'E %prec UMINUS { $$= -$2; }
| NUM
;
%%
#include "lex.yy.c"
int main()
{
printf("Enter the expression: ");
yyparse();
}
Output:

Ex.No:9

Shift Reduce Parser

Date:

Aim:
To Design a shift reduced parser which accepts a string and tells whether the string is accepted by
below grammar or not.
Consider the following grammar:
S --> ABC
A--> abA | ab
B--> b | BC
C--> c | cC
Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char left[10],stack[15],input[20],start,a,store[10],right[10][10];
int top=0,ippointer,n,l,i,loop,x,j,k,equal=0;
for(i=0;i<=15;i++)
stack[i]=NULL;
for(i=0;i<20;i++)
input[i]=NULL;
for(i=0;i<10;i++)
{
store[i]=NULL;
left[i]=NULL;
for(j=0;j<10;j++)
right[i][j]=NULL;
}
stack[0]='$';
top=0;
ippointer=0;
clrscr();
printf("\n ENTER THE NUMBER OF PRODUCTION \n");
scanf("%d",&n);
printf("\n ENTER THE GRAMMAR \n");
for(i=0;i<n;i++)
{
scanf("\n %c %s",&left[i],&right[i]);
}
start=left[0];

printf("\n ENTER THE INPUT SYMBOL\n");


scanf("%s",input);
l=strlen(input);
l=strlen(input);
input[l]='$';
while(1)
{
a=input[ippointer];
if(a=='$'&&stack[top]==start&&top==1)
{
printf("\n\n GIVEN EXPRESSION IS ACCEPTED\n\n");
break;
}
else
{
if(a=='$'&&top>1)
{
printf("incorrect input");
break;
}
else
{
loop=1;
if(a!='$')
{
stack[++top]=a;
printf("\n stack:%s",stack);
}
a:
for(i=0;i<n;i++)
{
equal=1;
x=strlen(right[i]);
for(j=x-1,k=top;j>=0;j--,k--)
{
if(right[i][j]!=stack[k])
{
equal=0;
break;
}
}
if(equal==1)break;
}
if(equal==1)

{
for(j=0;j<strlen(right[i]);j++)
{
stack[top]=NULL;
printf("\n stack:%s",stack);
top=top-1;
}
top=top+1;
stack[top]=left[i];
printf("\n stack:%s",stack);
if(loop==1)
{
loop=0;
goto a;
}
}
}
}
if(a!='$')
ippointer=ippointer+1;
}
getch();
}
Ouput:

Ex.No:11(a)

Eliminating Left Recursion

Date:

Aim:
To write a Program to eliminate left recursion from a given CFG

Program:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct production
{
char l;
char r[10];
int rear;
};
struct production prod[20],pr_new[20];
int p=0,b=0,d,f,q,n,flag=0;
char terminal[20],nonterm[20],alpha[10];
char x,epsilon='^';
void main()
{
clrscr();
cout<<"Enter the number of terminals: ";
cin>>d;
cout<<"Enter the terminal symbols for your production: ";
for(int k=0;k<d;k++)
{
cin>>terminal[k];
}
cout<<"\nEnter the number of non-terminals: ";
cin>>f;
cout<<"Enter the non-terminal symbols for your production: ";
for(k=0;k<f;k++)
{
cin>>nonterm[k];
}
cout<<"\nEnter the number of Special characters(except nonterminals): ";
cin>>q;
cout<<"Enter the special characters for your production: ";

for(k=0;k<q;k++)
{
cin>>alpha[k];
}
cout<<"\nEnter the number of productions: ";
cin>>n;
for(k=0;k<=n-1;k++)
{
cout<<"Enter the "<< k+1<<" production: ";
cin>>prod[k].l;
cout<<"->";
cin>>prod[k].r;
prod[k].rear=strlen(prod[k].r);
}
for(int m=0;m<f;m++)
{
x=nonterm[m];
for(int j=0;j<n;j++)
{
if((prod[j].l==x)&&(prod[j].r[0]==prod[j].l))
flag=1;
}
for(int i=0;i<n;i++)
{
if((prod[i].l==x)&&(prod[i].r[0]!=x)&&(flag==1))
{
pr_new[b].l=x;
for(int c=0;c<prod[i].rear;c++)
pr_new[b].r[c]=prod[i].r[c];
pr_new[b++].r[c]=alpha[p];
}
else if((prod[i].l==x)&&(prod[i].r[0]==x)&&(flag==1))
{
pr_new[b].l=alpha[p];
for(int a=0;a<=prod[i].rear-2;a++)
pr_new[b].r[a]=prod[i].r[a+1];
pr_new[b++].r[a]=alpha[p];
pr_new[b].l=alpha[p];
pr_new[b++].r[0]=epsilon;
}
else if((prod[i].l==x)&&(prod[i].r[0]!=x)&&(flag==0))
{
pr_new[b].l=prod[i].l;
strcpy(pr_new[b].r,prod[i].r);
b++;
}
}

flag=0;
p++;
}
cout<<"\n\n*******************************************";
cout<<"\n
AFTER REMOVING LEFT RECURSION
";
cout<<"\n*******************************************"<<endl;
for(int s=0;s<=b-1;s++)
{
cout<<"Production "<<s+1<<" is: ";
cout<<pr_new[s].l;
cout<<"->";
cout<<pr_new[s].r;
cout<<endl;
}
getche();
}
Output:

Ex.No:11(b)

Left factoring

Date:

Aim:
To write a Program to to generate left factoring from a given CFG

Program:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
//Structure Declaration
struct production
{
char lf;
char rt[10];
int prod_rear;
int fl;
};
struct production prodn[20],prodn_new[20];

//Creation of object

//Variables Declaration
int b=-1,d,f,q,n,m=0,c=0;
char terminal[20],nonterm[20],alpha[10],extra[10];
char epsilon='^';
//Beginning of Main Program
void main()
{
clrscr();
//Input of Special characters
cout<<"\nEnter the number of Special characters(except nonterminals): ";
cin>>q;
cout<<"Enter the special characters for your production: ";
for(int cnt=0;cnt<q;cnt++)
{
cin>>alpha[cnt];
}
//Input of Productions
cout<<"\nEnter the number of productions: ";
cin>>n;

for(cnt=0;cnt<=n-1;cnt++)
{
cout<<"Enter the "<< cnt+1<<" production: ";
cin>>prodn[cnt].lf;
cout<<"->";
cin>>prodn[cnt].rt;
prodn[cnt].prod_rear=strlen(prodn[cnt].rt);
prodn[cnt].fl=0;
}
//Condition for left factoring
for(int cnt1=0;cnt1<n;cnt1++)
{
for(int cnt2=cnt1+1;cnt2<n;cnt2++)
{
if(prodn[cnt1].lf==prodn[cnt2].lf)
{
cnt=0;
int p=-1;
while((prodn[cnt1].rt[cnt]!='\0')&&(prodn[cnt2].rt[cnt]!='\0'))
{
if(prodn[cnt1].rt[cnt]==prodn[cnt2].rt[cnt])
{
extra[++p]=prodn[cnt1].rt[cnt];
prodn[cnt1].fl=1;
prodn[cnt2].fl=1;
}
else
{
if(p==-1)
break;
else
{
int h=0,u=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
prodn_new[++b].lf=alpha[c];
for(g=cnt;g<=prodn[cnt1].prod_rear;g++)
prodn_new[b].rt[u++]=prodn[cnt1].rt[g];
m=1;
break;
}
}
cnt++;

}
if((prodn[cnt1].rt[cnt]==0)&&(m==0))
{
int h=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
prodn_new[b].rt[0]=epsilon;
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
}
if((prodn[cnt2].rt[cnt]==0)&&(m==0))
{
int h=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
prodn_new[b].rt[0]=epsilon;
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt1].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt1].rt[g];
}
c++;
m=0;
}
}
}
//Display of Output
cout<<"\n\n********************************";
cout<<"\n
AFTER LEFT FACTORING
";
cout<<"\n********************************";
cout<<endl;
for(int cnt3=0;cnt3<=b;cnt3++)
{
cout<<"Production "<<cnt3+1<<" is: ";
cout<<prodn_new[cnt3].lf;
cout<<"->";
cout<<prodn_new[cnt3].rt;
cout<<endl<<endl;
}
for(int cnt4=0;cnt4<n;cnt4++)
{
if(prodn[cnt4].fl==0)

{
cout<<"Production "<<cnt3++<<" is: ";
cout<<prodn[cnt4].lf;
cout<<"->";
cout<<prodn[cnt4].rt;
cout<<endl<<endl;
}
}
getche();
}

Output:

Ex.No:12

Infix to Postfix Using YACC

Date:

Aim:
To write a YACC program that reads the input expression and convert it to post fix expression.
Program:

%{
#include<stdio.h>
#include<ctype.h>
%}
%token num
%left '+''-'
%left '*' '/'
%%
s:e'\n'{}
e:e'+'e{printf("+");}
|e'-'e{printf("-");}
|e'/'e{printf("/");}
|e'*'e{printf("*");}
|num1{printf("%d",$1);}
num1:num1 num {$$=$1*10+$2;}
|num
;
%%
yylex()
{
int c;
c=getchar();
if(isdigit(c))
{ yylval=c-'0';
return num;
}return c;
}
int main()
{
printf(Enter the expression:);
yyparse();

return 1;
}
int yyerror()
{
return 1;
}
int yywrap()
{
return 1;
}
Output:

Ex.No:14

Quadruple Three Address Intermediate Date:


Code

Aim:
To write a YACC program that reads the C statements and converts them into quadruple three
address intermediate code
Program:
/* lex program */
%{
#include "y.tab.h"
extern char yyval;
%}
number [0-9]+
letter [a-zA-Z]+
%%
{number} {yylval.sym=(char)yytext[0];return number;}
{letter} {yylval.sym=(char)yytext[0]; return letter; }
\n {return 0;}
. {return yytext[0];}
%%
/*yaac program */
%{
#include<stdio.h>
#include<string.h>
int nIndex=0;
struct Intercode
{
char operand1;
char operand2;
char opera;
};
%}
%union
{
char sym;
}
%token <sym> letter number
%type <sym> expr
%left '-' '+'
%right '*' '/'
%%
Statement: letter '=' expr ';' { addtotable((char)$1,(char)$3,'=' ); }
| expr ;
;

expr: expr '+' expr { $$=addtotable((char)$1,(char)$3,'+');}


| expr '-' expr { $$=addtotable((char)$1,(char)$3,'-');}
| expr '*' expr { $$=addtotable((char)$1,(char)$3, '*');}
| expr '/' expr { $$=addtotable((char)$1,(char)$3,'/');}
| '(' expr ')' { $$= (char)$2;}
| number { $$= (char)$1;}
| letter { $$= (char)$1;}
%%
yyerror(char *s)
{
printf("%s",s);
exit (0);
}
struct Intercode code[20];
char addtotable(char operand1, char operand2,char opera)
{
char temp = 'A';
code[nIndex].operand1 = operand1;
code[nIndex].operand2 = operand2;
code[nIndex].opera = opera;
nIndex++;
temp++;
return temp;
}
threeaddresscode()
{
int nCnt=0;
char temp='A';
printf("\n\n\t three addrtess codes\n\n");
temp++;
while(nCnt<nIndex)
{
printf("%c:=\t",temp);
if (isalpha(code[nCnt].operand1))
printf("%c\t", code[nCnt].operand1);
else
printf("%c\t",temp);
if (isalpha(code[nCnt].operand2))
printf("%c\t", code[nCnt].operand2);
else
printf("%c\t",temp);
printf("\n");
nCnt++;
temp++;
}
}
void quadruples()
{
int nCnt=0;

char temp = 'A';


temp++;
printf("\n\n\t Quardruples \n");
printf("\n ID OPERATOR OPERAND1 OPERAND2 RESULT\n");
while(nCnt<nIndex)
{
printf("\n (%d) \t %c \t",nCnt,code[nCnt].opera);
if(isalpha(code[nCnt].operand1))
printf("%c\t", code[nCnt].operand1);
else
printf("%c\t",temp);
if(isalpha(code[nCnt].operand2))
printf("%c\t", code[nCnt].operand2);
else
printf("%c\t",temp);
printf("%c\t",temp);
printf("\n");
nCnt++;
temp++;
}
}
main()
{
printf("enter expression");
yyparse();
threeaddresscode();
quadruples();
}
yywrap()
{
return 1;
}
Output:

You might also like