ARYAN
NAME-Arjun SRIVASTAVA
Chaurasia
SECTION-CS-31
UNIVERSITY ROLL NO.-1705410034
38
CD ASSIGNMENT-3
Ques-1. What is syntax directed Translation (SDT)? Write short note on
a. Synthesized Attribute
b. Inherited Attribute
c. Dependency Graph
d. Evolution Order
e. Directed Acyclic Graph
ANS1- Syntax Directed Translation are augmented rules to the grammar that facilitate semantic analysis.
SDT involves passing information bottom-up and/or top-down the parse tree in form of attributes
attached to the nodes. Syntax directed translation rules use 1) lexical values of nodes, 2) constants & 3)
attributes associated to the non-terminals in their definitions.
The general approach to Syntax-Directed Translation is to construct a parse tree or syntax tree and
compute the values of attributes at the nodes of the tree by visiting them in some order. In many cases,
translation can be done during parsing without building an explicit tree
1.Synthesized attributes –
A Synthesized attribute is an attribute of the non-terminal on the left-hand side of a production.
Synthesized attributes represent information that is being passed up the parse tree. The attribute can
take value only from its children (Variables in the RHS of the production).
For eg. let’s say A -> BC is a production of a grammar, and A’s attribute is dependent on B’s attributes or
C’s attributes then it will be synthesized attribute.
2.Inherited attributes –
An attribute of a nonterminal on the right-hand side of a production is called an inherited attribute. The
attribute can take value either from its parent or from its siblings (variables in the LHS or RHS of the
production).
For example, let’s say A -> BC is a production of a grammar and B’s attribute is dependent on A’s
attributes or C’s attributes then it will be inherited attribute.
3.Dependency Graph- A dependency graph is a directed graph representing dependencies of several
objects towards each other. It is possible to derive an evaluation order or the absence of an evaluation
order that respects the given dependencies from the dependency graph.
In a dependency graph, the cycles of dependencies (also called circular dependencies) lead to a
situation in which no valid evaluation order exists, because none of the objects in the cycle may be
evaluated first. If a dependency graph does not have any circular dependencies, it forms a directed
acyclic graph, and an evaluation order may be found by topological sorting. Most topological sorting
algorithms are also capable of detecting cycles in their inputs; however, it may be desirable to
perform cycle detection separately from topological sorting in order to provide appropriate handling for
the detected cycles.
Assume the simple calculator from before. The equation system "A=B; B=D+C; C=D+A; D=12;" contains
a circular dependency formed by A, B and C, as B must be evaluated before A, C must be evaluated
before B and A must be evaluated before C.
4.Evolution order-the topology sort of directed acyclic graph(DAG) is any ordering of
m1,m2,m3,.........,mn of nodes of the graph such that if mi-->mj is an edge then m1 appears before mj.
Any Topological sort of dependency graph gives a valid evolution order of sementic rules.
5. Directed Acyclic Graph (DAG) – It 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 can be understood here:
Leaf nodes represent identifiers, names or constants. Interior nodes represent operators.
Ques-2 Discuss the role of Syntax directed translation scheme
ANS2-Syntax directed translation scheme
o The Syntax directed translation scheme is a context -free grammar.
o The syntax directed translation scheme is used to evaluate the order of semantic rules.
o In translation scheme, the semantic rules are embedded within the right side of the productions.
o The position at which an action is to be executed is shown by enclosed between braces. It is
written within the right side of the production.
Example
Production Semantic Rules
S E $ { printE.VAL }
E E + E{E.VAL := E.VAL + E.VAL }
E E * E {E.VAL := E.VAL * E.VAL }
E (E) {E.VAL := E.VAL }
E I {E.VAL := I.VAL }
I I digit {I.VAL := 10 * I.VAL + LEXVAL }
I digit { I.VAL:= LEXVAL}
Ques-3.What is Quadruples Triples Indirect Triples? Write the triple for expression
x=(a+b)*&c/d
ANS3- The commonly used representations for implementing Three Address Code are-
1. Quadruples
2. Triples
3. Indirect Triples
1. Quadruple –
It is structure with consist of 4 fields namely op, arg1, arg2 and result. op denotes the operator and arg1
and arg2 denotes the two operands and result is used to store the result of the expression.
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.
2. Triples –
This representation doesn’t make use of extra temporary variable to represent a single operation instead
when a reference to another triple’s value is needed, a pointer to that triple is used. So, it consist of only
three fields namely op, arg1 and arg2.
● The contents of the operand1, operand2, and result fields are therefore normally the pointers to
the symbol records for the names represented by these fields.
● Hence, it becomes necessary to enter temporary names into the symbol table as they are
created.
● This can be avoided by using the position of the statement to refer to a temporary value.
● If this is done, then a record structure with three fields is enough to represent the three-address
statements:
● The first holds the operator value, and the next two holding values for the operand1 and
operand2, respectively. Such a representation is called a "triple representation".
● The contents of the operand1 and operand2 fields are either pointers to the symbol table
records, or they are pointers to records (for temporary names) within the triple representation
itself.
● For example, a triple representation of the three-address code for the statement x = (a + b)* ˆ’
c/d.
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. With help of pointer one can directly
access symbol table entry.
expression x=(a+b)*&c/d
3. Indirect Triples –
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.
Ques-4. Discuss the importance of symbol table?
ANS4- Symbol table is an important data structure created and maintained by compilers in order to
store information about the occurrence of various entities such as variable names, function names,
objects, classes, interfaces, etc. Symbol table is used by both the analysis and the synthesis parts of a
compiler.
A symbol table may serve the following purposes depending upon the language in hand:
● To store the names of all entities in a structured form at one place.
● To verify if a variable has been declared.
● To implement type checking, by verifying assignments and expressions in the source code are
semantically correct.
● To determine the scope of a name (scope resolution).
● A symbol table is simply a table which can be either linear or a hash table. It maintains an entry
for each name in the following format:
<symbol name, type, attribute>
Ques-5. How Boolean expression and assignment statements are
translated?
ANS5- Methods of Translating Boolean Expressions:
There are two principal methods of representing the value of a boolean expression. They are :
* To encode true and false numerically and to evaluate a boolean expression analogously to an
arithmetic expression. Often, 1 is used to denote true and 0 to denote false.
* To implement boolean expressions by flow of control, that is, representing the value of a boolean
expression by a position reached in a program. This method is particularly convenient in
implementing the boolean expressions in flow-of-control statements, such as the if-then and
while-do statements.
Translation scheme using a numerical representation for booleans