Professional Documents
Culture Documents
COMPILER DESIGN ASSIGNMENT TWO 17 12 2022 Submit
COMPILER DESIGN ASSIGNMENT TWO 17 12 2022 Submit
Intermediate code can translate the source program into the machine program.
Intermediate code is generated because the compiler can’t generate machine code
directly in one pass.
Therefore, first, it converts the source program into intermediate code, which performs
efficient generation of machine code further.
The intermediate code can be represented in the form of postfix notation, syntax
tree, directed acyclic graph, three address codes, Quadruples, and triples.
2. Postfix Notation − In postfix notation, the operator comes after an operand, i.e., the
operator follows an operand.
Example
Postfix Notation for the expression (a+b) * (c+d) is ab + cd +*
Postfix Notation for the expression (a*b) - (c+d) is ab* + cd + - .
Syntax Trees − It is condensed form of parse tree in which leaves are identifiers
and interior node will be operators.
3. Syntax Tree:
A syntax tree is nothing more than a condensed form of a parse tree. The operator
and keyword nodes of the parse tree are moved to their parents, a chain of single
productions is replaced by the single link in the syntax tree the internal nodes are
operators, and child nodes are operands. To form a syntax tree put parentheses in the
expression, this way it is easy to recognize which operand should come first.
2)Describe Three-Address Code with its examples
(1pt)
Example:
Expression a = b + c + d can be converted into the following Three Address Code.
t1 = b + c
t2 = t1 + d
a = t2
Where t1 and t2 are temporary variables generated by the compiler.
Most of the time a statement includes less than three references, but it is still known as
a three-address statement.
is a tool that depicts the structure of basic blocks, helps to see the flow of values flowing
among the basic blocks, and offers optimization too. DAG provides easy transformation
on basic blocks. DAG can be understood here: Leaf nodes represent identifiers, names
or constants.
In Compiler design, Directed Acyclic Graph is a directed graph that does not contain
any cycles in it.
a) Production
A production or production rule in computer science
Example-1:
As the start symbol is S then we can produce Aa, Ab, a,b, which can further
produce strings where A can be replaced by the Strings mentioned in the
production rules and hence this grammar can be used to produce strings of the
form (a+b)*.
Derivation Of Strings :
Example-2:
As the start symbol is S then we can produce Aa, AAa, a, which can further
produce strings where A can be replaced by the Strings mentioned in the
production rules and hence this grammar can be used to produce strings of
form (a)*.
Derivation Of Strings :
A->a #using production rule 3
OR
A->Aa #using production rule 1
Aa->aa #using production rule 3
OR
A->Aa #using production rule 1
Aa->AAa #using production rule 1
AAa->Aa #using production rule 4
Aa->aa #using production rule 3
Equivalent Grammars:
Grammars are said to be equivalent id they produce the same language.
Both syntax tree of previous phase and symbol table are used to check the
consistency of the given code.
Semantic Analyzer:
It uses syntax tree and symbol table to check whether the given program is
semantically consistent with language definition.
It gathers type information and stores it in either syntax tree or symbol table.
Example:
float x = 10.1;
float y = x*30;
In the above example integer, 30 will be type casted to float 30.0 before
multiplication, by semantic analyzer.
An annotated parse tree is a parse tree showing the values of the attributes
at each node. The process of computing the attribute values at the nodes is
called annotating or decorating the parse tree.
6)Code Optimization: Describe the following terms
with examples (4pts)
The code optimization in the synthesis phase is a program transformation technique,
which tries to improve the intermediate code by making it consume fewer resources (i.e.
CPU, Memory) so that faster-running machine code will result.
Example:
Copy Propagation:
//Before Optimization
c = a * b
x = a
till
d = x * b + 4
//After Optimization
c = a * b
x = a
till
d = a * b + 4
b)Dead-code Elimination
Copy propagation often leads to making assignment statements into dead
code.
A variable is said to be dead if it is never used after its last definition.
In order to find the dead variables, a data flow analysis should be done.
Example:
c = a * b
x = a
till
d = a * b + 4
//After elimination :
c = a * b
till
d = a * b + 4
1. Postfix Notation:
The postfix notation for the same expression places the operator at the
right end as ab +.
Example 1:
Example 2:
2. Three-Address Code:
A statement involving no more than three references (two for operands and one
for result) is known as a three address statement. A sequence of three address
statements is known as a three address code. Three address statement is of
form x = y op z, where x, y, and z will have address (memory location).
Sometimes a statement might contain less than three references but it is still
called a three address statement.
Example:
3.Syntax Tree:
A syntax tree is nothing more than a condensed form of a parse tree. The
operator and keyword nodes of the parse tree are moved to their parents, a
chain of single productions is replaced by the single link in the syntax tree the
internal nodes are operators, and child nodes are operands. To form a 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)
b) Describe Target Languages with example