coul you provide me concise and accurate answers to these compiler design questions Q1. Consider the following SDTS(Syntax Directed Translation Schemes): (a) E → E/T E → E+T E → T T → F F → 2|4|6 {E.val → E1.valT.val} {E.val → E1.val+T.val} {E.val → T.val} {T.val → F.val} {F.val=2} {F.val=4} {F.val=6} Using the above STDS, construct the parse tree for the expression 4/2 + 6/2 and evaluate 'E.val' at the root of tree. Q2. Explain S-attribute and L-attribute with examples. Identify Synthesised and Inherited attributes given below: D → TL L.in = T.type; L.x=T.y L → L1,id L1.in = L.in; L.x=L1.x T → id Justify your answer. Q3. What are the different representations of three address statements. Illustrate with example. Q4. Write 3 address code for the following code segment: int a[10], b[10],dot_prod, i; int a1; int* b1; dot_prod=0; a1=a; b1=b; for(i=0;i < 10;i++) dot_prod=a1++ *b1++; Q5. Differentiate between SDT and SDD.
Q1.
To evaluate the expression 4/2 + 6/2 using the given SDTS, we first construct the parse tree and then compute
E` at the root:
The parse tree for the expression 4/2 + 6/2 is follows:
E / | / | \ E + T / \ / \ E / T / | T | T T | T | / F / F F | F | 4 2 6 2
Assign values according to the rules:
[Bottom-most F nodes have values F.val 4, F.val = 2, F.val = 6, F.val 2] [T nodes take F.val, i.e., T.val = 4, T.val = 2, T.val = 6, T = 2] [E1 -> T.val = 4] 2 -> E1.val / T.val = 4 /2 = 2] (following {E.val → E1.val/T.val}) [E3 T.val = 6] [E4 -> E2.val + T.val = 2 + 6 / 2 = 5] (following {E.val → E1.valT.val})
Thus, the value of E.val
at the root (after evaluating the whole tree) is 5
.
Q2.
S-attribute (synthesized attribute) definitions are attribute evaluation rules that define value of an attribute at a parse tree node in terms of the attribute values at the children of that node. Example: A.attr =.attr + C.attr
, where A
derives B
and C
.
L-attribute (in attribute) definitions permit the value of an attribute at a parse tree node to be defined in terms of the attribute values at the parent and/or siblings of that node. Example: B.attr = A.attr - C.attr
, where A
derives B
C
.
Given D → TL, L.in = T.type; L.x=T.y
, L.in
and L.x
are inherited attributes because their values depend on the values of attributes in parent or sibling nodes. Synthesized attributes in this context aren't explicitly, but typically, if there were D.out
or T.type
defined by the values at their child, these would be synthesized attributes.
Q3. The different representations of three-address statements include:
Example for quadruples:
op | arg1 | arg2 | result --------------------------- * | a | b | 1 + | t1 | c | t
Q4. Three-address code for the code segment look like:
i = 0 t1 = 0 // dot_prod = 0 L1 if i >= 10 goto L2 t2 = i * 4 // offset for a[i] t3 = a1 + t2 4 = *t3 // fetched a[i] t5 = i * 4 // offset for b[i] t6 = b + t5 t7 = *t // fetched b[i] t8 = t * t7 // t8 = a[i] * b] t1 = t1 + t8 // dot_prod +=[i] * b[i] i = i + 1 goto L1 L2: dot_prod = t1
Q5. SDTSyntax-Directed Translation) refers to specific methods where each grammar rule is associated with a set of semantic rules for managing the attributes of the symbols in the grammar. It provides a structured way of associating actions with grammar productions in the parsing process.
SDD (Syntax-Directed Definition) a context-free grammar with attributes and associated rules that define how the attributes should be computed at node in the parse tree. SDDs do not explicitly specify the order in which the attribute values are computed.
The difference between SDT and SDD is that SDT is a scheme for embedding program within a grammar that specifies when the fragments are executed during parsing. It focuses on the translation process itself. In contrast,