unit3part1
unit3part1
UNIT III
Question
Option A
Recursive Descent parsing is LL(1) parsing
which is top down parsing.
Question
For the grammar below, a partial LL(1) parsing table is also presented
along with the grammar. Entries that need to be filled are indicated as
E1, E2, and E3. |epsilon is the empty string, $ indicates end of input,
and, | separates alternate right hand sides of productions.
Answer: (A)
• Graphical representation
• Linear representation
Graphical representation:
• Syntax tree-It depicts hierarchical structure of the source language
• DAG: It gives same information but in more compact way
Linear representation:
• Postfix notation
• Three address code: TAC instructions are of the form
x=y op z,
where x, y, z are names, constants or compiler generated
temporaries,
op is any operator
Consider the statement
x= a* b + c can be written as
Forms of Intermediate representation
Linear representation:
• Postfix notation
• Three address code: TAC instructions are of the form
x=y op z,
where x, y, z are names, constants or compiler generated
temporaries,
op is any operator
Consider the statement
x= a* b + c can be written as
T1=a*b
T2=T1+c
x=T2
Three Address Code
Triples
Indirect triples
3 Address Instruction Set
• Assignment:
• x = y op z (op binary) • Procedure call/return:
• x = op y (op unary); • param x, k (x is the kth
•x=y param)
• Jumps: • call p
• if ( x op y ) goto L (L a • return
label);
• goto L • Type Conversion:
• Pointer and indexed • x = cvt_A_to_B y (A, B base
assignments: types) e.g.:
• x = y[ z ] cvt_int_to_float
• y[ z ] = x
• x = &y • Miscellaneous
• x = *y • label L
• *y = x.
Data structures for three address codes
• Quadruples :
• Has four fields: op, arg1, arg2 and result
• It uses temporaries to store intermediate result.
Advantage –
• Easy to rearrange code for global optimization.
• One can quickly access value of temporary variables
using symbol table.
Disadvantage –
• Contain lot of temporaries.
• Temporary variable creation increases time and space
complexity.
Data structures for three address codes
• Triples :
• Temporaries are not used and instead references to
instructions (another triple’s value) are made
• It consist 3 fields, op, arg1 and arg2 . The result field
is not considered
Disadvantage –
• Temporaries are implicit and difficult to rearrange
code.
• It is difficult to optimize because optimization
involves moving intermediate code. When a triple is
moved, any other triple referring to it must be
updated also.
Data structures for three address codes
• Indirect triples:
• In addition to triples we use a list of pointers to
triples in the desired order.
• This representation makes use of pointer to the
listing of all references to computations which is
made separately and stored. Its similar in utility as
compared to quadruple representation but requires
less space than it. Temporaries are implicit and
easier to rearrange code.
Intermediate Code Representation-
Example
Represent the expression a = (b + c) * −c in
quadruple, triple and indirect triple representation
TAC:
T1 = b + c
T2 = −c
T3 = T1* T2
a = T3
Intermediate Code Representation-
Example (Contd..)
TAC:
T1 = b + c
T2 = −c
T3 = T1* T2
a = T3
Quadruple
op x (operand1) y (operand2) z (result)
Intermediate Code Representation-
Example (Contd..)
TAC:
T1 = b + c
T2 = −c
T3 = T1* T2
a = T3
Quadruple
op x (operand1) y (operand2) z (result)
(1) + b c T1
(2) - c T2
(3) * T1 T2 T3
(4) = a T3
Intermediate Code Representation-
Example (Contd..)
TAC:
T1 = b + c
T2 = −c
T3 = T1* T2
a = T3
Quadruple
op x (operand1) y (operand2) z (result)
(1) + b c T1
(2) - c T2
(3) * T1 T2 T3
(4) = a T3
Intermediate Code Representation-
Example (Contd..)
TAC:
T1 = b + c
T2 = −c
T3 = T1* T2
a = T3
Triple
op x (operand1) y (operand2)
(1) - b c
(2) - c
(4) = a (3)
Intermediate Code Representation-
Example (Contd..)
TAC:
T1 = b + c
T2 = −c
T3 = T1* T2
a = T3
Indirect Triple
op x (operand1) y (operand2)
(1) (1) - b c
(2) (2) - c
(3) (3) * (1) (2)
(4) (4) = a (3)
Example
• a =b * - c + b *- c
Happy Learning !