23
Parsing ³ Part II (Top-down parsing, left-recursion removal) Copyright 200 3, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.

Lec 09-Left Recursion Removal

Embed Size (px)

Citation preview

Page 1: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 1/23

Parsing ³ Part II(Top-down parsing, left-recursion removal)

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.Students enrolled in Comp 412 at Rice University have explicit permission to make copies of thesematerials for their personal use.

Page 2: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 2/23

Parsing Techniques

Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves

Pick a production & try to match the input

Bad ´pickµ may need to backtrack

Some grammars are backtrack-free (predictive parsing)

Bottom-up parsers (LR(1), operator precedence)

Start at the leaves and grow toward root

As input is consumed, encode possibilities in an internal state

Start in a state valid for legal first tokens

Bottom-up parsers handle a large(r) class of grammars

Page 3: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 3/23

A top-down parser starts with the root of the parse treeThe root node is labeled with the goal symbol of the grammar

Top-down parsing algorithm: Construct the root node of the parse tree

Repeat until the fringe of the parse tree matches the input string:

At a node labeled with non-terminal  A, select a production with  A on

its lhs and , for each symbol on its rhs, construct the appropriate

child 

When a terminal symbol is added to the fringe and it doesn¶t match

the fringe, backtrack 

Find the next node to be expanded  (label   NT)

The key is picking the right production in step 1p That choice should  be guided  by  the input string 

Top-do wn Parsing 

Page 4: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 4/23

Remember the expression grammar?

1 Goal  p E xprE xpr p E xpr + erm

| E xpr ² erm

| erm5 erm p erm * Fa tor6 | erm / Fa tor7 | Fa tor8 Fa tor p n mber

9 | i

An the inp t x ² * y

Version with pre e en

e erive l ast l e t re

Page 5: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 5/23

Let·s try x ² 2 * y : 

Example

le e te ti al F rm p t

G al ox ² 2 * y

1 Expr ox ² 2 * y

2 Expr + erm ox ² 2 * yerm + erm ox ² 2 * y

7 Fa

t r + erm ox ² 2 * y

9 <i  ,x> + erm ox ² 2 * y

9 <i  ,x> + erm x o² 2 * y

Goal 

Expr 

Term +Expr 

Term 

Fact.

<id,x >

Leftmost  derivation, choose productions  in  an  order  that  e xposes problems 

Page 6: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 6/23

Let·s try x ² 2 * y : 

This worked well, except that ´²µ doesn·t match ´+µThe parser must backtrack to here

Example

ule entential Form nput

Goal ox ² 2 * y

1 Expr ox ² 2 * y

2 Expr + Term ox ² 2 * yTerm + Term ox ² 2 * y

7 Factor + Term ox ² 2 * y

9 <id,x> + Term ox ² 2 * y

9 <id,x> + Term x o² 2 * y

Goal 

Expr 

Term +Expr 

Term 

Fact.

<id,x >

Page 7: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 7/23

Example

Continuing with x ² 2 * y : Ru l S nt nti l r In  u t 

 ³ l  ox ² 2 * y

1 xp r  ox ² 2 * y

3 xp r ² r  ox ² 2 * y

4 r ² r   ox ² 2 * y

7 t r ² r   ox ² 2 * y

9 i ,x> ² r  ox ² 2 * y

9 i ,x> ² r  x o ² 2 * y

 ³ i ,x> ² r  x ² o2 * y

G   

¡   l 

¢   

xp r 

£ ¤  r ¥    ² 

¢   

xp r 

£ ¤  r ¥   

¦ ¡ §  t.

 ̈  i ,x>

Page 8: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 8/23

Page 9: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 9/23

Example

Trying to match the ́ 2µ in x ² 2 * y : 

le entential Form np t

<i ,x> ² Term x ² o2 * y

7 <i ,x> ² Factor x ² o2 * y

9 <i ,x> ² <n m,2> x ² o2 * y<i ,x> ² <n m,2> x ² 2 o* y

Goal 

Expr 

Term  ² Expr 

Term 

Fact.

<id,x >

Fact.

<num, >

Page 10: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 10/23

Example

Trying to match the ́ 2µ in x ² 2 * y : 

Where are we?

´2µ matches ´2µ

We have more input, but no NT s left to expand

The expansion terminated too soon

Need to backtrack

Goal 

Expr 

Term -Expr 

Term 

Fact.

<id,x >

Fact.

<num, >

Ru l S t ti l  F  r I p u t 

³ <i  , > ² r x ² o2 *

7 <i ,x> ² F  t r x ² o2 *

9 <i ,x> ² < ,2> x ² o2 *³ <i ,x> ² < ,2> x ² 2 o*

Page 11: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 11/23

Rule entential   orm n ut  

³ <i d,x> ² T erm  x ² o2 * y

<i d,x> ² T erm * actor  x ² o2 * y

<i d,x> ² actor * actor  x ² o2 * y<i d,x> ² <num ,2> * actor  x ² o2 * y

³ <i d,x> ² <num ,2> * actor  x ² 2 o* y

³ <i d,x> ² <num ,2> * actor  x ² 2 * o y

<i d,x> ² <num ,2> * <i d,y> x ² 2 * o y

