{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# lecture07 - CS 143 Lecture 7 1 Top-Down Par sing and I ntr...

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

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 143 Lecture 7 1 Top-Down Par sing and I ntr o to Bottom-Up Par sing Lecture 7 CS 143 Lecture 7 2 Pr edictive Par ser s • Like recursive-descent but parser can “predict” which production to use – By looking at the next few tokens – No backtracking • Predictive parsers accept LL(k) grammars – L means “left-to-right” scan of input – L means “leftmost derivation” – k means “predict based on k tokens of lookahead” – I n practice, LL(1) is used CS 143 Lecture 7 3 LL(1) vs. Recur sive Descent • I n recursive-descent, – At each step, many choices of production to use – Backtracking used to undo bad choices • I n LL(1), – At each step, only one choice of production – That is • When a non-terminal A is leftmost in a derivation • The next input symbol is t • There is a unique production A → α to use – Or no production to use (an error state) • LL(1) is a recursive descent variant without backtracking CS 143 Lecture 7 4 Pr edictive Par sing and Left Factor ing • Recall the grammar E → T + E | T T → int | int * T | ( E ) • Hard to predict because – For T two productions start with int – For E it is not clear how to predict • We need to left-factor the grammar CS 143 Lecture 7 5 Left-Factor ing Example • Recall the grammar E → T + E | T T → int | int * T | ( E ) • Factor out common prefixes of productions E → T X X → + E | ε T → ( E ) | int Y Y → * T | ε CS 143 Lecture 7 6 LL(1) Par sing Table Example • Left-factored grammar E → T X X → + E | ε T → ( E ) | int Y Y → * T | ε • The LL(1) parsing table: int * + ( ) \$ E T X T X X + E ε ε T int Y ( E ) Y * T ε ε ε leftmost non-terminal next input token rhs of production to use CS 143 Lecture 7 7 LL(1) Par sing Table Example (Cont.) • Consider the [E, int] entry – “When current non-terminal is E and next input is int , use production E → T X ” – This can generate an int in the first position • Consider the [Y,+] entry – “When current non-terminal is Y and current token is + , get rid of Y ” – Y can be followed by + only if Y → ε CS 143 Lecture 7 8 LL(1) Par sing Tables. Er r or s • Blank entries indicate error situations • Consider the [E,*] entry – “There is no way to derive a string starting with * from non-terminal E ” CS 143 Lecture 7 9 Using Par sing Tables • Method similar to recursive descent, except – For the leftmost non-terminal S – We look at the next input token a – And choose the production shown at [S,a] • A stack records frontier of parse tree – Non-terminals that have yet to be expanded – Terminals that have yet to matched against the input – Top of stack = leftmost pending terminal or non-terminal • Reject on reaching error state • Accept on end of input & empty stack CS 143 Lecture 7 10 LL(1) Par sing Algor ithm initialize stack = <S \$> and next repeat case stack of <X, rest> : if T[X,*next] = Y 1 …Y...
View Full Document

{[ snackBarMessage ]}

### Page1 / 54

lecture07 - CS 143 Lecture 7 1 Top-Down Par sing and I ntr...

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

View Full Document
Ask a homework question - tutors are online