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 11–15 of 17
Consider the following Syntax Directed Translation Scheme(SDTS),with non-terminals {S, A} and terminals {a, b}.
S aA {print 1}
S a { print 2}
A Sb { print 3}
Using the above SDTS, the output printed by a bottom-up parser, for the input aab is:
S aA {print 1}
S a { print 2}
A Sb { print 3}
Using the above SDTS, the output printed by a bottom-up parser, for the input aab is:
Answer: (C)
A bottom-up parser prints at each reduction. Parse with the rules {print 1}, {print 2}, {print 3}:
1. Shift , shift . Reduce the second by : output 2.
2. Shift . Reduce by : output 3.
3. Reduce by : output 1.
The output is 231.
Answer: (C)
1. Shift , shift . Reduce the second by : output 2.
2. Shift . Reduce by : output 3.
3. Reduce by : output 1.
The output is 231.
Answer: (C)
Consider the following translation scheme.
S ER
R * E{print('*');}R|
EF+E{print('+');}|F
F(S)|id{print(id.value);}
Here id is a taken that represents an integer and id.value represents the corresponding integer value. For an input '2*3+4', this translation scheme prints
S ER
R * E{print('*');}R|
EF+E{print('+');}|F
F(S)|id{print(id.value);}
Here id is a taken that represents an integer and id.value represents the corresponding integer value. For an input '2*3+4', this translation scheme prints
Answer: (D)
Build the parse tree for the input.
Walk the tree depth first from left to right. Each print action runs when the walk reaches it:
1. for the first operand: prints 2.
2. The token * is read. Inside the next , the first prints 3.
3. The token + is read. The inner prints 4.
4. The action of now runs: prints +.
5. The action of now runs: prints *.
6. prints nothing.
The output is , that is 234+*.
Answer: (D)
Walk the tree depth first from left to right. Each print action runs when the walk reaches it:
1. for the first operand: prints 2.
2. The token * is read. Inside the next , the first prints 3.
3. The token + is read. The inner prints 4.
4. The action of now runs: prints +.
5. The action of now runs: prints *.
6. prints nothing.
The output is , that is 234+*.
Answer: (D)
Consider the grammar rule for arithmetic expressions. The code generated is targeted to a CPU having a single user register. The subtraction operation requires the first operand to be in the register. If E1 and E2 do not have nay-common sub-expression, in order to get the shortest possible code
Answer: (B)
Evaluate first.
There is only one register, and the subtraction needs in it.
- first: compute and store it in a temporary memory location. Then compute , which stays in the register, and subtract the temporary directly from memory.
- first: compute and store it, since computing needs the register. After is computed it sits in the register, but the subtraction needs there. So has to be stored too and loaded back. This takes extra store and load instructions.
Evaluating first gives the shortest code. The order matters, and no interleaving is needed.
Answer: (B)
There is only one register, and the subtraction needs in it.
- first: compute and store it in a temporary memory location. Then compute , which stays in the register, and subtract the temporary directly from memory.
- first: compute and store it, since computing needs the register. After is computed it sits in the register, but the subtraction needs there. So has to be stored too and loaded back. This takes extra store and load instructions.
Evaluating first gives the shortest code. The order matters, and no interleaving is needed.
Answer: (B)
Which of the following statements is FALSE?
Answer: (C)
In a statically typed language the type of each variable is fixed at compile time and checked then. So (A) is true.
Because the declared type never changes, every value the variable holds during execution is a value of that one type. This still holds with unions or subtyping: a union variable has the single union type, and a variable of a base type that holds a derived-type object still has the single declared type, since that object is also a value of the base type. So (D) is true.
In an untyped language (such as assembly or machine code) values are just bit patterns and carry no type. So (B) is true.
In a dynamically typed language, a variable is not tied to one type, but it is not typeless. The value it holds has a type, and that type is found and checked at run time. For example, a variable holding 5 has an integer value now and may hold a string later. So "variables have no types" is false; it is the variable's type that can change, not that types are missing.
Answer: (C)
Because the declared type never changes, every value the variable holds during execution is a value of that one type. This still holds with unions or subtyping: a union variable has the single union type, and a variable of a base type that holds a derived-type object still has the single declared type, since that object is also a value of the base type. So (D) is true.
In an untyped language (such as assembly or machine code) values are just bit patterns and carry no type. So (B) is true.
In a dynamically typed language, a variable is not tied to one type, but it is not typeless. The value it holds has a type, and that type is found and checked at run time. For example, a variable holding 5 has an integer value now and may hold a string later. So "variables have no types" is false; it is the variable's type that can change, not that types are missing.
Answer: (C)
In a bottom-up evaluation of a syntax directed definition, inherited attributes can
Answer: (B)
A synthesized attribute is computed from the children, so a bottom-up (LR) parser can compute it when it reduces a production. An inherited attribute is computed from the parent or the siblings, so its value is needed before the subtree below is reduced.
An SDD is L-attributed if every inherited attribute of a symbol on the right side depends only on the inherited attributes of the head and on the attributes of the symbols to its left. So, in a left-to-right pass, every value needed is already known when the parser reaches that symbol. A bottom-up parser can then evaluate these attributes: each embedded action is replaced by a marker non-terminal (an empty production) that copies the needed values from the parser stack.
(A) is wrong: an inherited attribute that depends on a symbol to its right is not available in a left-to-right bottom-up pass. (C) is wrong: having synthesized attributes does not help evaluate inherited ones. (D) is wrong: L-attributed definitions show they can be evaluated.
Answer: (B)
An SDD is L-attributed if every inherited attribute of a symbol on the right side depends only on the inherited attributes of the head and on the attributes of the symbols to its left. So, in a left-to-right pass, every value needed is already known when the parser reaches that symbol. A bottom-up parser can then evaluate these attributes: each embedded action is replaced by a marker non-terminal (an empty production) that copies the needed values from the parser stack.
(A) is wrong: an inherited attribute that depends on a symbol to its right is not available in a left-to-right bottom-up pass. (C) is wrong: having synthesized attributes does not help evaluate inherited ones. (D) is wrong: L-attributed definitions show they can be evaluated.
Answer: (B)
Keep practising
Topics in Compiler Design
Lexical AnalysisParsing
Syntax-directed TranslationYou are here
Runtime EnvironmentAssemblers, Linkers and LoadersIntermediate Code GenerationMatching