³ <i d,x> ² <num ,2> * <i d,y> x ² 2 * yo

Exam le 

T r  yin g a gain wit h ´2µ in x ² 2 * y :  

Thi s time , we  matc he d & con sume d all  t he  in ut 

ucce ss!

Goal 

Expr 

Term  ² Expr 

Term 

Fact.

<id,x>

Fact.

<id, y>

Term 

Fact.

<num,2>

*

Page 12: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 12/23

Page 13: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 13/23

Left Recursion

Top-d own pa rsers ca nnot ha ndl e l eft-recursiv e g ramma rs

ormall  y,

A  g ramma r is l eft recursiv e if A NT such tha t

a  d eriva tion A + AE, for som e strin g E (NT  T )*

Our expression g ramma r is l eft recursiv e

This can lead to non-termination in a top-down parser

or a top-down parser, any recursion must be right recursion

We would like to convert the left recursion to right recursion

Non-termination is a bad property in any part of a compiler

Page 14: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 14/23

Eliminating Left Recursion

To remove left recursion, we can transform the grammar

Consider a grammar fragment of the form: 

ee p ee E| F

where neither E nor F start with ee.

Note that this means F followed by zero or more E.

So we can rewrite it as: ee p F ie

ie p E ie

| Iwhere ie is a new non-terminal.

This accepts the same language, but uses only right recursion

Page 15: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 15/23

Eliminating Left Recursion

The expression grammar contains two cases of left recursion

Applying the transformation yields

These fragments use only right recursion

They retain the original left associativity

Expr p Expr + Term

| Expr ² Term

| Term

Term p Term * actor

| Term / actor

| actor

Expr p Term Exprd

Exprd | + Term Exprd

| ² Term Exprd

|  I

Term p actor Termd

Termd | * actor Termd

| / actor Termd

| I

Page 16: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 16/23

Eliminating Left Recursion

Substituting them back into the grammar yields

This grammar is correct,if somewhat non-intuitive.

It is left associative, as wasthe original

A top-down parser willterminate using it.

A top-down parser mayneed to backtrack with it.

1 Goal p Expr2 Expr p Term Exprd

Exprd p + Term Exprd

| ² Term Exprd

5 |I

6 Term p Factor Termd

7 Termd p * FactorTermd

8 | / FactorTermd

9 | I

1 Factor p number11 | id12 | ( Expr )

Page 17: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 17/23

Eliminating Left Recursion

The transformation eliminates immediate left recursion

What about more general, indirect left recursion ?

The general algorithm: arrange the NTs into some order A1, A2 , «, An

for i n 1 to n

for s n 1 to i ² 1replace each production Ai p AsK with Ai p H 1KH 2 K~H k K ,

where As p H 1H 2 ~H k  are all the current productions for As

eliminate any  immediate left recursion on Ai

using the direct transformation

This assumes that the initial grammar has no cy cles (Ai  + Ai ),and no epsilon productions

And back 

Must start with 1 to ensure that

A1 p A1 F is transformed

Page 18: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 18/23

Eliminating Left Recursion

How does this algorithm work?

1. Impose arbitrary order on the non-terminals

2. Outer loop cycles through NT in order

3. Inner loop ensures that a production expanding Ai has nonon-terminal As in its rhs, for s < i

4. Last step in outer loop converts any direct recursion on Aito right recursion using the transformation showed earlier

. New non-terminals are added at the end of the order & haveno left recursion

At the start of the ith outer loop iterationor all k < i, no production that expands Ak contains a non-terminal

As in its rhs, for s < k

Page 19: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 19/23

Example

G p E 

E p E + T E p T 

T p E ~ T 

T p id

Order of symbols:  , E, T 

Page 20: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 20/23

Example

1. Ai = G 

G p E 

E p E + T E p T 

T p E ~ T 

T p id

Order of symbols:  , E, T 

Page 21: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 21/23

Example

1. Ai = G 

G p E 

E p E + T E p T 

T p E ~ T 

T p id

. Ai = E 

G p E 

E p T E' E' p + T E' 

E' p I

T p E ~ T 

T p id

Order of symbols:  , E, T 

Page 22: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 22/23

Example

1. Ai = G 

G p E 

E p E + T E p T 

T p E ~ T 

T p id

. Ai = E 

G p E 

E p T E' E' p + T E' 

E' p I

T p E ~ T 

T p id

. Ai = T, As = E 

G p E 

E p T E' E' p + T E' 

E' p I

T p T E' ~ T 

T p id

Order of symbols:  , E, T 

Go t   Al g rit hm

Page 23: Lec 09-Left Recursion Removal

8/8/2019 Lec 09-Left Recursion Removal

http://slidepdf.com/reader/full/lec-09-left-recursion-removal 23/23

Example

1. Ai = G 

G p E 

E p E + T E p T 

T p E ~ T 

T p id

. Ai = E 

G p E 

E p T E' E' p + T E' 

E' p I

T p E ~ T 

T p id

. Ai = T, As = E 

G p E 

E p T E' E' p + T E' 

E' p I

T p T E' ~ T 

T p id

. Ai = T 

G p E 

E p T E' E' p + T E' 

E' p I

T p id T' 

T' p E' ~ T T' 

T' p I

Order of symbols:  , E, T