Left Recursion - people.hsc.edu

Download Report

Transcript Left Recursion - people.hsc.edu

Left Recursion

Lecture 7 Fri, Feb 4, 2005

A Problem with Recursive Descent Parsers

 Suppose the grammar were

S

A B

|

C D A B C D

   

B A C A B A A C

|

C A

| | |

A D A D B D

|

a

|

b

 How could a top-down parser decide which production for

S

to use?

Another Problem with Recursive Descent Parsers

 Suppose the grammar were

S

S S | a

 How could the parser decide how many times to use the production

S

the production

S

a

?

S S

before using

Futile Attempt #1 void

{ } S() if (token == a) match(a); else { S(); S(); }

Futile Attempt #2 void

{ S() if (token != EOF) { S(); S(); } }

Left Recursion

 The method of recursive descent does not work if the grammar is

left recursive

.

 A

grammar

is left recursive if there is a derivation

A

 +

A

 for some nonterminal

A

and string  .

 In particular, a

production

is of the form

A

A

 .

is left recursive if it

Left Recursion

 Applying the method of recursive descent would lead to the function

void

{ A() A(); // Process  } which leads to infinite recursion.

Left Recursion

 Recall that in the earlier example, we added the production

S'

S S'

|  ,

not

the production

S'

S' S

|   Why?

 Are they equivalent as far as the language of the grammar is concerned?

Eliminating Left Recursion

 Left recursion in a production may be removed by transforming the grammar in the following way.

 Replace

A

A

 |  with

A A'

 

A'

 

A'

|  .

Eliminating Left Recursion

 Under the original productions, a derivation of  is

A

A

 

A

 

A

   .

 Under the new productions, a derivation of  is

A

 

A'

 

A'

 

A'

 

A'

 

.

Example: Eliminating Left Recursion

 Consider the left recursive grammar

E

E

+

T

|

T T F

  (

T E

*

F

) | |

id

F

 Apply the transformation to

E

:

E

T E' E'

 +

T E'

|  .

Example: Eliminating Left Recursion

 Then apply the transformation to T:

T

F T' T'

 *

F T'

|  .

Example: Eliminating Left Recursion

 Now the grammar is

E

T E' E' F

T T'

 

F T'

*

F T'

 ( +

T E' E

) |

id

|  |  .

.

Example: Eliminating Left Recursion

 The function for

E'

would be

void

{ Eprime()

if

{ (token == PLUS) match(PLUS); T(); Eprime(); }

return

; }

Advantages of Left Recursion

 A left recursive grammar is often more intuitive than the transformed grammar.

 A left recursive grammar will match expressions earlier, leading to shallow recursion.

 Consider parsing a + b + c + d + e.

 Bottom-up parsing takes advantage of the benefits of left recursion.

Example

 Consider the simple grammar

E

E

+

E

|

num

 Convert it to

E E'

  num E' +

E E'

|   Example: SimpleParser

Exercise

 The grammar

R

R

R

|

RR

|

R

* | (

R

) |

a

|

b

generates all regular expressions on the alphabet {

a

,

b

}.

 Rewrite the grammar to reflect the precedence rules.

 Eliminate left recursion.