removing left recursion

63
Left Recursion Removal and Left Factoring Left Recursion Removal and Left Factoring

Upload: kamba-giri-a

Post on 07-Mar-2015

121 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Removing Left Recursion

Left Recursion Removal and Left Factoring

Left Recursion Removal and Left Factoring

Page 2: Removing Left Recursion

Motivating example

I In this lecture we discuss techniques (that sometimes work) toconvert a grammar that is not LL(1) into an equivalentgrammar that is LL(1).

I - Consider the following grammar:exp → exp addop term | termaddop → + | −term→ term mulop factor | factormulop → ∗factor → ( exp ) | number

- This grammar is not LL(1) since number is in First(exp) andin First(term).

- Thus in the entry M[exp, number] in the LL(1) parsing tablewe will have the entries exp → exp addop term andexp → term

- The problem is the presence of the left recursive ruleexp → exp addop term | term.

- Thus in order to try to convert this grammar into an LL(1)grammar, we remove the left recursion from this grammar.

Left Recursion Removal and Left Factoring

Page 3: Removing Left Recursion

Motivating example

I In this lecture we discuss techniques (that sometimes work) toconvert a grammar that is not LL(1) into an equivalentgrammar that is LL(1).

I - Consider the following grammar:

exp → exp addop term | termaddop → + | −term→ term mulop factor | factormulop → ∗factor → ( exp ) | number

- This grammar is not LL(1) since number is in First(exp) andin First(term).

- Thus in the entry M[exp, number] in the LL(1) parsing tablewe will have the entries exp → exp addop term andexp → term

- The problem is the presence of the left recursive ruleexp → exp addop term | term.

- Thus in order to try to convert this grammar into an LL(1)grammar, we remove the left recursion from this grammar.

Left Recursion Removal and Left Factoring

Page 4: Removing Left Recursion

Motivating example

I In this lecture we discuss techniques (that sometimes work) toconvert a grammar that is not LL(1) into an equivalentgrammar that is LL(1).

I - Consider the following grammar:exp → exp addop term | termaddop → + | −term→ term mulop factor | factormulop → ∗factor → ( exp ) | number

- This grammar is not LL(1) since number is in First(exp) andin First(term).

- Thus in the entry M[exp, number] in the LL(1) parsing tablewe will have the entries exp → exp addop term andexp → term

- The problem is the presence of the left recursive ruleexp → exp addop term | term.

- Thus in order to try to convert this grammar into an LL(1)grammar, we remove the left recursion from this grammar.

Left Recursion Removal and Left Factoring

Page 5: Removing Left Recursion

Motivating example

I In this lecture we discuss techniques (that sometimes work) toconvert a grammar that is not LL(1) into an equivalentgrammar that is LL(1).

I - Consider the following grammar:exp → exp addop term | termaddop → + | −term→ term mulop factor | factormulop → ∗factor → ( exp ) | number

- This grammar is not LL(1) since number is in First(exp) andin First(term).

- Thus in the entry M[exp, number] in the LL(1) parsing tablewe will have the entries exp → exp addop term andexp → term

- The problem is the presence of the left recursive ruleexp → exp addop term | term.

- Thus in order to try to convert this grammar into an LL(1)grammar, we remove the left recursion from this grammar.

Left Recursion Removal and Left Factoring

Page 6: Removing Left Recursion

Motivating example

I In this lecture we discuss techniques (that sometimes work) toconvert a grammar that is not LL(1) into an equivalentgrammar that is LL(1).

I - Consider the following grammar:exp → exp addop term | termaddop → + | −term→ term mulop factor | factormulop → ∗factor → ( exp ) | number

- This grammar is not LL(1) since number is in First(exp) andin First(term).

- Thus in the entry M[exp, number] in the LL(1) parsing tablewe will have the entries exp → exp addop term andexp → term

- The problem is the presence of the left recursive ruleexp → exp addop term | term.

- Thus in order to try to convert this grammar into an LL(1)grammar, we remove the left recursion from this grammar.

Left Recursion Removal and Left Factoring

Page 7: Removing Left Recursion

Motivating example

