1
1
1
2. What is the difference between Pars Tree, Abstract Syntax Tree and Directed
Acyclic Graph? Describe clearly with appropriate example.
Virtually every phase of the compiler will use the symbol table.
The initialization phase will place keywords, operators, and standard
identifiers in it.
If we generate machine code directly from source code then for n target
machine we will have optimizers and n code generator but if we will have a
machine-independent intermediate code, we will have only one optimizer.
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 representations:
1. Postfix Notation:
Also known as reverse Polish notation or suffix notation.
In the infix notation, the operator is placed between operands.
e.g. a +b. Postfix notation positions the operator at the right end as in ab +.
For any postfix expressions e1 and e2 with a binary operator (+) , applying
the operator yields e1e2+.
Postfix notation eliminates the need for parentheses, as the operator’s
position and arity allow unambiguous expression decoding.
In postfix notation, the operator consistently follows the operand.
Example 1: The postfix representation of the expression
(a + b) * c is : ab + c *
Example 2: The postfix representation of the expression
(a – b) * (c + d) + (a – b) is : ab – cd + *ab -+
Three-Address Code:
A three address statement involves a maximum of three references,
consisting of two for operands and one for the result.
A sequence of three address statements collectively forms a three
address code.
The typical form of a three address statement is expressed as x = y op
z, where x, y, and z represent memory addresses.
Each variable (x, y, z) in a three address statement is associated with
a specific memory location.
While a standard three address statement includes three references,
there are instances where a statement may contain fewer than three
references, yet it is still categorized as a three address statement.
Example: The three address code for the expression a + b * c + d : T1
= b * c T2 = a + T1 T3 = T2 + d; T 1 , T2 , T3 are temporary variables.
There are 3 ways to represent a Three-Address Code in compiler
design:
i) Quadruples
ii) Triples
iii) Indirect Triples
3. Syntax Tree:
A syntax tree serves as a condensed representation of a parse tree.
The operator and keyword nodes present in the parse tree undergo a
relocation process to become part of their respective parent nodes in
the syntax tree.
the internal nodes are operators and child nodes are operands.
The syntax tree not only condenses the parse tree but also offers an
improved visual representation of the program’s syntactic structure,
Example: x = (a + b * c) / (a – b * c)