In computer science, left recursion is a special case of recursion.
A formal grammar that contains left recursion cannot be parsed by a recursive descent parser. In contrast, left recursion is preferred for LALR parsers because it results in lower stack usage than right recursion.
Definition
“A grammar is left-recursive if we can find some non-terminal A which will eventually derive a sentential form with itself as the left-symbol.”Notes on Formal Language Theory and Parsing, James Power, Department of Computer Science National University of Ireland, Maynooth Maynooth, Co. Kildare, Ireland.JPR02
Immediate left recursion
Immediate left recursion occurs in rules of the form
<math>A \rightarrow A\alpha\,|\,\beta</math>
Where <math>\alpha</math> and <math>\beta</math> are sequences of nonterminals and terminals, and <math>\beta</math> doesn’t start with A.
Example :
The rule
<math>Expr \rightarrow Expr\,+\,Term</math>
is immediately left-recursive. The recursive descent parser for this rule might look like :
- function Expr() {
- Expr(); match(’+'); Term();
- }
and a recursive descent parser would fall into infinite recursion when trying to parse a grammar which contains this rule.
Indirect left recursion
Indirect left recursion in its simplest form could be defined as :
<math>A \rightarrow B\alpha\,|\,C</math>
<math>B \rightarrow A\beta\,|\,D</math>
Possibly giving the derivation <math>A \Rightarrow B\alpha \Rightarrow A\beta\alpha \Rightarrow … </math>
More generally, for the non-terminals <math>A_0, A_1, …, A_n</math>, indirect left recursion can be defined as being of the form :
<math>A_0 \rightarrow A_1\alpha_1\,|…</math>
<math>A_1 \rightarrow A_2\alpha_2\,|…</math>
<math>…</math>
<math>A_n \rightarrow A_0\alpha_{(n+1)}\,|…</math>
Where <math>\alpha_1, \alpha_2, …, \alpha_n</math> are sequences of nonterminals and terminals.
Removing left recursion
Removing immediate left recursion
The general algorithm to remove immediate left recursion follows. Several improvements to this method have been made, including the ones described in “Removing Left Recursion from Context-Free Grammars” Removing Left Recursion from Context-Free Grammars, written by Robert C. Moore.
For each rule of the form
<math>A \rightarrow A\alpha_1\,|\,…\,|\,A\alpha_n\,|\,\beta_1\,|\,…\,|\,\beta_m </math>
Where :
- A is a left-recursive nonterminal
- <math>\alpha</math> is a sequence of nonterminals and terminals that is not null (<math>\alpha \ne \epsilon </math>)
- <math>\beta</math> is a sequence of nonterminals and terminals that does not start with A.
Replace the A-production by the production :
<math>A \rightarrow \beta_1A^\prime\, |\, …\, |\, \beta_mA^\prime</math>
And create a new nonterminal
<math>A^\prime \rightarrow \epsilon\, |\, \alpha_1A^\prime\, |\, …\, |\, \alpha_nA^\prime</math>
This newly created symbol is often called the “tail”, or the “rest”.
Removing indirect left recursion
If the grammar has no <math>\epsilon</math>-productions (no productions of the form <math>A \rightarrow … | \epsilon | … </math>) and is not cyclic (no derivations of the form <math>A \Rightarrow … \Rightarrow A </math> for any nonterminal A), this general algorithm may be applied to remove indirect left recursion :
Arrange the nonterminals in some (any) fixed order <math>A_1</math>, … <math>A_n</math>.
- for i = 1 to n {
- for j = 1 to i – 1 {
-
- let the current <math>A_j</math> productions be
- <math>A_j \rightarrow \delta_1 | … | \delta_k</math>
- replace each production <math>A_i \rightarrow A_j \gamma</math> by
- <math>A_i \rightarrow \delta_1\gamma | … | \delta_k\gamma</math>
- remove direct left recursion for <math>A_i</math>
- }
- }
Pitfalls
The above transformations remove left-recursion by creating a right-recursive grammar; but this changes the associativity of our rules. Left recursion makes left associativity; right recursion makes right associativity.
Example :
We start out with a grammar :
<math>Expr \rightarrow Expr\,+\,Term\,|\,Term</math>
<math>Term \rightarrow Term\,*\,Factor\,|\,Factor</math>
<math>Factor \rightarrow (Expr)\,|\,Int</math>
After having applied standard transformations to remove left-recursion, we have the following grammar :
<math>Expr \rightarrow Term\ Expr’</math>
<math>Expr’ \rightarrow {} + Term\ Expr’\,|\,\epsilon</math>
<math>Term \rightarrow Factor\ Term’</math>
<math>Term’ \rightarrow {} * Factor\ Term’\,|\,\epsilon</math>
<math>Factor \rightarrow (Expr)\,|\,Int</math>
Parsing the string ‘a + a + a’ with the first grammar in an LALR parser (which can recognize left-recursive grammars) would have resulted in the parse tree :
Expr
/ \
Expr + Term
/ | \ \
Expr + Term Factor
| | |
Term Factor Int
| |
Factor Int
|
Int
This parse tree grows to the left, indicating that the ‘+’ operator is left associative, representing (a + a) + a.
But now that we’ve changed the grammar, our parse tree looks like this :
Expr ---
/ \
Term Expr' --
| / | \
Factor + Term Expr' ------
| | | \ \
Int Factor + Term Expr'
| | |
Int Factor <math>\epsilon</math>
|
Int
We can see that the tree grows to the right, representing a + ( a + a). We have changed the associativity of our operator ‘+’, it is now right-associative. While this isn’t a problem for the associativity of addition with addition it would have a significantly different value if this were subtraction.
The problem is that normal arithmetic requires left associativity. Several solutions are: (a) rewrite the grammar to be left recursive, or (b) rewrite the grammar with more nonterminals to force the correct precedence/associativity, or (c) if using YACC or Bison, there are operator declarations, %left, %right and %nonassoc, which tell the parser generator which associativity to force.
See also
External links
- http://www.cs.may.ie/~jpower/Courses/parsing/parsing.pdf
- http://www.cs.umd.edu/class/fall2002/cmsc430/lec4.pdf
- http://www.wvutech.edu/mclark/Systems%20Programming/Removing%20Left%20Recursion.pdf
- Practical Considerations for LALR(1) Grammars
References