0% found this document useful (0 votes)
166 views26 pages

Elimination of Left Recursion in Parsing

This document discusses the elimination of left recursion in grammars. Left recursive grammars cannot be parsed using recursive descent parsing. The key steps are: 1) Identify productions with the form A → Aα as immediate left recursion 2) Replace these with A → βA' and A' → αA' | ε to eliminate left recursion 3) For general left recursion, first transform the grammar into an immediately left recursive form then apply the previous step.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views26 pages

Elimination of Left Recursion in Parsing

This document discusses the elimination of left recursion in grammars. Left recursive grammars cannot be parsed using recursive descent parsing. The key steps are: 1) Identify productions with the form A → Aα as immediate left recursion 2) Replace these with A → βA' and A' → αA' | ε to eliminate left recursion 3) For general left recursion, first transform the grammar into an immediately left recursive form then apply the previous step.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

G52MAL: Lecture 18

Recursive-Descent Parsing: Elimination of Left Recursion


Henrik Nilsson University of Nottingham, UK

G52MAL: Lecture 18 p. 1/17

This Lecture

The problem of recursive-descent parsing and left recursive grammars. Elimination of left recursion.

G52MAL: Lecture 18 p. 2/17

Left Recursion
Consider: A Aa |

G52MAL: Lecture 18 p. 3/17

Left Recursion
Consider: A Aa | Parsing function: parseA ts = case parseA ts of Just (a : ts) -> Just ts _ -> Just ts

G52MAL: Lecture 18 p. 3/17

Left Recursion
Consider: A Aa | Parsing function: parseA ts = case parseA ts of Just (a : ts) -> Just ts _ -> Just ts Any problem?

G52MAL: Lecture 18 p. 3/17

Left Recursion
Consider: A Aa | Parsing function: parseA ts = case parseA ts of Just (a : ts) -> Just ts _ -> Just ts Any problem? Would loop! Recursive-descent parsers cannot deal with left-recursive grammars.

G52MAL: Lecture 18 p. 3/17

Elimination of Left Recursion (1)

A grammar is left-recursive if there is some + non-terminal A such that A A.

G52MAL: Lecture 18 p. 4/17

Elimination of Left Recursion (1)

A grammar is left-recursive if there is some + non-terminal A such that A A. Certain parsing methods cannot handle left-recursive grammars.

G52MAL: Lecture 18 p. 4/17

Elimination of Left Recursion (1)

A grammar is left-recursive if there is some + non-terminal A such that A A. Certain parsing methods cannot handle left-recursive grammars. If we want to use such a parsing method for parsing a language L = L(G) given by a left-recursive grammar G, then the grammar rst has to be transformed into an equivalent grammar G that is not left-recursive.

G52MAL: Lecture 18 p. 4/17

Recap: Equivalence of Grammars


Two grammars G1 and G2 are equivalent iff L(G1 ) = L(G2 ). Example: G1 : S | A A a | aA L(G1 ) = {a} = L(G2 ) (The equivalence of CFGs is in general undecidable.)
G52MAL: Lecture 18 p. 5/17

G2 :

S A A | Aa

Elimination of Left Recursion (2)

We will rst consider immediate left recursion; i.e., productions of the form A A where cannot derive .

G52MAL: Lecture 18 p. 6/17

Elimination of Left Recursion (2)

We will rst consider immediate left recursion; i.e., productions of the form A A where cannot derive . Key idea: A | A and A () are equivalent.

G52MAL: Lecture 18 p. 6/17

Elimination of Left Recursion (2)

We will rst consider immediate left recursion; i.e., productions of the form A A where cannot derive . Key idea: A | A and A () are equivalent. The latter can be expressed as: A A A A |
G52MAL: Lecture 18 p. 6/17

Exercise

The following grammar G1 is immediately left-recursive: A b | Aa Draw the derivation tree for baa using G1 .

The following is a non-left-recursive grammar G1 equivalent to G1 : A bA A aA | Draw the derivation tree for baa using G1 .
G52MAL: Lecture 18 p. 7/17

Elimination of Left Recursion (3)


For each nonterminal A dened by some leftrecursive production, group the productions for A A A1 | A2 | . . . | Am | 1 | 2 | . . . | n such that no i begins with an A.

G52MAL: Lecture 18 p. 8/17

Elimination of Left Recursion (3)


For each nonterminal A dened by some leftrecursive production, group the productions for A A A1 | A2 | . . . | Am | 1 | 2 | . . . | n such that no i begins with an A. Then replace the A productions by A 1 A | 2 A | . . . | n A A 1 A | 2 A | . . . | m A | Assumption: no i derives .
G52MAL: Lecture 18 p. 8/17

Elimination of Left Recursion (4)


Consider the (immediately) left-recursive grammar: S A|B A ABc | AAdd | a | aa B Bee | b Terminal strings derivable from B include: b, bee, beeee, beeeeee Terminal strings derivable from A include: a, aa, aadd, aaadd, aaadddd, abc, aabc, abeec, aabeec, abeecbeec, aabeeeecddbeec
G52MAL: Lecture 18 p. 9/17

Elimination of Left Recursion (5)


Let us do a leftmost derivation of aabeeeecddbeec: SA ABc AAddBc aAddBc aABcddBc aaBcddBc aaBeecddBc aaBeeeecddBc aabeeeecddBc aabeeeecddBeec aabeeeecddbeec
G52MAL: Lecture 18 p. 10/17

Elimination of Left Recursion (6)


Here is the grammar again: S A|B A ABc | AAdd | a | aa B Bee | b

G52MAL: Lecture 18 p. 11/17

Elimination of Left Recursion (6)


Here is the grammar again: S A|B A ABc | AAdd | a | aa B Bee | b An equivalent right-recursive grammar: S A|B A aA | aaA A BcA | AddA | B bB B eeB |

G52MAL: Lecture 18 p. 11/17

Elimination of Left Recursion (7)


Derivation of aabeeeecddbeec in the new grammar: S A aA aAddA aaA ddA aaBcA ddA aabB cA ddA aabeeB cA ddA aabeeeeB cA ddA aabeeeecA ddA aabeeeecddA aabeeeecddBcA aabeeeecddbB cA aabeeeecddbeeB cA aabeeeecddbeecA aabeeeecddbeec
G52MAL: Lecture 18 p. 12/17

General Left Recursion (1)


To eliminate general left recursion:

rst transform the grammar into an immediately left-recursive grammar through systematic substitution then proceed as before.

G52MAL: Lecture 18 p. 13/17

Substitution

An occurrence of a non-terminal in a right-hand side may be replaced by the right-hand sides of the productions for that non-terminal if done in all possible ways. All productions for non-terminals that, as a result, cannot be reached from the start symbol, can be eliminated.

(See e.g. Aho, Sethi, and Ullman (1986) for details.)

G52MAL: Lecture 18 p. 14/17

General Left Recursion (2)


For example, the generally left-recursive grammar A Ba B Ab | Ac | is rst transformed into the immediately left-recursive grammar A Aba A Aca A a

G52MAL: Lecture 18 p. 15/17

Exercise
Transform the following generally left-recursive grammar A BaB B Cb | C Ab | Ac into an equivalent immediately left-recursive grammar.

G52MAL: Lecture 18 p. 16/17

Solution
First: A BaB B Abb |Acb | A AbbaB | AcbaB | aB B Abb |Acb |

Then:

Or, eliminating B completely: A AbbaAbb | AcbaAbb | aAbb | AbbaAcb | AcbaAcb | aAcb | Abba | Acba | a
G52MAL: Lecture 18 p. 17/17

You might also like