I In this lecture we discuss techniques (that sometimes work) toconvert a grammar that is not LL(1) into an equivalentgrammar that is LL(1).

I - Consider the following grammar:exp → exp addop term | termaddop → + | −term→ term mulop factor | factormulop → ∗factor → ( exp ) | number

- This grammar is not LL(1) since number is in First(exp) andin First(term).

- Thus in the entry M[exp, number] in the LL(1) parsing tablewe will have the entries exp → exp addop term andexp → term

- The problem is the presence of the left recursive ruleexp → exp addop term | term.

- Thus in order to try to convert this grammar into an LL(1)grammar, we remove the left recursion from this grammar.

Left Recursion Removal and Left Factoring

Page 8: Removing Left Recursion

Motivating example

I In this lecture we discuss techniques (that sometimes work) toconvert a grammar that is not LL(1) into an equivalentgrammar that is LL(1).

I - Consider the following grammar:exp → exp addop term | termaddop → + | −term→ term mulop factor | factormulop → ∗factor → ( exp ) | number

- This grammar is not LL(1) since number is in First(exp) andin First(term).

- Thus in the entry M[exp, number] in the LL(1) parsing tablewe will have the entries exp → exp addop term andexp → term

- The problem is the presence of the left recursive ruleexp → exp addop term | term.

- Thus in order to try to convert this grammar into an LL(1)grammar, we remove the left recursion from this grammar.

Left Recursion Removal and Left Factoring

Page 9: Removing Left Recursion

Left Recursion and Right Recursion

I Before we look at the technique of removing left recursionfrom a grammar, we first discuss left recursion in general.

I Grammar rules in BNF provide for concatenation and choicebut no specific operation equivalent to the ∗ of regularexpressions are provided.

I We can obtain repetition by using for example rules of theformA→ Aa | a or A→ aA | aBoth these grammars generate {an|n ≥ 1}.

I We call the rule A→ Aa | a left recursive and A→ aA | aright recursive.

Left Recursion Removal and Left Factoring

Page 10: Removing Left Recursion

Left Recursion and Right Recursion

I Before we look at the technique of removing left recursionfrom a grammar, we first discuss left recursion in general.

I Grammar rules in BNF provide for concatenation and choicebut no specific operation equivalent to the ∗ of regularexpressions are provided.

I We can obtain repetition by using for example rules of theformA→ Aa | a or A→ aA | aBoth these grammars generate {an|n ≥ 1}.

I We call the rule A→ Aa | a left recursive and A→ aA | aright recursive.

Left Recursion Removal and Left Factoring

Page 11: Removing Left Recursion

Left Recursion and Right Recursion

I Before we look at the technique of removing left recursionfrom a grammar, we first discuss left recursion in general.

I Grammar rules in BNF provide for concatenation and choicebut no specific operation equivalent to the ∗ of regularexpressions are provided.

I We can obtain repetition by using for example rules of theform

A→ Aa | a or A→ aA | aBoth these grammars generate {an|n ≥ 1}.

I We call the rule A→ Aa | a left recursive and A→ aA | aright recursive.

Left Recursion Removal and Left Factoring

Page 12: Removing Left Recursion

Left Recursion and Right Recursion

I Before we look at the technique of removing left recursionfrom a grammar, we first discuss left recursion in general.

I Grammar rules in BNF provide for concatenation and choicebut no specific operation equivalent to the ∗ of regularexpressions are provided.

I We can obtain repetition by using for example rules of theformA→ Aa | a

or A→ aA | aBoth these grammars generate {an|n ≥ 1}.

I We call the rule A→ Aa | a left recursive and A→ aA | aright recursive.

Left Recursion Removal and Left Factoring

Page 13: Removing Left Recursion

Left Recursion and Right Recursion

I Before we look at the technique of removing left recursionfrom a grammar, we first discuss left recursion in general.

I Grammar rules in BNF provide for concatenation and choicebut no specific operation equivalent to the ∗ of regularexpressions are provided.

I We can obtain repetition by using for example rules of theformA→ Aa | a or

A→ aA | aBoth these grammars generate {an|n ≥ 1}.

I We call the rule A→ Aa | a left recursive and A→ aA | aright recursive.

