0% found this document useful (0 votes)
153 views

Qa - CD Unit-3

Syntax directed translation associates semantic rules with productions in a context-free grammar. It allows attaching subroutines to grammar rules to generate intermediate code during parsing. There are two types of attributes in syntax directed translation: synthesized attributes whose values depend on child nodes, and inherited attributes whose values depend on parent or sibling nodes. Intermediate code generation takes the parse tree as input and produces machine-independent code as output, allowing optimizations and retargeting of code generation.

Uploaded by

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

Qa - CD Unit-3

Syntax directed translation associates semantic rules with productions in a context-free grammar. It allows attaching subroutines to grammar rules to generate intermediate code during parsing. There are two types of attributes in syntax directed translation: synthesized attributes whose values depend on child nodes, and inherited attributes whose values depend on parent or sibling nodes. Intermediate code generation takes the parse tree as input and produces machine-independent code as output, allowing optimizations and retargeting of code generation.

Uploaded by

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

UNIT-3

Q1. Define syntax directed translation. Construct an


annotated parse tree for the expression (4 * 7 + 1) * 2,
using the simple desk calculator grammar.
Ans.

 1. Syntax directed definition/translation is a generalization of context free


grammar in which each grammar production X → ⍺ is associated with a set of
semantic rules of the form a: = f(b1,b2…….bk), where a is an attribute obtained
from the function f.
 2. Syntax directed translation is a kind of abstract specification.
 3. It is done for static analysis of the language.
 4. It allows subroutines or semantic actions to be attached to the productions of a
context free grammar. These subroutines generate intermediate code when
called at appropriate time by a parser for that grammar.
 5. The syntax directed translation is partitioned into two subsets called the
synthesized and inherited attributes of grammar.

Annotated tree for the expression (4*7 + 1)*2:


Q2. Explain attributes. What are synthesized and
inherited attribute?
Ans. Attributes:

 1. By tying attributes to the grammar symbols that correspond to a language


construct, information is connected with that construct.
 2. Grammar symbols that serve as the labels of the nodes in the parse tree are
associated with attributes.
 3. An attribute can (reasonably) represent anything, including a string, a number,
a type, a location in memory, a section of code, etc.
 4. A semantic rule connected to the production used at a parse tree node
determines the value of an attribute at that node.
Synthesized attribute:

 1. An attribute at a node is said to be synthesized if its value is computed from


the attributed values of the children of that node in the parse tree.
 2. A syntax directed definition that uses the synthesized attributes is exclusively
said to be S-attributed definition.
 3. Thus, a parse tree for S-attributed definition can always be annotated by
evaluating the semantic rules for the attributes at each node from leaves to root.
 4. If the translations are specified using S-attributed definitions, then the
semantic rules can be conveniently evaluated by the parser itself during the
parsing.
For example: A parse tree along with the values of the attributes at nodes (called an
“annotated parse tree”) for an expression 2 + 3*5 with synthesized attributes is shown in
the Fig.

Inherited attribute:

 1. An inherited attribute is one whose value at a node in a parse tree is defined in


terms of attributes at the parent and/or sibling of that node.
 2. Inherited attributes are convenient for expressing the dependence of a
programming language construct.
For example: Syntax directed definitions that uses inherited attribute are given as:
The parse tree, along with the attribute values at the parse tree nodes, for an input
string int id1, id2, and id3, is shown in the Fig.

Q3. What is intermediate code generation and discuss


benefits of intermediate code ?
Ans. Intermediate code generation is the fourth phase of compiler which takes parse
tree as an input from semantic phase and generates an intermediate code as output.

The benefits of intermediate code are:

 1. Intermediate code is machine independent, which makes it easy to retarget


the compiler to generate code for newer and different processors.
 2. Intermediate code is nearer to the target machine as compared to the source
language so it is easier to generate the object code.
 3. The intermediate code allows the machine independent optimization of the
code by using specialized techniques.
 4. Syntax directed translation implements the intermediate code generation, thus
by augmenting the parser, it can be folded into the parsing.
Q4. Write the quadruples, triple and indirect triple for
the following expression:
(x + y) * (y + z)+ (x + y + z)

Ans. The three address code for given expression:

i. The quadruple representation:

ii. The triple representation:

iii. The indirect triple representation:


Q5. Explain procedure call with example.
Ans. Procedures call:

 1. Procedure is an important and frequently used programming construct for a


compiler.
 2. It is used to generate code for procedure calls and returns.
 3. Queue is used to store the list of parameters in the procedure call.
 4. The translation for a call includes a sequence of actions taken on entry and
exit from each procedure. Following actions take place ina calling sequence:
 a. When a procedure call occurs then space is allocated for activation
record.
 b. Evaluate the argument of the called procedure.
 c. Establish the environment pointers to enable the called procedure to
access data in enclosing blocks.
 d. Save the state of the calling procedure so that it can resume execution
after the call.
 e. Also save the return address. It is the address of the location to which the
called routine must transfer after it is finished.
 f. Finally generate a jump to the beginning of the code for the called
procedure.
For example: Let us consider a grammar for a simple procedure call statement:
A suitable transition scheme for procedure call would be:

Q6. Explain declarative statements with example.


Ans. In the declarative statements the data items along with their data types are
declared.

For example:
 1. Initially, the value of offset is set to zero. The computation of offset can be
done by using the formula offset = offset + width.
 2. In the above translation scheme, T.type, T.width are the synthesized
attributes. The type indicates the data type of corresponding identifier and width
is used to indicate the memory units associated with an identifier of
corresponding type. For instance integer has width 4 and real has 8.
 3. The rule D → id: T is a declarative statements for id declaration. The enter_tab
is a function used for creating the symbol table entry for identifier along with its
type and offset.
 4. The width of array is obtained by multiplying the width of each element by
number of elements in the array.
 5. The width of pointer types of supposed to be 4.

You might also like