1 CS 1622 Lecture 10 1 CS1622 Lecture 10 Parsing (5) CS 1622 Lecture 10 2 LL(1) Parsing Table Example Left-factored grammar E → T X X → + E | ε T → ( E ) | int Y Y → * T | ε The LL(1) parsing table: ε ε ε * T Y ( E ) int Y T ε ε + E X T X T X E $ ) ( + * int CS 1622 Lecture 10 3 LL(1) Parsing 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 production can generate an int in the first place Consider the [Y,+] entry “When current non-terminal is Y and current token is +, get rid of Y” Y can be followed by + only in a derivation in which Y → ε
20
Embed
LL(1) Parsing Table Example - University of Pittsburghpeople.cs.pitt.edu/~mock/cs1622/lectures/lecture10.pdf · LL(1) Parsing Table Example Left-factored grammar ... Bottom-up parsing
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
CS 1622 Lecture 10 1
CS1622
Lecture 10Parsing (5)
CS 1622 Lecture 10 2
LL(1) Parsing Table Example Left-factored grammar
E → T X X → + E | εT → ( E ) | int Y Y → * T | ε
The LL(1) parsing table:
εεε* TY( E )int YT
εε+ EXT XT XE
$)(+*int
CS 1622 Lecture 10 3
LL(1) Parsing Table Example(Cont.) Consider the [E, int] entry
“When current non-terminal is E and next input isint, use production E → T X
This production can generate an int in the firstplace
Consider the [Y,+] entry “When current non-terminal is Y and current token
is +, get rid of Y” Y can be followed by + only in a derivation in
Consider the [E,*] entry “There is no way to derive a string starting
with * from non-terminal E”
CS 1622 Lecture 10 5
Using Parsing Tables Method similar to recursive descent, except
For each non-terminal S We look at the next token a And chose the production shown at [S,a]
We use a stack to keep track of pending non-terminals - instead of procedure stack!
We reject when we encounter an error state We accept when we encounter end-of-input
CS 1622 Lecture 10 6
LL(1) Parsing Algorithminitialize stack = <S $> and nextrepeat case stack of <X, rest> : if T[X,*next] = Y1…Yn then stack ← <Y1… Yn
rest>; else error (); <t, rest> : if t == *next ++ then stack ← <rest>; else error ();until stack == < >
3
CS 1622 Lecture 10 7
LL(1) Parsing ExampleStack Input ActionE $ int * int $ T XT X $ int * int $ int Yint Y X $ int * int $ terminalY X $ * int $ * T* T X $ * int $ terminalT X $ int $ int Yint Y X $ int $ terminalY X $ $ εX $ $ ε$ $ ACCEPT
CS 1622 Lecture 10 8
Constructing Parsing Tables LL(1) languages are those defined by a
parsing table for the LL(1) algorithm No table entry can be multiply defined We want to generate parsing tables
from CFG
CS 1622 Lecture 10 9
Constructing Parsing Tables(Cont.) If A → α, where in the line of A we place α ? In the column of t where t can start a string
derived from α α →* t β We say that t ∈ First(α)
In the column of t if α is ε and t can follow anA S →* β A t δ We say t ∈ Follow(A)
4
CS 1622 Lecture 10 10
Computing First SetsDefinition First(X) = { t | X →* tα} ∪ {ε | X →* ε}Algorithm: First(t) = { t } ε ∈ First(X) if X → ε is a production ε ∈ First(X) if X → A1 … An
and ε ∈ First(Ai) for 1 ≤ i ≤ n First(α) ⊆ First(X) if X → A1 … An α