Left Recursion Removal and Left Factoring

Page 14: Removing Left Recursion

Left Recursion and Right Recursion

I Before we look at the technique of removing left recursionfrom a grammar, we first discuss left recursion in general.

I Grammar rules in BNF provide for concatenation and choicebut no specific operation equivalent to the ∗ of regularexpressions are provided.

I We can obtain repetition by using for example rules of theformA→ Aa | a or A→ aA | a

Both these grammars generate {an|n ≥ 1}.I We call the rule A→ Aa | a left recursive and A→ aA | a

right recursive.

Left Recursion Removal and Left Factoring

Page 15: Removing Left Recursion

Left Recursion and Right Recursion

I Before we look at the technique of removing left recursionfrom a grammar, we first discuss left recursion in general.

I Grammar rules in BNF provide for concatenation and choicebut no specific operation equivalent to the ∗ of regularexpressions are provided.

I We can obtain repetition by using for example rules of theformA→ Aa | a or A→ aA | aBoth these grammars generate {an|n ≥ 1}.

I We call the rule A→ Aa | a left recursive and A→ aA | aright recursive.

Left Recursion Removal and Left Factoring

Page 16: Removing Left Recursion

Left Recursion and Right Recursion

I Before we look at the technique of removing left recursionfrom a grammar, we first discuss left recursion in general.

I Grammar rules in BNF provide for concatenation and choicebut no specific operation equivalent to the ∗ of regularexpressions are provided.

I We can obtain repetition by using for example rules of theformA→ Aa | a or A→ aA | aBoth these grammars generate {an|n ≥ 1}.

I We call the rule A→ Aa | a left recursive and A→ aA | aright recursive.

Left Recursion Removal and Left Factoring

Page 17: Removing Left Recursion

Left and right recursion continue

I In general, rules of the form

A→ Aα | β are called left recursiveand rules of the formA→ αA | β right recursive.

I Grammars equivalent to the regular expression a∗ are given bypause A→ Aa | ε or A→ aA | ε

Left Recursion Removal and Left Factoring

Page 18: Removing Left Recursion

Left and right recursion continue

I In general, rules of the formA→ Aα | β are called left recursive

and rules of the formA→ αA | β right recursive.

I Grammars equivalent to the regular expression a∗ are given bypause A→ Aa | ε or A→ aA | ε

Left Recursion Removal and Left Factoring

Page 19: Removing Left Recursion

Left and right recursion continue

I In general, rules of the formA→ Aα | β are called left recursiveand rules of the formA→ αA | β right recursive.

I Grammars equivalent to the regular expression a∗ are given bypause A→ Aa | ε

or A→ aA | ε

Left Recursion Removal and Left Factoring

Page 20: Removing Left Recursion

Left and right recursion continue

I In general, rules of the formA→ Aα | β are called left recursiveand rules of the formA→ αA | β right recursive.

I Grammars equivalent to the regular expression a∗ are given bypause A→ Aa | ε or

A→ aA | ε

Left Recursion Removal and Left Factoring

Page 21: Removing Left Recursion

Left and right recursion continue

I In general, rules of the formA→ Aα | β are called left recursiveand rules of the formA→ αA | β right recursive.

I Grammars equivalent to the regular expression a∗ are given bypause A→ Aa | ε or A→ aA | ε

Left Recursion Removal and Left Factoring

Page 22: Removing Left Recursion

Left and right recursion continue

I Notice that left recursive rules ensure that expressionsassociate on the left.

I The parse tree for the expression 34− 3− 42 in the grammar

exp → exp addop term | termaddop → + | −term → term mulop factor | factormulop → ∗factor → ( exp ) | number

is for example given by

Left Recursion Removal and Left Factoring

Page 23: Removing Left Recursion

Left and right recursion continue

I Notice that left recursive rules ensure that expressionsassociate on the left.

I The parse tree for the expression 34− 3− 42 in the grammar

exp → exp addop term | termaddop → + | −term → term mulop factor | factormulop → ∗factor → ( exp ) | number

is for example given by

Left Recursion Removal and Left Factoring

Page 24: Removing Left Recursion

Left and right recursion continue

