0% found this document useful (0 votes)
30 views4 pages

Experiment No 7 (Infix To Postfix)

The document outlines Experiment No. 07, focusing on Intermediate Code Generation and converting infix expressions to postfix using a Yacc parser. It details the objectives, theory, algorithms, and advantages of intermediate code generation, as well as providing program code for implementation. The outcome confirms successful conversion of infix expressions to postfix format.

Uploaded by

amishav2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Experiment No 7 (Infix To Postfix)

The document outlines Experiment No. 07, focusing on Intermediate Code Generation and converting infix expressions to postfix using a Yacc parser. It details the objectives, theory, algorithms, and advantages of intermediate code generation, as well as providing program code for implementation. The outcome confirms successful conversion of infix expressions to postfix format.

Uploaded by

amishav2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment No.

07

Experiment Title
Intermediate code Generator

Task:

Implement Intermediate Code Generation and convert infix


expression to postfix expression.(Yacc parser)
Student Name Amisha Verma

Roll No. 66

Objectives: To understand parser generator tool : YACC and Intermediate code


generation phase of compiler
Theory /Algorithm:

1) Explain Intermediate Code Generation Phase in detail


Intermediate Code Generation Phase in Compiler Design
Introduction
The Intermediate Code Generation phase is a crucial step in
the compilation process, where the source code is transformed
into an intermediate representation (IR). This IR is
independent of any specific machine architecture, making it
easier to optimize and convert into final machine code.
Purpose of Intermediate Code Generation
1. Portability: IR is independent of the target machine,
allowing compilers to generate machine code for multiple
platforms.
2. Optimization: IR enables code optimization before
generating the final code.
3. Abstraction: Simplifies compilation by separating front-end
and back-end processes.
Process of Intermediate Code Generation
The process involves translating the syntax tree or abstract
syntax tree (AST) into a form that is easier to analyze and
optimize. The following steps occur:
1. Syntax Tree Traversal: The compiler traverses the AST
generated during parsing.
2. IR Construction: The syntax tree is converted into an
intermediate representation like three-address code (TAC),
quadruples, triples, or control flow graphs.
3. Optimization Opportunities: The IR allows optimizations
like constant folding, common subexpression elimination,
and dead code removal.
4. Storage Management: Variables, constants, and expressions
are efficiently stored in registers and memory.
Types of Intermediate Representations (IRs)
Different forms of IR are used, each having specific benefits:
1. Three-Address Code (TAC)
 Each instruction has at most three operands.
 Example:
 t1 = a + b
 t2 = t1 * c
2. Quadruples
 A four-field structure representing operations.
 Example:

Operator Arg1 Arg2 Result

+ a b t1

* t1 c t2
3. Triples
 Similar to quadruples but without result storage.
 Example:

Index Operator Arg1 Arg2

(0) + a b

(1) * (0) c

4. Abstract Syntax Tree (AST)


 A tree-like representation where each node is an operation.
 Example: The expression (a + b) * c is represented as:
 (*)
 / \
 (+) c
 / \
 a b
Advantages of Intermediate Code Generation
✔ Simplifies code optimization
✔ Enhances portability
✔ Improves efficiency
✔ Separates compilation concerns
Conclusion
The Intermediate Code Generation phase serves as a bridge
between semantic analysis and code optimization, making the
compilation process more structured, efficient, and portable. It
plays a vital role in modern compilers by enabling optimizations
before final code generation.

Program Code: lx.l


%{
/* Definition section */
%}
ALPHA [A-Z a-z]
DIGIT [0-9]

/* 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]);
}

Input to the a*b+c


Program: a+b*d

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/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy