27
COMP2012/G52LAC Languages and Computation Lecture 13 Recursive-Descent Parsing: Elimination of Left Recursion Henrik Nilsson University of Nottingham, UK COMP2012/G52LACLanguages and ComputationLecture 13 – p.1/18

COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

COMP2012/G52LAC

Languages and ComputationLecture 13

Recursive-Descent Parsing:Elimination of Left Recursion

Henrik Nilsson

University of Nottingham, UK

COMP2012/G52LACLanguages and ComputationLecture 13 – p.1/18

Page 2: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

This Lecture

• The problem of recursive-descent parsingand left recursive grammars.

• Elimination of left recursion.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.2/18

Page 3: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Left Recursion

Consider: A → Aa | ǫ

COMP2012/G52LACLanguages and ComputationLecture 13 – p.3/18

Page 4: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Left Recursion

Consider: A → Aa | ǫ

Parsing function:

parseA :: [Token] -> Maybe [Token]

parseA ts =

case parseA ts of

Just (’a’ : ts’) -> Just ts’

_ -> Just ts

COMP2012/G52LACLanguages and ComputationLecture 13 – p.3/18

Page 5: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Left Recursion

Consider: A → Aa | ǫ

Parsing function:

parseA :: [Token] -> Maybe [Token]

parseA ts =

case parseA ts of

Just (’a’ : ts’) -> Just ts’

_ -> Just ts

Any problem?

COMP2012/G52LACLanguages and ComputationLecture 13 – p.3/18

Page 6: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Left Recursion

Consider: A → Aa | ǫ

Parsing function:

parseA :: [Token] -> Maybe [Token]

parseA ts =

case parseA ts of

Just (’a’ : ts’) -> Just ts’

_ -> Just ts

Any problem?

Would loop! Recursive-descent parsers cannot(easily) deal with left-recursive grammars.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.3/18

Page 7: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Elimination of Left Recursion (1)

• A grammar is left-recursive if there is some

non-terminal A such that A+⇒ Aα.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.4/18

Page 8: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

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 handleleft-recursive grammars.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.4/18

Page 9: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

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 handleleft-recursive grammars.

• If we want to use such a parsing method forparsing a language L = L(G) given by aleft-recursive grammar G, then the grammarfirst has to be transformed into an equivalentgrammar G′ that is not left-recursive.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.4/18

Page 10: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Recap: Equivalence of Grammars

Two grammars G1 and G2 are equivalent iffL(G1) = L(G2).

Example:

G1:S → ǫ | A

A → a | aAG2:

S → A

A → ǫ | Aa

L(G1) = {a}∗ = L(G2)

(The equivalence of CFGs is in generalundecidable.)

COMP2012/G52LACLanguages and ComputationLecture 13 – p.5/18

Page 11: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Elimination of Left Recursion (2)

• We will first consider immediate leftrecursion; i.e., productions of the form

A → Aα

We will further assume that α cannot derive ǫ.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.6/18

Page 12: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Elimination of Left Recursion (2)

• We will first consider immediate leftrecursion; i.e., productions of the form

A → Aα

We will further assume that α cannot derive ǫ.

• Key idea: A → β | Aα and A → β(α)∗ areequivalent.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.6/18

Page 13: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Elimination of Left Recursion (2)

• We will first consider immediate leftrecursion; i.e., productions of the form

A → Aα

We will further assume that α cannot derive ǫ.

• Key idea: A → β | Aα and A → β(α)∗ areequivalent.

• The latter can be expressed as:

A → βA′

A′ → αA′ | ǫ

where A′ is a new nonterminal (name arbitrary).

COMP2012/G52LACLanguages and ComputationLecture 13 – p.6/18

Page 14: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Exercise

• The following grammar G1 is immediatelyleft-recursive:

A → b | Aa

Draw the derivation tree for baa using G1.

• The following is a non-left-recursive grammarG′

1 equivalent to G1:

A → bA′

A′ → aA′ | ǫ

Draw the derivation tree for baa using G′1.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.7/18

Page 15: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Elimination of Left Recursion (3)

For each nonterminal A defined by some left-recursive production, group the productions for A