I Notice that left recursive rules ensure that expressionsassociate on the left.

I The parse tree for the expression 34− 3− 42 in the grammar

exp → exp addop term | termaddop → + | −term → term mulop factor | factormulop → ∗factor → ( exp ) | number

is for example given by

Left Recursion Removal and Left Factoring

Page 25: Removing Left Recursion

Left and right recursion continue

I Notice that left recursive rules ensure that expressionsassociate on the left.

I The parse tree for the expression 34− 3− 42 in the grammar

exp → exp addop term | termaddop → + | −term → term mulop factor | factormulop → ∗factor → ( exp ) | number

is for example given by

Left Recursion Removal and Left Factoring

Page 26: Removing Left Recursion

Left and right recursion continue

I Notice that left recursive rules ensure that expressionsassociate on the left.

I The parse tree for the expression 34− 3− 42 in the grammar

exp → exp addop term | termaddop → + | −term → term mulop factor | factormulop → ∗factor → ( exp ) | number

is for example given by

Left Recursion Removal and Left Factoring

Page 27: Removing Left Recursion

Left Recursion Removal and Left Factoring

I In the rule

exp → exp + term | exp − term | term

we have immediate left recursion and in

A→ B a | A a | cB → B b | A b | d

we have indirect left recursion.

I We only consider how to remove immediate left recursion.

Left Recursion Removal and Left Factoring

Page 28: Removing Left Recursion

Left Recursion Removal and Left Factoring

I In the rule

exp → exp + term | exp − term | term

we have immediate left recursion and in

A→ B a | A a | cB → B b | A b | d

we have indirect left recursion.

I We only consider how to remove immediate left recursion.

Left Recursion Removal and Left Factoring

Page 29: Removing Left Recursion

Left Recursion Removal and Left Factoring

I In the rule

exp → exp + term | exp − term | term

we have immediate left recursion and in

A→ B a | A a | cB → B b | A b | d

we have indirect left recursion.

I We only consider how to remove immediate left recursion.

Left Recursion Removal and Left Factoring

Page 30: Removing Left Recursion

Left Recursion Removal and Left Factoring continue

I Consider again the rule

exp → exp addop term | term

We rewrite this rule as

exp → term exp′

exp′→ addop term exp

′| ε

to remove the left recursion.

I In general if we have productions of the form

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

we rewrite this as

A→ β1 A′| ... | βmA

A′→ α1 A

′| ... | αn A

′| ε

in order to remove the left recursion.

Left Recursion Removal and Left Factoring

Page 31: Removing Left Recursion

Left Recursion Removal and Left Factoring continue

I Consider again the rule

exp → exp addop term | term

We rewrite this rule as

exp → term exp′

exp′→ addop term exp

′| ε

to remove the left recursion.

I In general if we have productions of the form

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

we rewrite this as

A→ β1 A′| ... | βmA

A′→ α1 A

′| ... | αn A

′| ε

in order to remove the left recursion.

Left Recursion Removal and Left Factoring

Page 32: Removing Left Recursion

Left Recursion Removal and Left Factoring continue

I Consider again the rule

exp → exp addop term | term

We rewrite this rule as

exp → term exp′

exp′→ addop term exp

′| ε

to remove the left recursion.

I In general if we have productions of the form

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

we rewrite this as

A→ β1 A′| ... | βmA

A′→ α1 A

′| ... | αn A

′| ε

in order to remove the left recursion.

Left Recursion Removal and Left Factoring

Page 33: Removing Left Recursion

Left Recursion Removal and Left Factoring continue

I Consider again the rule

exp → exp addop term | term

We rewrite this rule as

exp → term exp′

exp′→ addop term exp

′| ε

to remove the left recursion.

I In general if we have productions of the form

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

we rewrite this as

A→ β1 A′| ... | βmA

A′→ α1 A

′| ... | αn A

′| ε

in order to remove the left recursion.

Left Recursion Removal and Left Factoring

Page 34: Removing Left Recursion

Left Recursion Removal and Left Factoring continue

I Consider again the rule

exp → exp addop term | term

We rewrite this rule as

exp → term exp′

exp′→ addop term exp

′| ε

