LEXICAL ANALYZER IN C
PROGRAM:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0;
char keys[32][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
main()
{
char ch,str[25],seps[15]=" \t\n,;(){}[]#\"<>",oper[]="!%^&*-+=~|.<>/?";
int j;
char fname[50];
FILE *f1;
//clrscr();
printf("enter file path (drive:\\fold\\filename)\n");
scanf("%s",fname);
f1 = fopen(fname,"r");
//f1 = fopen("Input","r");
if(f1==NULL)
{
printf("file not found");
exit(0);
}
while((ch=fgetc(f1))!=EOF)
{
for(j=0;j<=14;j++)
{
if(ch==oper[j])
{
printf("%c is an operator\n",ch);
op++;
str[i]='\0';
keyw(str);
}
}
for(j=0;j<=14;j++)
{
if(i==-1)
break;
if(ch==seps[j])
{
if(ch=='#')
{
while(ch!='>')
{
printf("%c",ch);
ch=fgetc(f1);
}
printf("%c is a header file\n",ch);
i=-1;
break;
}
if(ch=='"')
{
do
{
ch=fgetc(f1);
printf("%c",ch);
}while(ch!='"');
printf("\b is an argument\n");
i=-1;
break;
}
str[i]='\0';
keyw(str);
}
}
if(i!=-1)
{
str[i]=ch;
i++;
}
else
i=0;
}
printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers:
%d\n",kw,id,op,num);
//getch();
}
void keyw(char *p)
{
int k,flag=0;
for(k=0;k<=31;k++)
{
if(strcmp(keys[k],p)==0)
{
printf("%s is a keyword\n",p);
kw++;
flag=1;
break;
}
}
if(flag==0)
{
if(isdigit(p[0]))
{
printf("%s is a number\n",p);
num++;
}
else
{
//if(p[0]!=13&&p[0]!=10)
if(p[0]!='\0')
{
printf("%s is an identifier\n",p);
id++;
}
}
}
i=-1;
}
OUTPUT:
void is a Keyword
main is an Identifier
int is a Keyword
a is an Identifier
b is an Identifier
c is an Identifier
printf is an Identifier
Enter is an Arsumer
While is a Keyword
( is an Operator
a is an Identifier
b is an Identifier
= is an Operator
a is an Identifier
+ is an Operator
b is an Identifier
c is an Identifier
= is an Operator
c is an Identifier
a is an Identifier
Keywords=3
Identifiers=12
Operators=4
CONTRUCTING NFA FROM REGULAR EXPRESSION
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char m[20],t[10][10];
intn,i,j,r=0,c=0;
clrscr();
printf("\n\t\t\t\tSIMULATION OF NFA");
printf("\n\t\t\t\t*****************");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
t[i][j]=' ';
}
}
printf("\n\nEnter a regular expression:");
scanf("%s",m);
n=strlen(m);
for(i=0;i<n;i++)
{
switch(m[i])
{
case '|' :
{
t[r][r+1]='E';
t[r+1][r+2]=m[i-1];
t[r+2][r+5]='E';
t[r][r+3]='E';
t[r+4][r+5]='E';
t[r+3][r+4]=m[i+1];
r=r+5;
break;
}
case '*':
{
t[r-1][r]='E';
t[r][r+1]='E';
t[r][r+3]='E';
t[r+1][r+2]=m[i-1];
t[r+2][r+1]='E';
t[r+2][r+3]='E';
r=r+3;
break;
}
case '+':
{
t[r][r+1]=m[i-1];
t[r+1][r]='E';
r=r+1;
break;
}
default:
{
if(c==0)
{
if((isalpha(m[i]))&&(isalpha(m[i+1])))
{
t[r][r+1]=m[i];
t[r+1][r+2]=m[i+1];
r=r+2;
c=1;
}
c=1;
}
else if(c==1)
{
if(isalpha(m[i+1]))
{
t[r][r+1]=m[i+1];
r=r+1;
c=2;
}
}
else
{
if(isalpha(m[i+1]))
{
t[r][r+1]=m[i+1];
r=r+1;
c=3;
}
}
}
break;
}
}
printf("\n");
for(j=0;j<=r;j++)
printf(" %d",j);
printf("\n___________________________________\n");
printf("\n");
for(i=0;i<=r;i++)
{
for(j=0;j<=r;j++)
{
printf(" %c",t[i][j]);
}
printf(" | %d",i);
printf("\n");
}
printf("\nStart state: 0\nFinal state: %d",i-1);
getch();
}
OUTPUT:
Enter a regular Expression: a|b
SIMULATION OF NFA
************************
Enter a regular expression:a|b
0 1 2 3 4 5
___________________________________
E E |0
a |1
E |2
b |3
E |4
|5
Start state: 0
Final state: 5
CONTRUCTING DFA FROM REGULAR EXPRESSION
PROGRAM:
#include<stdio.h>
#include<conio.h>
typedefstruct
{
int input[2];
}
state;
voidreadtt(state*states,intnstates,intninps)
{
intstate,inp;
printf("enter the state transition:\n");
for(state=0;state<nstate;state++)
for(inp=0;inp<ninps;inp++)
{
printf("(state:%d,%d):",state,inp);
scanf("%d",&states[state].input[inp]);
}
}
voiddfasmul(state*states,intnstates,char*inpstr)
{
inti,start;
start=0;
for(i=0;inpstr[i]!='\0';i++)
{
start=state[start].input[inpstr[i]-'0'];
}
if(start==-1)
{
printf("DFA halted");
return;
}
else
if(start==nstates-1)
printf("string is accepted");
else
printf("string is not accepted");
}
void main()
{
state states[100];
intnstates,ninps;
charinpstr[20];
clrscr();
printf("\n\t\t\tSIMULATION OF DFA");
printf("\n\t\t\t*****************");
printf("\n enter the no of states:");
scanf("%d",&nstates);
printf("enter the no of inputs:");
scanf("%d",&ninps);
readtt("\n initial state:0\nfinal state:%d",nstates-1);
printf("\n\n enter the string:");
scanf("%s,inpstr);
dfasimul(states,nstates,inpstr);
getch();
}
OUTPUT:
SIMULATION OF DFA
************************
Enter the no. of states: 3
Enter the no. of inputs: 2
Enter the state transition:
(state : 0,0) :0
(state : 0,1) :1
(state : 1,0) :2
(state : 1,1) :3
(state : 2,0) :0
(state : 2,1) :3
Initial state :0
Final state :2
Enter the string :2
String is accepted
LEFT FACTORING
PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char gram[20],part1[20],part2[20],modifiedGram[20],newGram[20],tempGram[20];
int i,j=0,k=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);
return 0;
}
OUTPUT:
Enter Production : A->iEtSx|iEtSeS
A->iEtSX
X->x|es
TOP DOWN PARSING
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void error()
{
cout<<"not accepted";
getch();
exit(0);
}
void main()
{
clrscr();
cout<<"Enter the string in single letter such as a+a*a \n\n";
char string[10],stack[10];
inti=0,k,top=0;
cout<<"Enter the Expression :";
cin>>string;
int j=strlen(string);
string[j]='$';
string[++j]='\0';
cout<<string<<endl;
stack[top]='E';
cout<<"$"<<stack<<"\t";
for(k=i;k<j;k++)
{
cout<<string[k];
}
cout<<endl;
top++;
while(top!=0)
{
if(stack[top-1]==string[i])
{
i++;
top--;
stack[top]='\0';
}
else if(stack[top-1]=='E')
{
if(string[i]=='a')
{
top--;
stack[top]='D';
stack[++top]='T';
top++;
stack[top]='\0';
}
else if(string[i]=='(')
{
top--;
stack[top]='D';
stack[++top]='T';
top++;
stack[top]='\0';
}
else error();
}
else if(stack[top-1]=='D')
{
if(string[i]=='+')
{
top--;
stack[top]='D';
stack[++top]='T';
stack[++top]='+';
top++;
stack[top]='\0';
}
else if(string[i]==')')
{
top--;
stack[top]='\0';
}
else if(string[i]=='$')
{
top--;
stack[top]='\0';
}
else error();
}
else if(stack[top-1]=='T')
{
if(string[i]=='a')
{
top--;
stack[top]='s';
stack[++top]='F';
top++;
stack[top]='\0';
}
else if(string[i]=='(')
{
top--;
stack[top]='s';
stack[++top]='F';
top++;
stack[top]='\0';
}
else error();
}
else if(stack[top-1]=='s')
{
if(string[i]=='+')
{
top--;
stack[top]='\0';
}
else if(string[i]=='*')
{
top--;
stack[top]='s';
stack[++top]='F';
stack[++top]='*';
top++;
stack[top]='\0';
}
else if(string[i]==')')
{
top--;
stack[top]='\0';
}
else if(string[i]=='$')
{
top--;
stack[top]='\0';
}
else error;
}
else if(stack[top-1]=='F')
{
if(string[i]=='a')
{
top--;
stack[top]='a';
top++;
stack[top]='\0';
}
else if(string[i]=='(')
{
top--;
stack[top]=')';
stack[++top]='E';
stack[++top]='(';
top++;
stack[top]='\0';
}
else error();
}
else error();
cout<<"$"<<stack<<"\t";
for(k=i;k<j;k++)
{
cout<<string[k];
}
cout<<endl;
}
cout<<"accepted";
getch();
}
OUTPUT:
Enter the string in single letter such as a+a*a
enter the expression : a+a*a
a+a*a$
$E a+a*a$
$DT a+a*a$
$DsF a+a*a$
$Dsa a+a*a$
$Ds +a*a$
$D +a*a$
$DT+ +a*a$
$DT a*a$
$DsF a*a$
$Dsa a*a$
$Ds *a$
$DsF* *a$
$DsF a$
$Dsa a$
$Ds $
$D $
$ $
accepted
SHIFT REDUCE PARSING
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str[25],stk[25];
int i,j,t=0,l,r;
clrscr();
printf("Enter the String : ");
scanf("%s",&str);
l=strlen(str);
str[l]='$';
stk[t]='$';
printf("Stack\t\tString\t\tAction\n-----------------------------------\n ");
for(i=0;i<l;i++)
{
if(str[i]=='i')
{
t++;
stk[t]=str[i];
stk[t+1]=str[i+1];
for(j=0;j<=t+1;j++)
printf("%c",stk[j]);
printf("\t\t ");
for(j=i+2;j<=l;j++)
printf("%c",str[j]);
printf("\t\tShift");
printf("\n ");
stk[t]='E';
i++;
}
else
{
t++;
stk[t]=str[i];
}
for(j=0;j<=t;j++)
printf("%c",stk[j]);
printf("\t\t ");
for(j=i+1;j<=l;j++)
printf("%c",str[j]);
if(stk[t]=='+' || stk[t]=='*')
printf("\t\tShift");
else
printf("\t\tReduce");
printf("\n ");
}
while(t>1)
{
if(stk[t]=='E' && (stk[t-1]=='+' || stk[t-1]=='*') && stk[t-2]=='E')
{
t-=2;
for(j=0;j<=t;j++)
printf("%c",stk[j]);
printf("\t\t");
printf(" %c",str[l]);
printf("\t\tReduce\n ");
}
Else
t-=2;
}
if(t==1 && stk[t]!='+' && stk[t]!='*')
printf("\nThe Given String is Valid\n\n");
else
printf("\nThe Given String is Invalid\n\n");
getch();
}
OUTPUT:
Enter the String :id+id*id
Stack String Action
-----------------------------------------------
$id +id*id$ Shift
$E +id*id$ Reduce
$E+ id*id$ Shift
$E+id *id$ Shift
$E+E *id$ Reduce
$E+E* id$ Shift
$E+E*id $ Shift
$E+E*E $ Reduce
$E+E $ Reduce
$E $ Reduce
The Given String is Valid
OPERATOR PRECEDENCE PARSING
PROGRAM:
#include<stdio.h>
#include<conio.h>
int find(char a)
{
switch(a)
{
case 'a':
return 0;
case '+':
return 1;
case '*':
return 2;
case '$':
return 3;
}
}
void display(char a[],int top1,char b[],int top2)
{
inti;
for(i=0;i<=top1;i++)
printf("%c",a[i]);
printf("\t");
for(i=top2;i<strlen(b);i++)
printf("%c",b[i]);
printf("\n");
}
int main()
{
char table[][4]= {' ','>','>','>','<','<','<','>','<','>','<','>','<','<','<',' '};
char input[10];
char stack[10]={'$'};
inttop_stack=0,top_input=0,i,j;
clrscr();
printf("operator precedence parsing for E->E+E/E*E/a\n");
printf("enter the string\n");
scanf("%s",input);
strcat(input,"$");
printf("stack\tinput\n");
display(stack,top_stack,input,top_input);
while(1)
{
if((stack[top_stack]=='$')&&(input[top_input]=='$'))
{
printf("string accepted");
break;
}
if(table[find(stack[top_stack])][find(input[top_input])]==' ')
{
printf("parse error");
getch();
exit(0);
}
if(table[find(stack[top_stack])][find(input[top_input])]=='<')
{
stack[++top_stack]='<';
stack[++top_stack]=input[top_input];
top_input++;
display(stack,top_stack,input,top_input);
continue;
}
if(table[find(stack[top_stack])][find(input[top_input])]=='>')
{
stack[++top_stack]='>';
display(stack,top_stack,input,top_input);
top_stack-=3;
display(stack,top_stack,input,top_input);
}
}
getch();
}
OUTPUT:
Operator precedence parsing for E->E+E/E*E/a
Enter the string
a+a*a
stack input
$ a+a*a$
$<a +a*a$
$<a> +a*a$
$ +a*a$
$<+ a*a$
$<+<a *a$
$<+<a> *a$
$<+ *a$
$<+<* a$
$<+<*<a $
$<+<*<a> $
$<+<* $
$<+<*> $
$<+ $
$<+> $
$ $
string accepted
SIMPLE LR PARSING
PROGRAM:
#include<stdio.h>
#include<string.h>
int i,j,k,m,n=0,o,p,ns=0,tn=0,rr=0,ch=0;
char
read[15][10],gl[15],gr[15][10],temp,templ[15],tempr[15][10],*ptr,temp2[5],dfa[15][1
5];
struct states
{
char lhs[15],rhs[15][10];
int n;
}I[15];
int compstruct(struct states s1,struct states s2)
{
int t;
if(s1.n!=s2.n)
return 0;
if( strcmp(s1.lhs,s2.lhs)!=0 )
return 0;
for(t=0;t<s1.n;t++)
if( strcmp(s1.rhs[t],s2.rhs[t])!=0 )
return 0;
return 1;
}
void moreprod()
{
int r,s,t,l1=0,rr1=0;
char *ptr1,read1[15][10];
for(r=0;r<I[ns].n;r++)
{
ptr1=strchr(I[ns].rhs[l1],'.');
t=ptr1-I[ns].rhs[l1];
if( t+1==strlen(I[ns].rhs[l1]) )
{
l1++;
continue;
}
temp=I[ns].rhs[l1][t+1];
l1++;
for(s=0;s<rr1;s++)
if( temp==read1[s][0] )
break;
if(s==rr1)
{
read1[rr1][0]=temp;
rr1++;
}
else
continue;
for(s=0;s<n;s++)
{
if(gl[s]==temp)
{
I[ns].rhs[I[ns].n][0]='.';
I[ns].rhs[I[ns].n][1]=NULL;
strcat(I[ns].rhs[I[ns].n],gr[s]);
I[ns].lhs[I[ns].n]=gl[s];
I[ns].lhs[I[ns].n+1]=NULL;
I[ns].n++;
}
}
}
}
void canonical(int l)
{
int t1;
char read1[15][10],rr1=0,*ptr1;
for(i=0;i<I[l].n;i++)
{
temp2[0]='.';
ptr1=strchr(I[l].rhs[i],'.');
t1=ptr1-I[l].rhs[i];
if( t1+1==strlen(I[l].rhs[i]) )
continue;
temp2[1]=I[l].rhs[i][t1+1];
temp2[2]=NULL;
for(j=0;j<rr1;j++)
if( strcmp(temp2,read1[j])==0 )
break;
if(j==rr1)
{
strcpy(read1[rr1],temp2);
read1[rr1][2]=NULL;
rr1++;
}
else
continue;
for(j=0;j<I[0].n;j++)
{
ptr=strstr(I[l].rhs[j],temp2);
if( ptr )
{
templ[tn]=I[l].lhs[j];
templ[tn+1]=NULL;
strcpy(tempr[tn],I[l].rhs[j]);
tn++;
}
}
for(j=0;j<tn;j++)
{
ptr=strchr(tempr[j],'.');
p=ptr-tempr[j];
tempr[j][p]=tempr[j][p+1];
tempr[j][p+1]='.';
I[ns].lhs[I[ns].n]=templ[j];
I[ns].lhs[I[ns].n+1]=NULL;
strcpy(I[ns].rhs[I[ns].n],tempr[j]);
I[ns].n++;
}
moreprod();
for(j=0;j<ns;j++)
{
//if ( memcmp(&I[ns],&I[j],sizeof(struct states))==1 )
if( compstruct(I[ns],I[j])==1 )
{
I[ns].lhs[0]=NULL;
for(k=0;k<I[ns].n;k++)
I[ns].rhs[k][0]=NULL;
I[ns].n=0;
dfa[l][j]=temp2[1];
break;
}
}
if(j<ns)
{
tn=0;
for(j=0;j<15;j++)
{
templ[j]=NULL;
tempr[j][0]=NULL;
}
continue;
}
dfa[l][j]=temp2[1];
printf("\n\nI%d :",ns);
for(j=0;j<I[ns].n;j++)
printf("\n\t%c -> %s",I[ns].lhs[j],I[ns].rhs[j]);
getch();
ns++;
tn=0;
for(j=0;j<15;j++)
{
templ[j]=NULL;
tempr[j][0]=NULL;
}
}
}
void main()
{
FILE *f;
int l;
clrscr();
for(i=0;i<15;i++)
{
I[i].n=0;
I[i].lhs[0]=NULL;
I[i].rhs[0][0]=NULL;
dfa[i][0]=NULL;
}
f=fopen("lrinput.txt","r");
while(!feof(f))
{
fscanf(f,"%c",&gl[n]);
fscanf(f,"%s\n",gr[n]);
n++;
}
printf("THE GRAMMAR IS AS FOLLOWS\n");
for(i=0;i<n;i++)
printf("\t\t\t\t%c -> %s\n",gl[i],gr[i]);
I[0].lhs[0]='Z';
strcpy(I[0].rhs[0],".S");
I[0].n++;
l=0;
for(i=0;i<n;i++)
{
temp=I[0].rhs[l][1];
l++;
for(j=0;j<rr;j++)
if( temp==read[j][0] )
break;
if(j==rr)
{
read[rr][0]=temp;
rr++;
}
else
continue;
for(j=0;j<n;j++)
{
if(gl[j]==temp)
{
I[0].rhs[I[0].n][0]='.';
strcat(I[0].rhs[I[0].n],gr[j]);
I[0].lhs[I[0].n]=gl[j];
I[0].n++;
}
}
}
ns++;
printf("\nI%d :\n",ns-1);
for(i=0;i<I[0].n;i++)
printf("\t%c -> %s\n",I[0].lhs[i],I[0].rhs[i]);
for(l=0;l<ns;l++)
canonical(l);
printf("\n\n\t\tPRESS ANY KEY FOR SLR TABLE");
getch();
clrscr();
printf("\t\t\t SLR TABLE IS AS FOLLOWS\n\n\n");
for(i=0;i<ns;i++)
{
printf("I%d : ",i);
for(j=0;j<ns;j++)
if(dfa[i][j]!='\0')
printf("'%c'->I%d | ",dfa[i][j],j);
printf("\n\n\n");
}
printf("\n\n\n\t\tPRESS ANY KEY TO EXIT");
getch();
}
OUTPUT:
Input File: lrinput.txt
S S+T
ST
T T*F
TF
F (S)
Fx
Output:
I 0:
Z→ .S
S→ .S+T
S→ .T
T→ .T*F
T→ .F
F→ .(S)
F→ .x
I 1:
Z→ S.
S→ S.+T
I 2:
S→ T.
T→T.*F
I 3:
T→ F.
I 4:
F→ (.S)
S→ .S+T
S→ .T
T→ .T*F
T→ .F
F→ .(S)
F→ .x
I 5:
F→ x.
I 6:
S→ S+.T
T→ .T*F
T→ .F
F→ .(S)
F→ .x
I 7:
T→ T*.F
F→ .(S)
F→ .x
I 8:
F→ (S.)
S→ S.+T
I 9:
S→ S+T.
T→ T.*F
I 10:
T→ T*F.
I 11:
F→ (S).
PRESS ANY KEY FOR SLR TABLE
SLR TABLE
I 1:
I 2:
I 3:
I 4:
I 5:
I 6: ‘F’→I3 | ‘(‘→I4 | ‘x’→I5 | ‘T’→I9 |
I 7: ‘(‘→I4 | ‘x’→I5 | ‘F’→I10 |
I 8: ‘+’→I6 | ‘)’→I11 |
I 9: ‘*’→I7 |
I 10:
I 11:
PRESS ANY KEY TO EXIT
INTERMEDIATE CODE GENERATION
PROGRAM:
#include"stdio.h"
#include"conio.h"
#include"string.h"
int i=1,j=0,no=0,tmpch=90;
char str[100],left[15],right[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
struct exp
{
int pos;
char op;
}k[15];
void main()
{
clrscr();
printf("\t\tINTERMEDIATE CODE GENERATION\n\n");
printf("Enter the Expression :");
scanf("%s",str);
printf("The intermediate code:\t\tExpression\n");
findopr();
explore();
getch();
}
void findopr()
{
for(i=0;str[i]!='\0';i++)
if(str[i]==':')
{
k[j].pos=i;
k[j++].op=':';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='/')
{
k[j].pos=i;
k[j++].op='/';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='*')
{
k[j].pos=i;
k[j++].op='*';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='+')
{
k[j].pos=i;
k[j++].op='+';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='-')
{
k[j].pos=i;
k[j++].op='-';
}
}
void explore()
{
i=1;
while(k[i].op!='\0')
{
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos]=tmpch--;
printf("\t%c := %s%c%s\t\t",str[k[i].pos],left,k[i].op,right);
for(j=0;j <strlen(str);j++)
if(str[j]!='$')
printf("%c",str[j]);
printf("\n");
i++;
}
fright(-1);
if(no==0)
{
fleft(strlen(str));
printf("\t%s := %s",right,left);
getch();
exit(0);
}
printf("\t%s := %c",right,str[k[--i].pos]);
getch();
}
void fleft(int x)
{
int w=0,flag=0;
x--;
while(x!= -1 &&str[x]!= '+' &&str[x]!='*'&&str[x]!='='&&str[x]!='\0'&&str[x]!='-
'&&str[x]!='/'&&str[x]!=':')
{
if(str[x]!='$'&& flag==0)
{
left[w++]=str[x];
left[w]='\0';
str[x]='$';
flag=1;
}
x--;
}
}
void fright(int x)
{
int w=0,flag=0;
x++;
while(x!= -1 && str[x]!=
'+'&&str[x]!='*'&&str[x]!='\0'&&str[x]!='='&&str[x]!=':'&&str[x]!='-'&&str[x]!='/')
{
if(str[x]!='$'&& flag==0)
{
right[w++]=str[x];
right[w]='\0';
str[x]='$';
flag=1;
}
x++;
}
}
OUTPUT:
Enter the Expression: w:=a*b+c/d-e/f+g*h
The intermediate code: Expression:
Z := c/d w := a*b+Z-e/f+g*h
Y := e/f w := a*b+Z-Y+g*h
X := a*b w := -X+Z-Y+g*h
W := g*h w := -X+Z-Y+W
V := X+Z w := V-Y+W
U := Y+W w := V-U
T := V-U w :=T
w := T
CODE GENERATION
PROGRAM:
#include"stdio.h"
#include"conio.h"
#include"string.h"
#include"stdlib.h"
structquadraple
{
intpos;
char op;
char arg1[5];
char arg2[5];
char result[5];
}
quad[15];
int n=0;
void assignment(int);
void uminus(int );
void explore();
void codegen(char op[5],int);
char tuple[15][15];
int main(void)
{
FILE *src;
intnRetInd,i;
char str[15];
clrscr();
src=fopen("int.txt","r");
fscanf(src,"%s",str);
while(!feof(src))
{
strcpy(tuple[n++],str);
fscanf(src,"%s",str);
}
printf("INPUT:\nIntermiate codes:\n");
for(i=0;i< n;i++)
printf("%s\n",tuple[i]);
explore();
getch();
clrscr();
printf("OUTPUT:\n");
printf("Quadruple: \n");
printf("pos\topr\targ1\targ2\tresult\n");
for(i=0;i< n;i++)
printf("\n%d\t%c\t%s\t%s\t%s",quad[i].pos,quad[i].op,quad[i].arg1,quad[i].arg2,quad
[i].result);
i=0;
printf("\n\ncode generated :\n");
while(i< n)
{
if(quad[i].op=='+')
codegen("ADD",i);
if(quad[i].op=='=')
assignment(i);
if(quad[i].op=='-')
if(!strcmp(quad[i].arg2,"\0"))
uminus(i);
else
codegen("SUB",i);
if(quad[i].op=='*')
codegen("MUL",i);
if(quad[i].op=='/')
codegen("DIV",i);
i++;
}
getch();
fcloseall();
return 0;
}
void codegen(char op[5],int t)
{
char str[25];
printf("MOV %s,R0\n",quad[t].arg1);
printf("%s %s,R0\n",op,quad[t].arg2);
printf("MOV R0,%s\n",quad[t].result);
}
void assignment(int t)
{
char str[25];
printf("MOV %s,%s\n",quad[t].result,quad[t].arg1);
}
void uminus(int t)
{
char str[25];
printf("MOV R0,0\n");
printf("SUB %s,R0\n",quad[t].arg1);
printf("MOV R0,%s\n",quad[t].result);
}
void explore()
{
int i,j,t,t1,t2;
for(i=0;i < n;i++)
{
quad[i].pos=i;
for(j=0,t=0;j <strlen(tuple[i])&&tuple[i][j]!='=';j++)
{
quad[i].result[t++]=tuple[i][j];
}
t1=j;
quad[i].result[t]='\0';
if(tuple[i][j]=='=')
{
quad[i].op='=';
}
if(tuple[i][j+1]=='+'||tuple[i][j+1]=='-'||tuple[i][j+1]=='*'||tuple[i][j+1]=='/')
{
quad[i].op=tuple[i][j+1];
t1=j+1;
}
for(j=t1+1,t=0;j< strlen(tuple[i])&&tuple[i][j]!='+'&&tuple[i][j]!='-
'&&tuple[i][j]!='*'&&tuple[i][j]!='/';j++)
{
quad[i].arg1[t++]=tuple[i][j];
}
t2=j;
quad[i].arg1[t]='\0';
if(tuple[i][j]=='+'||tuple[i][j]=='-'||tuple[i][j]=='*'||tuple[i][j]=='/')
{
quad[i].op=tuple[i][j];
}
for(j=t2+1,t=0;j< strlen(tuple[i]);j++)
{
quad[i].arg2[t++]=tuple[i][j];
}
quad[i].arg2[t]='\0';
}
}
OUTPUT:
Input File:Int.txt
a=b+c
d=a*e
f=d/g
Quadruple:
Pos opr arg1 arg2 result
0 + b c a
1 * a e d
2 / d g f
Code generated:
MOV b,R0
ADD c,R0
MOV R0, a
MOV a,R0
MUL e,R0
MOV R0,d
MOV d,R0
DIV g,R0
MOV R0,f