Syntax-Directed-Translation
Question 1 |
Consider the syntax-directed translation schema (SDTS) shown below:
E → E + E {print “+”}
E → E ∗ E {print “.”}
E → id {print id.name}
E → (E)
An LR-parser executes the actions associated with the productions immediately after a reduction by the corresponding production. Draw the parse tree and write the translation for the sentence.
(a+b)∗(c+d), using the SDTS given above.
Theory Explanation. |
Question 2 |
The syntax of the repeat-until statement is given by the gollowing grammar
S → repeat S1 until E
Where E stands for expressions, S and S1 stand for statement. The non-terminals S and S1 have an attribute code that represents generated code. The nonterminal E has two attributes. The attribute code represents generated code to evaluate the expression and store its truth value in a distinct variable, and the attribute varName contains the name of the variable in which the truth value is stored? The truth-value stored in the variable is 1 if E is true, 0 if E is false.
Give a syntax-directed definition to generate three-address code for the repeatuntil statement. Assume that you can call a function newlabel( ) that returns a distinct label for a statement. Use the operator ‘\\’ to concatenate two strings and the function gen(s) to generate a line containing the string s.
Theory Explanation is given below. |
Question 3 |
In a bottom-up evaluation of a syntax directed definition, inherited attributes can
always be evaluated | |
be evaluated only if the definition is L-attributed | |
be evaluated only if the definition has synthesized attributes | |
never be evaluated |
L-Attributed definitions are a class of syntax directed definitions whose attributes can be evaluated by a single traversal of the parse-tree.
