Experiment No 7 (Infix To Postfix)
Experiment No 7 (Infix To Postfix)
07
Experiment Title
Intermediate code Generator
Task:
Roll No. 66
+ a b t1
* t1 c t2
3. Triples
Similar to quadruples but without result storage.
Example:
(0) + a b
(1) * (0) c
/* Rule Section */
%%
{ALPHA}({ALPHA}|{DIGIT})* return ID;
{DIGIT}+ {yylval=atoi(yytext); return
ID;}
[\n \t] yyterminate();
. return yytext[0];
%%
Yc.y
%{
/* Definition section */
#include <stdio.h>
#include <stdlib.h>
%}
%token ID
%left '+' '-'
%left '*' '/'
%left UMINUS
/* Rule Section */
%%
S:E
E : E'+'{A1();}T{A2();}
| E'-'{A1();}T{A2();}
|T
;
T : T'*'{A1();}F{A2();}
| T'/'{A1();}F{A2();}
|F
;
F : '('E{A2();}')'
| '-'{A1();}F{A2();}
| ID{A3();}
;
%%
#include"lex.yy.c"
char st[100];
int top=0;
//driver code
int main()
{
printf("Enter infix expression: ");
yyparse();
printf("\n");
return 0;
}
A1()
{
st[top++]=yytext[0];
}
A2()
{
printf("%c", st[--top]);
}
A3()
{
printf("%c", yytext[0]);
}
Output of the
program:
Outcome of the This experiment successfully converts an infix expression into its
Experiment: equivalent postfix expression using a Yacc parser.
References: https://2k8618.blogspot.com/2011/08/parser-for-infix-expression-to-
postfix.html
https://www.geeksforgeeks.org/yacc-program-for-conversion-of-
infix-to-postfix-expression/