









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










th
Topic: Left-Recursion and Left Factoring Sakhi Bandyopadhyay Department of Computer Science and BCA Kharagpur College
Problem-1: Consider the following grammar and eliminate left recursion- A → AAα / β Solution: The grammar after eliminating left recursion is- A → βA’ A’ → AαA’ / ∈ Problem-2: Consider the following grammar and eliminate left recursion- S → S0S1S / 01 Solution: The grammar after eliminating left recursion is- S → 01A A → 0S1SA / ∈
Problem-3: Consider the following grammar and eliminate left recursion- A → ABd / Aa / a B → Be / b Solution: The grammar after eliminating left recursion is- A → aA’ A’ → BdA’ / aA’ / ∈ B → bB’ B’ → eB’ / ∈ Problem-4: Consider the following grammar and eliminate left recursion- E → E + E / E x E / a Solution: The grammar after eliminating left recursion is- E → aA A → +EA / xEA / ∈
Problem-1: Do left factoring in the following grammar- A → aAB / aBc / aAc A → aA’ A’ → AB / Bc / Ac A → aA’ A’ → AD / Bc D → B / c This is a left factored grammar.
Left Recursion: A grammar is left recursive if it has a nonterminal A such that there is a derivation A -> Aα | β where α and β are sequences of terminals and nonterminals that do not start with A. While designing a top down-parser, if the left recursion exist in the grammar then the parser falls in an infinite loop, here because A is trying to match A itself, which is not possible. We can eliminate the above left recursion by rewriting the offending production. As- A -> βA' A' -> αA' | epsilon Left Factoring: Left factoring is required to eliminate non-determinism of a grammar. Suppose a grammar, S -> abS | aSb Here, S is deriving the same terminal a in the production rule(two alternative choices for S), which follows non-determinism. We can rewrite the production to defer the decision of S as- S -> aS' S' -> bS | Sb Thus, S' can be replaced for bS or Sb
First(α) is a set of terminal symbols that begin in strings derived from α. Consider the production rule- A → abc / def / ghi Then, we have- First(A) = { a , d , g }
Follow(α) is a set of terminal symbols that appear immediately to the right of α.
∈ may appear in the first function of a non-terminal. ∈ will never appear in the follow function of a non-terminal. Before calculating the first and follow functions, eliminate Left Recursion from the grammar, if present.