Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE) PDF Download

Intermediate Code Generation

In the analysis-synthesis model of a compiler, the front end of a compiler translates a source program into an independent intermediate code, then the back end of the compiler uses this intermediate code to generate the target code (which can be understood by the machine).

The benefits of using machine independent intermediate code are:

  1. Because of the machine independent intermediate code, portability will be enhanced.For ex, suppose, if a compiler translates the source language to its target machine language without having the option for generating intermediate code, then for each new machine, a full native compiler is required. Because, obviously, there were some modifications in the compiler itself according to the machine specifications.
  2. Retargeting is facilitated
  3. It is easier to apply source code modification to improve the performance of source code by optimising the intermediate code.

Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)

If we generate machine code directly from source code then for n target machine we will have n optimisers and n code generators but if we will have a machine independent intermediate code,
we will have only one optimiser. Intermediate code can be either language specific (e.g., Bytecode for Java) or language. independent (three-address code).

The following are commonly used intermediate code representation :

  1. Postfix notation
  2. Syntax tree
  3. Three-address code

Postfix Notation

The ordinary (infix) way of writing the sum of a and b is with operator in the middle : a + b
The postfix notation for the same expression places the operator at the right end as ab +. In general, if e1 and e2 are any postfix expressions, and + is any binary operator, the result of applying + to the values denoted by e1 and e2 is postfix notation by e1e2 +. No parentheses are needed in postfix notation because the position and arity (number of arguments) of the operators permit only one way to decode a postfix expression. In postfix notation the operator follows the operand.

Eg : the postfix representation of the expression (a – b) * (c + d) + (a – b) is :   ab – cd + ab -+*.

Syntax Tree
Syntax tree is nothing more than condensed form of a parse tree. The operator and keyword nodes of the parse tree are moved to their parents and a chain of single productions is replaced by single link in syntax tree the internal nodes are operators and child nodes are operands. To form syntax tree put parentheses in the expression, this way it's easy to recognize which operand should come first.

Example :
x = (a + b * c) / (a – b * c)

Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)

The document Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Intermediate Code Generation - Compiler Design - Computer Science Engineering (CSE)

1. What is intermediate code generation in computer science engineering (CSE)?
Ans. Intermediate code generation in computer science engineering is the process of converting the source code of a programming language into an intermediate representation that is closer to machine code. This intermediate code acts as a bridge between the high-level source code and the low-level machine code, making it easier for subsequent stages of compilation or interpretation.
2. Why is intermediate code generation important in CSE?
Ans. Intermediate code generation is important in CSE for several reasons. Firstly, it allows for platform independence, as the same intermediate code can be translated into different target machine languages. Secondly, it simplifies the optimization process, as optimizations can be performed on the intermediate code before generating the final machine code. Lastly, it facilitates code reuse and modularization, as intermediate code can be stored and reused for different programs or modules.
3. What are the common intermediate code representations used in CSE?
Ans. There are several common intermediate code representations used in CSE. Some popular ones include: - Three-address code: This representation uses instructions with at most three operands, allowing for efficient translation into machine code. - Quadruples: Quadruples represent statements in four fields, including the operator, two operands, and the result. They are often used for code optimization. - Abstract Syntax Trees (AST): ASTs represent the structure of the source code using a tree-like data structure. They are useful for performing semantic analysis and code generation.
4. How does intermediate code generation help in program optimization?
Ans. Intermediate code generation plays a crucial role in program optimization. By translating the source code into an intermediate representation, it allows for various optimization techniques to be applied before generating the final machine code. These optimizations can include constant folding, common subexpression elimination, dead code elimination, and loop optimization. Performing these optimizations at the intermediate code level can lead to significant improvements in the efficiency and performance of the resulting program.
5. Can intermediate code be executed directly by the computer?
Ans. No, intermediate code cannot be executed directly by the computer. Intermediate code is an abstraction that is closer to the source code than to the machine code. It needs to be further translated into machine code, either through interpretation or compilation, before it can be executed by the computer. The intermediate code serves as an intermediate representation that simplifies the translation process and allows for optimization and code reuse.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

ppt

,

video lectures

,

Objective type Questions

,

Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)

,

Exam

,

Viva Questions

,

Extra Questions

,

MCQs

,

Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)

,

Semester Notes

,

practice quizzes

,

shortcuts and tricks

,

study material

,

Free

,

Previous Year Questions with Solutions

,

Important questions

,

Summary

,

pdf

,

mock tests for examination

,

Sample Paper

,

Intermediate Code Generation | Compiler Design - Computer Science Engineering (CSE)

;