⏱️
15 questions · 22 marks · 45 minutes. Negative marking on MCQ only: −1/3 mark for a wrong 1-mark MCQ, −2/3 for a wrong 2-mark MCQ. No negative marking for MSQ or NAT.
Q1MCQ1 markAttributes
Terminals in an SDD can have:
- (A) only inherited attributes
- (B) only synthesized attributes
- (C) both kinds
- (D) no attributes
Q2NAT2 marksEvaluation
With E → E₁ # T {E.val = E₁.val × T.val}, E → T, T → T₁ & F {T.val = T₁.val + F.val}, T → F, F → n, evaluate 2 # 3 & 5 # 6 & 4.
Numerical answer: ________
Q3NAT2 marksEvaluation
With E → E₁ * T {E.val = E₁.val + T.val}, E → T, T → T₁ + F {T.val = T₁.val × F.val}, T → F, F → n, evaluate 2 + 3 * 4 + 5.
Numerical answer: ________
Q4MCQ1 markDependency graph
Evaluation order of attributes must be:
- (A) the order of the productions
- (B) a topological order of the dependency graph
- (C) right to left
- (D) random
Q5MCQ1 markClasses
Every S-attributed definition is:
- (A) L-attributed
- (B) circular
- (C) not evaluable by LR parsers
- (D) inherited-only
Q6MCQ2 marksL-attributed
Production A → B C D. Which rule is allowed in an L-attributed SDD?
- (A) B.i = C.s
- (B) C.i = A.s
- (C) D.i = B.s + C.i
- (D) B.i = D.i
Q7NAT1 markValue stack
An LR parser evaluates the desk calculator on 3 * 5 + 4. What value does the final reduction E → E + T push?
Numerical answer: ________
Q8NAT2 marksTrace
S → a a W {print 1} | b {print 2}; W → S c {print 3}. Output for a a a a b c c?
Numerical answer: ________
Q9NAT2 marksTrace
S → A S {print 1} | A {print 2}; A → a {print 3} | b {print 4}. Output for a b b a?
Numerical answer: ________
Q10MCQ1 markPostfix
The scheme E → T R, R → + T {print('+')} R | ε, T → num {print(num.val)} translates infix to:
- (A) prefix
- (B) postfix
- (C) infix
- (D) binary
Q11MSQ2 marksTranslation schemes
Which are true?
- (A) An action runs when the walk reaches its position.
- (B) Actions at production ends give postorder execution.
- (C) Moving an action never changes the output.
- (D) Middle actions need markers in LR parsers.
Q12NAT1 markSyntax tree
How many nodes does the syntax tree of a − 4 + c have?
Numerical answer: ________
Q13MCQ1 markTypes
The type of i + x with i int and x float is:
- (A) int
- (B) float
- (C) error
- (D) char
Q14MCQ2 marksParsers
An SDD whose inherited attributes depend only on parents’ inherited attributes and left siblings can be evaluated during parsing by:
- (A) a recursive-descent parser
- (B) only after the parse is complete
- (C) the lexer
- (D) no parser
Q15MCQ1 markAnnotated tree
In the annotated parse tree of float a, b, c with the declaration SDD, L.inh at every L node equals:
- (A) integer
- (B) float
- (C) the entry of the id
- (D) undefined
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (B)1 mark
Their values come from the lexer.
- (A) is wrong: Terminals have no children or rules of their own.
- (C) is wrong: Only synthesized.
- (D) is wrong: lexval is an attribute.
Re-read: Lesson 3.1 — Syntax-directed definitions: attributes, semantic rules, synthesized vs inherited, annotated parse trees, dependency graphs and evaluation order
Q2 Answer: 1602 marks
(2 × (3 + 5)) × (6 + 4) = 160.
Re-read: Lesson 3.1 — Syntax-directed definitions: attributes, semantic rules, synthesized vs inherited, annotated parse trees, dependency graphs and evaluation order
Q3 Answer: 262 marks
Tree: (2 + 3) * (4 + 5) with + multiplying and * adding: 6 + 20 = 26.
Re-read: Lesson 3.1 — Syntax-directed definitions: attributes, semantic rules, synthesized vs inherited, annotated parse trees, dependency graphs and evaluation order
Q4 Answer: (B)1 mark
Every used attribute must be computed before the one that uses it.
- (A) is wrong: Unrelated.
- (C) is wrong: Not in general.
- (D) is wrong: Must follow the edges.
Re-read: Lesson 3.1 — Syntax-directed definitions: attributes, semantic rules, synthesized vs inherited, annotated parse trees, dependency graphs and evaluation order
Q5 Answer: (A)1 mark
S ⊂ L.
- (B) is wrong: Never circular.
- (C) is wrong: LR evaluates them directly.
- (D) is wrong: Synthesized-only.
Re-read: Lesson 3.2 — S-attributed and L-attributed definitions; which parsers can evaluate which
Q6 Answer: (C)2 marks
D’s inherited attribute from left siblings only.
- (A) is wrong: Right sibling.
- (B) is wrong: Parent’s synthesized.
- (D) is wrong: Right sibling.
Re-read: Lesson 3.2 — S-attributed and L-attributed definitions; which parsers can evaluate which
Q7 Answer: 191 mark
15 + 4 = 19.
Re-read: Lesson 3.2 — S-attributed and L-attributed definitions; which parsers can evaluate which
Q8 Answer: 231312 marks
23131 (computed).
Re-read: Lesson 3.3 — Translation schemes with embedded actions; tracing what an SDT prints or computes
Q9 Answer: 344321112 marks
34432111 (computed).
Re-read: Lesson 3.3 — Translation schemes with embedded actions; tracing what an SDT prints or computes
Q10 Answer: (B)1 mark
Each operator after its right operand.
- (A) is wrong: Operators come after operands.
- (C) is wrong: Operators are moved.
- (D) is wrong: Not a number base.
Re-read: Lesson 3.3 — Translation schemes with embedded actions; tracing what an SDT prints or computes
Q11 Answer: (A) (B) (D)2 marks
Position decides the output.
Re-read: Lesson 3.3 — Translation schemes with embedded actions; tracing what an SDT prints or computes
Q12 Answer: 51 mark
3 leaves + 2 operators.
Re-read: Lesson 3.4 — Applications: building syntax trees; basic type checking (brief)
Q13 Answer: (B)1 mark
int widened to float.
- (A) is wrong: x is float.
- (C) is wrong: Coercion makes it legal.
- (D) is wrong: Not a char.
Re-read: Lesson 3.4 — Applications: building syntax trees; basic type checking (brief)
Q14 Answer: (A)2 marks
It is L-attributed: parameters and return values.
- (B) is wrong: One pass suffices.
- (C) is wrong: The lexer has no tree.
- (D) is wrong: Top-down parsers can.
Re-read: Lesson 3.2 — S-attributed and L-attributed definitions; which parsers can evaluate which
Q15 Answer: (B)1 mark
Handed down from T.type = float.
- (A) is wrong: The keyword is float.
- (C) is wrong: That is id.entry.
- (D) is wrong: It is defined everywhere.
Re-read: Lesson 3.1 — Syntax-directed definitions: attributes, semantic rules, synthesized vs inherited, annotated parse trees, dependency graphs and evaluation order
Wrong on Q → re-read
| Question | Topic | Re-read |
|---|---|---|
| Q1 | Attributes | Lesson 3.1 |
| Q2 | Evaluation | Lesson 3.1 |
| Q3 | Evaluation | Lesson 3.1 |
| Q4 | Dependency graph | Lesson 3.1 |
| Q5 | Classes | Lesson 3.2 |
| Q6 | L-attributed | Lesson 3.2 |
| Q7 | Value stack | Lesson 3.2 |
| Q8 | Trace | Lesson 3.3 |
| Q9 | Trace | Lesson 3.3 |
| Q10 | Postfix | Lesson 3.3 |
| Q11 | Translation schemes | Lesson 3.3 |
| Q12 | Syntax tree | Lesson 3.4 |
| Q13 | Types | Lesson 3.4 |
| Q14 | Parsers | Lesson 3.2 |
| Q15 | Annotated tree | Lesson 3.1 |