Syntax-directed Translation · Compiler Design
17 questions · asked in 11 of 27 years (2000–2026)
MCQ 13 · MSQ 2 · NAT 21-mark 10 · 2-mark 7
2000Questions per year2026
Questions 6–10 of 17
Consider the syntax directed translation given by the following grammar and semantic rules. Here are non-terminals. is the starting non-terminal, and are lexical tokens corresponding to input letters , respectively. denotes the synthesized attribute (a numeric value) associated with a non-terminal . and denote occurrences of and on the right hand side of a production, respectively. For the tokens and , and .
The value computed by the translation scheme for the input string
is ____ (Rounded off to three decimal places)
The value computed by the translation scheme for the input string
is ____ (Rounded off to three decimal places)
Answer: 2.374 to 2.376
Integer part "10" (binary):
I for "1" = 1; I for "10" = .
Fraction part "011" (evaluated right to left):
- F("1") =
- F("11") =
- F("011") =
N.val = 2 + 0.375 = 2.375 (i.e. binary 10.011).
Answer: 2.375
I for "1" = 1; I for "10" = .
Fraction part "011" (evaluated right to left):
- F("1") =
- F("11") =
- F("011") =
N.val = 2 + 0.375 = 2.375 (i.e. binary 10.011).
Answer: 2.375
Consider the following grammar (that admits a series of declarations, followed by expressions) and the associated syntax directed translation (SDT) actions, given as pseudo-code
With respect to the above grammar, which one of the following choices is correct?
With respect to the above grammar, which one of the following choices is correct?
Answer: (B)
The rule always gives E.type = int, even if ID was declared as bool. So:
- Integer declarations and integer expressions (+) are type-checked correctly.
- Boolean expressions fail. !ID sees an int and reports an error.
- Nothing runs in an infinite loop.
Answer: type-check integer variable declarations and integer expressions
- Integer declarations and integer expressions (+) are type-checked correctly.
- Boolean expressions fail. !ID sees an int and reports an error.
- Nothing runs in an infinite loop.
Answer: type-check integer variable declarations and integer expressions
Consider the following C code segment:
a = b + c;
e = a + 1;
d = b + c;
f = d + 1;
g = e + f;
In a compiler, this code segment is represented internally as a directed acyclic graph (DAG). The number of nodes in the DAG is _____________
a = b + c;
e = a + 1;
d = b + c;
f = d + 1;
g = e + f;
In a compiler, this code segment is represented internally as a directed acyclic graph (DAG). The number of nodes in the DAG is _____________
Answer: 6
Nodes that compute the same value are shared in the DAG:
- Leaves: b, c and 1, which is 3 nodes.
- b + c is used by both a and d so it is 1 node.
- (b + c) + 1 is used by both e and f so it is 1 node.
- e + f is 1 node.
Total = 6
Answer: 6
- Leaves: b, c and 1, which is 3 nodes.
- b + c is used by both a and d so it is 1 node.
- (b + c) + 1 is used by both e and f so it is 1 node.
- e + f is 1 node.
Total = 6
Answer: 6
Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let be the placeholders for the non-terminals D, T, L or in the following table:
Which one of the following are the appropriate choices for ?
| Production rule | Semantic action |
| D → T L | X1.type = X2.type |
| T → int | T.type = int |
| T → float | T.type = float |
| L → L1 , id | X3.type = X4.type addType(id.entry, X5.type) |
| L → id | addType(id.entry, X6.type) |
Which one of the following are the appropriate choices for ?
Answer: (A)
Here the type is passed from T to the list L, so it is an inherited attribute.
- In the rule D T L, the list L takes the type of T , so .
- In the rule L , id, the type is handed down to the smaller list , so (and id gets L.type).
Answer:
- In the rule D T L, the list L takes the type of T , so .
- In the rule L , id, the type is handed down to the smaller list , so (and id gets L.type).
Answer:
Which one of the following statements is FALSE?
Answer: (B)
Type checking belongs to semantic analysis. This phase runs after parsing, because it works on the syntax tree. So the statement "type checking is done before parsing" is false. The other statements are true.
Answer: Type checking is done before parsing
Answer: Type checking is done before parsing
Keep practising
Topics in Compiler Design
Lexical AnalysisParsing
Syntax-directed TranslationYou are here
Runtime EnvironmentAssemblers, Linkers and LoadersIntermediate Code GenerationMatching