A → Aα1 | Aα2 | . . . | Aαm | β1 | β2 | . . . | βn

such that no βi begins with an A.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.8/18

Page 16: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Elimination of Left Recursion (3)

For each nonterminal A defined by some left-recursive production, group the productions for A

A → Aα1 | Aα2 | . . . | Aαm | β1 | β2 | . . . | βn

such that no βi begins with an A.

Then replace the A productions by

A → β1A′ | β2A

′ | . . . | βnA′

A′ → α1A′ | α2A

′ | . . . | αmA′ | ǫ

Assumption: no αi derives ǫ.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.8/18

Page 17: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

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

COMP2012/G52LACLanguages and ComputationLecture 13 – p.9/18

Page 18: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Elimination of Left Recursion (5)

Let us do a leftmost derivation of aabeeeecddbeec:

S ⇒ A

⇒ ABc

⇒ AAddBc

⇒ aAddBc

⇒ aABcddBc

⇒ aaBcddBc

⇒ aaBeecddBc

⇒ aaBeeeecddBc

⇒ aabeeeecddBc

⇒ aabeeeecddBeec

⇒ aabeeeecddbeec

COMP2012/G52LACLanguages and ComputationLecture 13 – p.10/18

Page 19: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Elimination of Left Recursion (6)

Here is the grammar again:

S → A | B

A → ABc | AAdd | a | aa

B → Bee | b

COMP2012/G52LACLanguages and ComputationLecture 13 – p.11/18

Page 20: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

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′ | ǫ

COMP2012/G52LACLanguages and ComputationLecture 13 – p.11/18

Page 21: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

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′ ⇒ aabeeeecddbeecCOMP2012/G52LACLanguages and ComputationLecture 13 – p.12/18

Page 22: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

General Left Recursion (1)

To eliminate general left recursion:

• first transform the grammar into animmediately left-recursive grammar throughsystematic substitution

• then proceed as before.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.13/18

Page 23: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Substitution

• An occurrence of a non-terminal in aright-hand side may be replaced by theright-hand sides of the productions for thatnon-terminal if done in all possible ways.

• All productions for non-terminals that, as aresult, cannot be reached from the startsymbol, can be eliminated.

(See e.g. the Typeset Lecture Notes section 8.3,or Aho, Sethi, and Ullman (1986) for details.)

COMP2012/G52LACLanguages and ComputationLecture 13 – p.14/18

Page 24: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

General Left Recursion (2)

For example, the generally left-recursive grammar

A → Ba

B → Ab | Ac | ǫ

is first transformed into the immediatelyleft-recursive grammar

A → Aba

A → Aca

A → a

COMP2012/G52LACLanguages and ComputationLecture 13 – p.15/18

Page 25: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Exercise

Transform the following generally left-recursivegrammar

A → BaB

B → Cb | ǫ

C → Ab | Ac

into an equivalent immediately left-recursivegrammar.

Then eliminate the left recursion.

COMP2012/G52LACLanguages and ComputationLecture 13 – p.16/18

Page 26: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Solution (1)

First:A → BaB

B → Abb |Acb | ǫ

Then:A → AbbaB | AcbaB | aB

B → Abb |Acb | ǫ

Or, eliminating B completely:

A → AbbaAbb | AcbaAbb | aAbb

| AbbaAcb | AcbaAcb | aAcb

| Abba | Acba | a

COMP2012/G52LACLanguages and ComputationLecture 13 – p.17/18

Page 27: COMP2012/G52LAC Languages and Computation Lecture 13psznhn/G52LAC/LectureNotes/lecture13.pdfElimination of Left Recursion (1) • A grammar is left-recursive if there is some non-terminal

Solution (2)

Let’s go with the smaller version (fewer productions):

A → AbbaB | AcbaB | aB

B → Abb |Acb | ǫ

Only productions for A are immediately left-recursive. Applying the elimination transformation:

A → aBA′

A′ → bbaBA′ | cbaBA′ | ǫ

B → Abb |Acb | ǫ

Note: A appears to the left in B-productions; yetgrammar no longer left-recursive. Why?

COMP2012/G52LACLanguages and ComputationLecture 13 – p.18/18