to remove the left recursion.

I In general if we have productions of the form

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

we rewrite this as

A→ β1 A′| ... | βmA

A′→ α1 A

′| ... | αn A

′| ε

in order to remove the left recursion.

Left Recursion Removal and Left Factoring

Page 35: Removing Left Recursion

Left Recursion Removal and Left Factoring continue

I Consider again the rule

exp → exp addop term | term

We rewrite this rule as

exp → term exp′

exp′→ addop term exp

′| ε

to remove the left recursion.

I In general if we have productions of the form

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

we rewrite this as

A→ β1 A′| ... | βmA

A′→ α1 A

′| ... | αn A

′| ε

in order to remove the left recursion.

Left Recursion Removal and Left Factoring

Page 36: Removing Left Recursion

Left Recursion Removal and Left Factoring continue

I Consider again the rule

exp → exp addop term | term

We rewrite this rule as

exp → term exp′

exp′→ addop term exp

′| ε

to remove the left recursion.

I In general if we have productions of the form

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

we rewrite this as

A→ β1 A′| ... | βmA

A′→ α1 A

′| ... | αn A

′| ε

in order to remove the left recursion.

Left Recursion Removal and Left Factoring

Page 37: Removing Left Recursion

Left recursion removal continue

I If we remove the left recursion from the rule

exp → exp + term | exp − term | term

we obtain

exp → term exp′

exp′→ + term exp

′| − term exp

′| ε

Left Recursion Removal and Left Factoring

Page 38: Removing Left Recursion

Left recursion removal continue

I If we remove the left recursion from the rule

exp → exp + term | exp − term | term

we obtain

exp → term exp′

exp′→ + term exp

′| − term exp

′| ε

Left Recursion Removal and Left Factoring

Page 39: Removing Left Recursion

Left recursion removal continue

I If we remove the left recursion from the rule

exp → exp + term | exp − term | term

we obtain

exp → term exp′

exp′→ + term exp

′| − term exp

′| ε

Left Recursion Removal and Left Factoring

Page 40: Removing Left Recursion

Left recursion removal continue

I If we remove left recursion from the grammar

exp → exp addop term | termaddop → + | −term → term mulop factor | factormulop → ∗factor → ( exp ) | number

we obtain the grammar

exp → term exp′

exp′→ addop term exp

′| ε

addop → + | −term → factor term

term′→ mulop factor term

′| ε

mulop → ∗factor → ( exp ) | number

Left Recursion Removal and Left Factoring

Page 41: Removing Left Recursion

Left recursion removal continue

I If we remove left recursion from the grammar

exp → exp addop term | termaddop → + | −term → term mulop factor | factormulop → ∗factor → ( exp ) | number

we obtain the grammar

exp → term exp′

exp′→ addop term exp

′| ε

addop → + | −term → factor term

term′→ mulop factor term

′| ε

mulop → ∗factor → ( exp ) | number

Left Recursion Removal and Left Factoring

Page 42: Removing Left Recursion

Left recursion removal continue

I If we remove left recursion from the grammar

exp → exp addop term | termaddop → + | −term → term mulop factor | factormulop → ∗factor → ( exp ) | number

we obtain the grammar

exp → term exp′

exp′→ addop term exp

′| ε

addop → + | −term → factor term

term′→ mulop factor term

′| ε

mulop → ∗factor → ( exp ) | number

Left Recursion Removal and Left Factoring

Page 43: Removing Left Recursion

Left recursion removal continue

I Now consider the parse tree for 3− 4− 5

I This tree no longer expresses the left associativity ofsubtraction.

I Nevertheless, a parser should still construct the appropriateleft associative syntax tree.

Left Recursion Removal and Left Factoring

Page 44: Removing Left Recursion

Left recursion removal continue

I Now consider the parse tree for 3− 4− 5

I This tree no longer expresses the left associativity ofsubtraction.

I Nevertheless, a parser should still construct the appropriateleft associative syntax tree.

Left Recursion Removal and Left Factoring

Page 45: Removing Left Recursion

Left recursion removal continue

I Now consider the parse tree for 3− 4− 5

I This tree no longer expresses the left associativity ofsubtraction.

