Top Banner
Left Recursion Removal and Left Factoring Left Recursion Removal and Left Factoring
63

Left Recursion Removal and Left Factoring

Jan 17, 2017

Download

Documents

phammien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Left Recursion Removal and Left Factoring

Left Recursion Removal and Left Factoring

Left Recursion Removal and Left Factoring

Page 2: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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: Left Recursion Removal and Left Factoring

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