GATE Computer ScienceVery hardGATE CS (PYQ pattern)
Sethi–Ullman labelling: a leaf is 1 as a left child and 0 as a right child (a right operand may be a memory operand); an interior node is max(l1, l2) if l1 ≠ l2, else l1 + 1. How many registers does a * (b + (c * (d + e))) need as written, and how many if the compiler may swap the operands of + and * (both commutative) to minimise the label?
- A. 3 as written, 2 after swapping
- B. 2 as written, 2 after swapping
- C. 3 as written, 1 after swapping
- D. 2 as written, 1 after swapping
Correct answer: D. 2 as written, 1 after swapping
As written: d + e = 1; c * (d + e) has c = 1 and 1 → 2; b + 2 → 2; a * 2 → 2. Swapping every operator turns the tree into the left-deep chain (((d + e) * c) + b) * a, where every right child is a memory leaf (0) and every node is max(1, 0) = 1: a single register suffices.
More Compiler Design questions
- Build the LL(1) table for E → T R; R → + T R | − T R | ε; T → F P; P → ^ T | ε; F → ( E ) | id (^ is right-associative exponent). How many…
- An LR parser for E → id | E [ E ] | ( E ) parses "id [ ( id [ id ] ) ]". What is the largest number of grammar symbols on its stack at any…
- S → ( S ) S | ε is both LL(1) and SLR(1). Both parsers process "( ( ) ( ) )". What is the largest number of grammar symbols on the LL(1)…
- Augment S → ( L ) | x; L → S | L , S with S′ → S and build the canonical collection of LR(0) item sets. How many states does it have, and…
- The grammar S → A B; A → a | ε; B → A b | c is not LL(1). Each change below is applied to it on its own. Which of them make the grammar…
- L is the set of strings over {a, b} such that in every prefix the number of a's and the number of b's differ by at most 2. How many states…