Download presentation
Presentation is loading. Please wait.
1
Chapter 4-3 Chang Chi-Chung 2008.6.10
2
SLR and Ambiguity Every SLR grammar is unambiguous, but not every unambiguous grammar is SLR, maybe LR(1) Consider for example the unambiguous grammar S L = R | R L * R | id R LFOLLOW(R) = {=, $} I 0 : S’ S S L=R S R L *R L id R L I 1 : S’ S I 2 : S L=R R L I 3 : S R I 4 : L *R R L L *R L id I 5 : L id I 6 : S L=R R L L *R L id I 7 : L *R I 8 : R L I 9 : S L=R action[2,=]=s6 action[2,=]=r5 no Has no SLR parsing table
3
LR(0) Automation I 1 : S’ S I 2 : S L=R R L I 3 : S R I 4 : L *R R L L *R L id I 5 : L id I 6 : S L=R R L L *R L id I 7 : L *R I 8 : R L I 9 : S L=R S L I 0 : S’ S S L=R S R L *R L id R L R * id R L R= * L *
4
Determining Lookaheads for (each item A→ α · β in K) { J = CLOSURE({[A → α · β, # ]}); if ([B →γ·Xδ, a ] is in J, and a is not #) conclude that lookahead a is generated spontaneously for item B →γX·δ in GOTO(I, X); if ([B →γ·Xδ, # ] is in J ) conclude that lookahead propagate from A → α · β in I to B →γX·δ in GOTO(I, X); }
5
Examples S’→ ·S CLOSURE( [S’→ ·S, #] ) S’→ · S, # S → · L = R, # S → ·R, # L → ·* R, #/= L → · id, #/= R → · L, # S L = R | R L * R | id R L L → * · R, = L → id ·, = S’→ S · S → L · = R S → R · L → * · R L → id · R → L · Propagated Spontaneous
6
Propagation of Lookaheads FromTo I 0 : S’→ ·SI 1 : S’→ S · I 2 : S → L · = R I 2 : R → L · I 3 : S’→ R · I 4 : L → * ·R I 5 : L → id · I 2 : S → L · = RI 6 : S → L = · R I 4 : L → * · R I 5 : L → id · I 7 : L → * R · I 8 : R → L · I 6 : S → L = · RI 4 : L → * ·R I 5 : L → id · I 8 : R → L · I 9 : S → L = R ·
7
Computation of Lookaheads SET ITEM Lookahead INITPass 1Pass 2Pass 3 I 0 : S’→ ·S $$$$ I 1 : S’→ S · $$$ I 2 : S → L · = R I 2 : R → L · $$$$ $$$$ $$$$ I 3 : S’→ R · $$$ I 4 : L → * ·R ==/$ I 5 : L → id · ==/$ I 6 : S → L = · R $$ I 7 : L → * R · ==/$ I 8 : R → L · ==/$ I 9 : S → L = R · $
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.