GATE Computer ScienceVery hardGATE CS (PYQ pattern)
In 1: i = 0; 2: c = 2; 3: L1: if i >= n goto L3; 4: d = c * 3; 5: if i > k goto L2; 6: c = 2; 7: L2: e = d + 1; 8: f = a * b; 9: i = i + 1; 10: goto L1; 11: L3: return e the loop is statements 3 to 10. Using the textbook definition (every operand is a constant, has all its reaching definitions outside the loop, or has exactly one reaching definition, which is an invariant statement inside the loop), which statements are loop-invariant?
- A. 4, 6, 7 and 8
- B. 8 only
- C. 4, 6 and 8 only
- D. 6 and 8 only
Correct answer: D. 6 and 8 only
c = 2 (6) has only a constant operand and f = a * b (8) uses variables never assigned in the loop. d = c * 3 (4) sees two reaching definitions of c, statement 2 outside and statement 6 inside, so it fails the test even though both assign 2 (only constant propagation would discover that). e = d + 1 (7) depends on the non-invariant 4.
More Compiler Design questions
- L1 is the set of strings over {a, b} whose third symbol from the right is a; its minimal DFA has 8 states. L2 = L1 ∩ {strings of even…
- For the augmented grammar S′ → S; S → ( L ) | x; L → S | L , S, how many states does the canonical LR(1) automaton have, and in how many…
- G: S → T | S ; T; T → a | ( S ). Removing the left recursion gives G′: S → T R; R → ; T R | ε; T → a | ( S ). Which statements are true?…
- The grammar A → B a | A a | c; B → B b | A b | d is transformed by the standard algorithm that removes (indirect and immediate) left…
- SDD: D → T L { L.inh = T.type }; T → int { T.type = integer }; L → L1 , id { L1.inh = L.inh; addType(id.entry, L.inh) }; L → id {…
- Which of the following are true? I. Every LL(1) grammar is LR(1). II. Every LL(1) grammar is LALR(1). III. Every LALR(1) grammar is LR(1)…