I Nevertheless, a parser should still construct the appropriateleft associative syntax tree.

Left Recursion Removal and Left Factoring

Page 46: Removing Left Recursion

Left recursion removal continue

I Now consider the parse tree for 3− 4− 5

I This tree no longer expresses the left associativity ofsubtraction.

I Nevertheless, a parser should still construct the appropriateleft associative syntax tree.

Left Recursion Removal and Left Factoring

Page 47: Removing Left Recursion

Left recursion removal continue

I We obtain the syntax tree by removing all the unnecessaryinformation from the parse tree. A parser will usuallyconstruct a syntax tree and not a parse tree.

Left Recursion Removal and Left Factoring

Page 48: Removing Left Recursion

Left recursion removal continue

I We obtain the syntax tree by removing all the unnecessaryinformation from the parse tree. A parser will usuallyconstruct a syntax tree and not a parse tree.

Left Recursion Removal and Left Factoring

Page 49: Removing Left Recursion

Left Factoring

I Left factoring is required when two or more grammar rulechoices share a common prefix string, as in the rule

A→ α β | α γ

I Obviously, an LL(1) parser cannot distinguish between theproduction choices in such a situation.

I In the following example we have exactly this problem:

if -stmt → if ( exp ) statement | if ( exp ) statement else statement

Left Recursion Removal and Left Factoring

Page 50: Removing Left Recursion

Left Factoring

I Left factoring is required when two or more grammar rulechoices share a common prefix string, as in the rule

A→ α β | α γ

I Obviously, an LL(1) parser cannot distinguish between theproduction choices in such a situation.

I In the following example we have exactly this problem:

if -stmt → if ( exp ) statement | if ( exp ) statement else statement

Left Recursion Removal and Left Factoring

Page 51: Removing Left Recursion

Left Factoring

I Left factoring is required when two or more grammar rulechoices share a common prefix string, as in the rule

A→ α β | α γ

I Obviously, an LL(1) parser cannot distinguish between theproduction choices in such a situation.

I In the following example we have exactly this problem:

if -stmt → if ( exp ) statement | if ( exp ) statement else statement

Left Recursion Removal and Left Factoring

Page 52: Removing Left Recursion

Left Factoring

I Left factoring is required when two or more grammar rulechoices share a common prefix string, as in the rule

A→ α β | α γ

I Obviously, an LL(1) parser cannot distinguish between theproduction choices in such a situation.

I In the following example we have exactly this problem:

if -stmt → if ( exp ) statement | if ( exp ) statement else statement

Left Recursion Removal and Left Factoring

Page 53: Removing Left Recursion

Left Factoring

I Left factoring is required when two or more grammar rulechoices share a common prefix string, as in the rule

A→ α β | α γ

I Obviously, an LL(1) parser cannot distinguish between theproduction choices in such a situation.

I In the following example we have exactly this problem:

if -stmt → if ( exp ) statement | if ( exp ) statement else statement

Left Recursion Removal and Left Factoring

Page 54: Removing Left Recursion

Left factoring continue

I Consider the following grammar of if-statements:

if -stmt → if ( exp ) statement | if ( exp ) statement else statement

The left factored form of this grammar is

if -stmt → if ( exp ) statement else-partelse-part → else statement | ε

Left Recursion Removal and Left Factoring

Page 55: Removing Left Recursion

Left factoring continue

I Consider the following grammar of if-statements:

if -stmt → if ( exp ) statement | if ( exp ) statement else statement

The left factored form of this grammar is

if -stmt → if ( exp ) statement else-partelse-part → else statement | ε

Left Recursion Removal and Left Factoring

Page 56: Removing Left Recursion

Left factoring continue

I Consider the following grammar of if-statements:

if -stmt → if ( exp ) statement | if ( exp ) statement else statement

The left factored form of this grammar is

if -stmt → if ( exp ) statement else-partelse-part → else statement | ε

Left Recursion Removal and Left Factoring

Page 57: Removing Left Recursion

Left factoring continue

I Consider the following grammar of if-statements:

if -stmt → if ( exp ) statement | if ( exp ) statement else statement

The left factored form of this grammar is

