🔀
Mixed set 3: ten questions from all phases in random order, as in the exam. Aim for 20 minutes. Every answer links back to the lesson to re-read.
Practice set
Q1MCQ1 markLexical errors
Which is detected by the lexical analyzer?
- (A) a missing semicolon
- (B) the character @ in a C identifier position
- (C) an undeclared variable
- (D) a type mismatch
Q2MCQ2 marksLeft recursion
Removing left recursion from A → A c | A a d | b d | ε gives:
- (A) A → b d A' | A'; A' → c A' | a d A' | ε
- (B) A → b d A'; A' → c A' | a d A'
- (C) A → A' b d; A' → c | a d | ε
- (D) A → b d | ε; A' → c A' | a d A'
Q3MCQ2 marksLALR
For S → a A d | b B d | a B e | b A e, A → c, B → c:
- (A) LALR(1) has no conflicts.
- (B) LALR(1) has reduce-reduce conflicts; CLR(1) has none.
- (C) CLR(1) has shift-reduce conflicts.
- (D) It is LL(1) but not LR(1).
Q4NAT1 markAmbiguity
In E → E + E | E * E | id, how many parse trees does id + id * id + id have?
Numerical answer: ________
Q5MCQ2 marksSDT
Same grammar as the postfix scheme, but each print action placed before the right side (E → {print("+")} E + T …, F → id {print(id.name)}). Output for a * b + c * d?
- (A) a b * c d * +
- (B) + * a b * c d
- (C) * + a b * c d
- (D) + a * b * c d
Q6MCQ1 markL-attributed
Which semantic rule for A → B C is allowed in an L-attributed definition?
- (A) B.i = C.s
- (B) C.i = B.s
- (C) B.i = A.s
- (D) A.i = C.s
Q7NAT2 marksBlocks
How many edges does the flow graph of this code have?
(1) x = 0
(2) i = 1
(3) if i > n goto (8)
(4) t = i * i
(5) x = x + t
(6) i = i + 1
(7) goto (3)
(8) y = x * 2
(9) if y > 100 goto (11)
(10) y = 0
(11) print y
Numerical answer: ________
Q8MCQ2 marksParameters
What does this print with call by value-result?
int i = 1; int a[3];
void f(int x) { i = i + 1; x = x + 5; }
main() { a[0] = 10; a[1] = 20; a[2] = 30; f(a[i]); print(a[0], a[1], a[2], i); }
- (A) 10 20 30 2
- (B) 10 25 30 2
- (C) 10 20 35 2
- (D) 10 25 35 2
Q9NAT1 markReaching
B1: x = 5, y = 1 → B2 (loops to itself): y = x + y, x = x − 1 → B3: z = y. How many definitions reach the start of B2?
Numerical answer: ________
Q10NAT1 markLoops
In a loop with i = i + 2 each iteration, the derived induction variable j = 4 * i + 3 changes by how much per iteration?
Numerical answer: ________
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (B)1 mark
@ cannot start any C token.
- (A) is wrong: Syntax error.
- (C) is wrong: Semantic error.
- (D) is wrong: Semantic error.
Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured
Q2 Answer: (A)2 marks
A → Aα | β becomes A → βA′, A′ → αA′ | ε, with β ∈ { b d, ε } (computed).
- (B) is wrong: Loses the ε alternatives.
- (C) is wrong: Still not in the right form.
- (D) is wrong: A′ is never reached.
Re-read: Lesson 2.2 — Grammar transformations: eliminating left recursion (direct and indirect); left factoring
Q3 Answer: (B)2 marks
The two states with A → c· and B → c· have the same core; merging them mixes lookaheads d and e (computed: 2 R/R conflicts).
- (A) is wrong: Merging creates conflicts.
- (C) is wrong: CLR is conflict-free here.
- (D) is wrong: It is CLR(1).
Re-read: Lesson 2.4.5 — LALR(1): merging states and which conflicts merging can create
Q4 Answer: 51 mark
Computed by counting: 5 (the number of ways to bracket 4 operands).
Re-read: Lesson 2.1.2 — Ambiguity; encoding operator precedence and associativity in a grammar
Q5 Answer: (B)2 marks
Actions at the start of a production print the operator first: prefix order (computed: + * a b * c d).
- (A) is wrong: Postfix.
- (C) is wrong: Wrong structure.
- (D) is wrong: Wrong grouping.
Re-read: Lesson 3.3 — Translation schemes with embedded actions; tracing what an SDT prints or computes
Q6 Answer: (B)1 mark
An inherited attribute may depend on the parent’s inherited attributes and on siblings to the left.
- (A) is wrong: C is to the right of B.
- (C) is wrong: A.s is not available yet.
- (D) is wrong: Rules define attributes of the right side, or A’s synthesized ones.
Re-read: Lesson 3.2 — S-attributed and L-attributed definitions; which parsers can evaluate which
Q7 Answer: 72 marks
Leaders 1, 3, 4, 8, 10, 11; edges B1→B2, B2→B4, B2→B3, B3→B2, B4→B6, B4→B5, B5→B6: 7.
Re-read: Lesson 4.4 — Basic blocks: leader rules and partitioning; control flow graphs (the second meaning of “CFG”); counting blocks and edges
Q8 Answer: (B)2 marks
The address of a[i] is fixed at the call (a[1]); x = 25 is copied back there (computed: 10 25 30 2).
- (A) is wrong: That is by value.
- (C) is wrong: That is by name (a[i] re-evaluated with i = 2).
- (D) is wrong: Only one element changes.
Re-read: Lesson 5.4 — Parameter passing: by value, by reference, by value-result, by name; output tracing
Q9 Answer: 41 mark
From B1: d1, d2; around the loop: d3, d4. 4 (computed).
Re-read: Lesson 6.2 — The data-flow analysis framework: gen/kill sets, in/out equations, forward vs backward, meet operator, iterating to a fixed point
Q10 Answer: 81 mark
4 × 2 = 8.
Re-read: Lesson 6.6 — Loop optimizations: code motion, induction variables (brief, low weight)