removing left recursion
TRANSCRIPT
![Page 1: Removing Left Recursion](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/1.jpg)
Left Recursion Removal and Left Factoring
Left Recursion Removal and Left Factoring
![Page 2: Removing Left Recursion](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/2.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/3.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/4.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/5.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/8.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/9.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/12.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/13.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/14.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/16.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/17.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/18.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/19.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/20.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/21.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/22.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/23.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/24.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/25.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/26.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/27.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/28.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/29.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/30.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/31.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/32.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/33.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/34.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/35.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/36.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/37.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/38.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/39.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/40.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/41.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/42.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/43.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/44.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/45.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/46.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/47.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/48.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/49.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/50.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/51.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/52.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/53.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/54.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/55.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/56.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/57.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/58.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/59.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/60.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/61.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/62.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042512/54fb91984a7959434c8b4ee6/html5/thumbnails/63.jpg)
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