if -stmt → if ( exp ) statement else-partelse-part → else statement | ε

Left Recursion Removal and Left Factoring

Page 58: Removing Left Recursion

Left factoring continue

I Here is a typical example where a programming language failsto be LL(1):

statement → assign-stmt | call-stmt | otherassign-stmt → identifier := expcall-stmt → identifier ( exp-list )

I This grammar is not in a form that can be left factored. Wemust first replace assign-stmt and call-stmt by the right-handsides of their defining productions:

statement → identifier := exp | identifier ( exp-list ) | other

I Then we left factor to obtain:

statement → identifier statement′| other

statement′→ := exp | ( exp-list )

I Note how this obscures the semantics of call and assignmentby separating the identifier from the actual call or assignaction.

Left Recursion Removal and Left Factoring

Page 59: Removing Left Recursion

Left factoring continue

I Here is a typical example where a programming language failsto be LL(1):

statement → assign-stmt | call-stmt | otherassign-stmt → identifier := expcall-stmt → identifier ( exp-list )

I This grammar is not in a form that can be left factored. Wemust first replace assign-stmt and call-stmt by the right-handsides of their defining productions:

statement → identifier := exp | identifier ( exp-list ) | other

I Then we left factor to obtain:

statement → identifier statement′| other

statement′→ := exp | ( exp-list )

I Note how this obscures the semantics of call and assignmentby separating the identifier from the actual call or assignaction.

Left Recursion Removal and Left Factoring

Page 60: Removing Left Recursion

Left factoring continue

I Here is a typical example where a programming language failsto be LL(1):

statement → assign-stmt | call-stmt | otherassign-stmt → identifier := expcall-stmt → identifier ( exp-list )

I This grammar is not in a form that can be left factored. Wemust first replace assign-stmt and call-stmt by the right-handsides of their defining productions:

statement → identifier := exp | identifier ( exp-list ) | other

I Then we left factor to obtain:

statement → identifier statement′| other

statement′→ := exp | ( exp-list )

I Note how this obscures the semantics of call and assignmentby separating the identifier from the actual call or assignaction.

Left Recursion Removal and Left Factoring

Page 61: Removing Left Recursion

Left factoring continue

I Here is a typical example where a programming language failsto be LL(1):

statement → assign-stmt | call-stmt | otherassign-stmt → identifier := expcall-stmt → identifier ( exp-list )

I This grammar is not in a form that can be left factored. Wemust first replace assign-stmt and call-stmt by the right-handsides of their defining productions:

statement → identifier := exp | identifier ( exp-list ) | other

I Then we left factor to obtain:

statement → identifier statement′| other

statement′→ := exp | ( exp-list )

I Note how this obscures the semantics of call and assignmentby separating the identifier from the actual call or assignaction.

Left Recursion Removal and Left Factoring

Page 62: Removing Left Recursion

Left factoring continue

I Here is a typical example where a programming language failsto be LL(1):

statement → assign-stmt | call-stmt | otherassign-stmt → identifier := expcall-stmt → identifier ( exp-list )

I This grammar is not in a form that can be left factored. Wemust first replace assign-stmt and call-stmt by the right-handsides of their defining productions:

statement → identifier := exp | identifier ( exp-list ) | other

I Then we left factor to obtain:

statement → identifier statement′| other

statement′→ := exp | ( exp-list )

I Note how this obscures the semantics of call and assignmentby separating the identifier from the actual call or assignaction.

Left Recursion Removal and Left Factoring

Page 63: Removing Left Recursion

Left factoring continue

I Here is a typical example where a programming language failsto be LL(1):

statement → assign-stmt | call-stmt | otherassign-stmt → identifier := expcall-stmt → identifier ( exp-list )

I This grammar is not in a form that can be left factored. Wemust first replace assign-stmt and call-stmt by the right-handsides of their defining productions:

statement → identifier := exp | identifier ( exp-list ) | other

I Then we left factor to obtain:

statement → identifier statement′| other

statement′→ := exp | ( exp-list )

I Note how this obscures the semantics of call and assignmentby separating the identifier from the actual call or assignaction.

Left Recursion Removal and Left Factoring