lec12mod

# lec12mod - CS415 Compilers left-recursion removal, left...

This preview shows pages 1–7. Sign up to view the full content.

CS415 Compilers left-recursion removal, left factoring, LL(1) summary, recursive descent parsing example, introduction to bottom-up parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 12 2 cs415, spring 10 Review - Left Recursion Top-down parsers cannot handle left-recursive grammars Formally, A grammar is left recursive if A NT such that a derivation A + A α , for some string ( NT T ) + What if my grammar does not have the LL(1) property? Sometimes, we can transform the grammar
Lecture 12 3 cs415, spring 10 Eliminating Left Recursion To remove left recursion, we can transform the grammar Consider a grammar fragment of the form Fee Fee α | β where neither α nor β start with Fee We can rewrite this as β Fie α | ε where Fie is a new non-terminal This accepts the same language, but uses only right recursion

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 12 4 cs415, spring 10 Example - Eliminating Left Recursion The expression grammar contains two cases of left recursion Applying the transformation yields These fragments use only right recursion Expr Expr + Term | – Term | Term Term * Factor | / Factor | Factor | + Term | – Term | ε Factor |* |/ | ε
Lecture 12 5 cs415, spring 10 Example - Eliminating Left Recursion Substituting them back into the grammar yields This grammar is correct, if somewhat non-intuitive. It is left associative, as was the original A top-down parser will terminate using it. General left recursion removal algorithm p.96 EAC 1 Goal Expr 2 Term Expr 3 + Term Expr 4| – Term Expr 5| ε 6 Term Factor Term 7 * Factor 8| / Factor 9| ε 10 number 11 | id 12 | ( Expr )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lecture 12 6 cs415, spring 10
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/29/2010 for the course CS 198:415 taught by Professor Kremer,u. during the Spring '10 term at Rutgers.

### Page1 / 20

lec12mod - CS415 Compilers left-recursion removal, left...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online