Removing Left Recursion

Preview:

Citation preview

Left Recursion Removal and Left Factoring

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Recommended