Which of the following is true about the standard procedure for converting a…
2012
Which of the following is true about the standard procedure for converting a CFG into an LL(1) grammar?
Answer: C. Both remove left recursion and factor the grammar — Concept — what makes a grammar LL(1). A grammar is LL(1) when a predictive top-down parser can choose the right production for every nonterminal from a single…
- A.
Remove left recursion alone
- B.
Factor the grammar alone
- C.
Both remove left recursion and factor the grammar
- D.
Neither remove left recursion nor factor the grammar
Attempted by 223 students.
Show answer & explanation
Correct answer: C
Concept — what makes a grammar LL(1). A grammar is LL(1) when a predictive top-down parser can choose the right production for every nonterminal from a single lookahead token. Formally, for any two distinct alternatives A → α | β of the same nonterminal, FIRST(α) and FIRST(β) must be disjoint, and if one of the two alternatives can derive ε, then the FIRST set of the other alternative must be disjoint from FOLLOW(A). Two structural defects break that test: left recursion, where a nonterminal derives a string that begins with itself, and a common prefix shared by two alternatives of the same nonterminal.
Applying the concept: the two standard repairs.
Eliminating left recursion: replace A → Aα | β by A → βA′ and A′ → αA′ | ε. Left recursion makes a predictive parser expand A into A again without consuming any input, so top-down parsing never terminates.
Left factoring: replace A → αβ | αγ by A → αA′ and A′ → β | γ. A shared prefix places the same tokens in the FIRST sets of two alternatives, so a single lookahead token cannot decide between them.
Cross-check — is one repair on its own the procedure? Each repair removes only its own defect, so neither one covers the conversion by itself. A grammar that has been left factored but is still left recursive still loops; a grammar free of left recursion but still sharing prefixes still has overlapping FIRST sets and a parsing-table cell holding two productions. The standard preparation therefore consists of both repairs, each applied wherever its defect actually occurs — and that is the statement that holds here.
Note. The two repairs are preparation, not a guarantee. A grammar that already shows neither defect needs neither repair, and some grammars — and some languages — stay non-LL(1) even after both. The conversion is settled only when the LL(1) parsing table has been built and no cell holds more than one production.
A video solution is available for this question — log in and enroll to watch it.