You are on page 1of 9

Left-factoring and left-recursion removal

Left-factoring and left-recursion removal


Ian J. Hayes

April 2, 2012

April 2, 2012

1/1

Left-factoring and left-recursion removal

Left factoring productions

Left factoring grammar productions

Productions such as IfStmt if ( Cond ) Stmt | if ( Cond ) Stmt else Stmt

are not suitable for recursive descent parsing with a single symbol look-ahead because the two alternatives share a common prex. One can rewrite the production as follows. IfStmt ElsePart if ( Cond ) Stmt ElsePart | else Stmt

April 2, 2012

2/1

Left-factoring and left-recursion removal

Left factoring productions

Left factor rewriting rule

To remove the left factor from A | one can rewrite the production as follows. A A A |

April 2, 2012

3/1

Left-factoring and left-recursion removal

Removing left recursion

Removing left recursion from grammars


A production of the form E E + T |T is not suitable for recursive descent parsing because the left recursion in the grammar leads to an innite recursion in the parsing program. In terms of T , the above production matches sequences of the form T, T + T, T + T + T, ... i.e., a T followed by zero or more occurrences of +T . The grammar can be rewritten to E E
April 2, 2012

T E |+T E
4/1

Left-factoring and left-recursion removal

Removing left recursion

Immediate left recursion (simple) rewriting rule

To remove the left recursion from A A| which matches a followed by zero or more occurrences of , one can rewrite the production as follows. A A A |A

April 2, 2012

5/1

Left-factoring and left-recursion removal

Removing left recursion

Immediate left recursion (general) rewriting rule


The general case for direct left recursion has the form: A A 1 | A 2 | . . . | A n | 1 | 2 | . . . | m We use grouping to see the structure in the same form as the simple case: A A (1 | 2 | . . . | n ) | (1 | 2 | . . . | m ) and rewrite the production as follows, A (1 | 2 | . . . | m ) A A | (1 | 2 | . . . | n ) A or without the grouping as follows. A 1 A | 2 A | . . . | m A A | 1 A | 2 A | . . . | n A
April 2, 2012 6/1

Left-factoring and left-recursion removal

Removing left recursion

Indirect left recursion rewriting rule


Left recursion may be indirect, e.g., A A 1 | B 2 B B 1 | C 2 C A 1 | 2 The rewriting for indirect left recursion takes multiple steps. First we remove the direct left recursions for A and B. A A B B C B 2 A | 1 A C 2 B | 1 B A 1 | 2

April 2, 2012

7/1

Left-factoring and left-recursion removal

Removing left recursion

... Indirect left recursion rewriting rule


Next replace the occurrence of B in the rst production by its denition and remove the (no longer used) production for B. A A B C C 2 B 2 A | 1 A | 1 B A 1 | 2

Then the occurrence of C by its denition and remove C. A A B (A 1 | 2 ) 2 B 2 A | 1 A | 1 B

April 2, 2012

8/1

Left-factoring and left-recursion removal

Removing left recursion

... Indirect left recursion rewriting rule

Removing the grouping from A exposes a direct left recursion in A A A 1 2 B 2 A | 2 2 B 2 A which we remove. A A A B 2 2 B 2 A A | 1 2 B 2 A A | 1 A | 1 B

April 2, 2012

9/1

You might also like