Download presentation
Presentation is loading. Please wait.
Published byChristine York Modified over 8 years ago
1
1 LR Parsers The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. LR(k) parsing. left to right right-most k lookhead scanning derivation (k is omitted it is 1) LR parsing is attractive because: LR parsing is most general non-backtracking shift-reduce parsing, yet it is still efficient. The class of grammars that can be parsed using LR methods is a proper superset of the class of grammars that can be parsed with predictive parsers. LL(1)-Grammars LR(1)-Grammars An LR-parser can detect a syntactic error as soon as it is possible to do so a left-to-right scan of the input.
2
2 LR Parsers LR-Parsers covers wide range of grammars. SLR – simple LR parser LR – most general LR parser LALR – intermediate LR parser (look-head LR parser) SLR, LR and LALR work same (they use the same algorithm), only their parsing tables are different.
3
3 LR Parsing Algorithm SmSmSmSm XmXmXmXm S m- 1 X m- 1.. S1S1S1S1 X1X1X1X1 S0S0S0S0 a1a1a1a1...... aiaiaiai anananan$ Action Table terminals and $ terminals and $s t four different a actions tes Goto Table non-terminal non-terminals t each item is a a state number tes LR Parsing Algorithm stack input output
4
4 A Configuration of LR Parsing Algorithm A configuration of a LR parsing is: ( S o X 1 S 1... X m S m, a i a i+1... a n $ ) StackRest of Input S m and a i decides the parser action by consulting the parsing action table. (Initial Stack contains just S o ) A configuration of a LR parsing represents the right sentential form: X 1... X m a i a i+1... a n $
5
5 Actions of A LR-Parser 1. shift s -- shifts the next input symbol and the state s onto the stack ( S o X 1 S 1... X m S m, a i a i+1... a n $ ) ( S o X 1 S 1... X m S m a i s, a i+1... a n $ ) 2. reduce A (or reduce n, where n is a production number) pop 2*| | (=r) items from the stack; then push A and s where s=goto[s m-r,A] ( S o X 1 S 1... X m S m, a i a i+1... a n $ ) ( S o X 1 S 1... X m-r S m-r A s, a i... a n $ ) Output is the reducing production reduce A 3. Accept – Parsing successfully completed 4. Error -- Parser detected an error (an empty entry in the action table)
6
6 Reduce Action pop 2*| | (=r) items from the stack; let us assume that = Y 1 Y 2...Y r then push A and s where s=goto[s m-r,A] ( S o X 1 S 1... X m-r S m-r Y 1 S m-r...Y r S m, a i a i+1... a n $ ) ( S o X 1 S 1... X m-r S m-r A s, a i... a n $ ) ( S o X 1 S 1... X m-r S m-r A s, a i... a n $ ) In fact, Y 1 Y 2...Y r is a handle. X 1... X m-r A a i... a n $ X 1... X m Y 1...Y r a i a i+1... a n $
7
7 (SLR) Parsing Tables for Expression Grammar stateid+*()$ETF 0s5s4123 1s6acc 2r2s7r2r2 3r4r4r4r4 4s5s4823 5r6r6r6r6 6s5s493 7s5s410 8s6s11 9r1s7r1r1 10r3r3r3r3 11r5r5r5r5 Action TableGoto Table 1) E E+T 2) E T 3) T T*F 4) T F 5) F (E) 6) F id
8
8 Actions of A (S)LR-Parser -- Example stackinputactionoutput 0id*id+id$shift 5 0id5*id+id$reduce by F id F id 0F3*id+id$reduce by T F T F 0T2*id+id$shift 7 0T2*7id+id$shift 5 0T2*7id5+id$reduce by F id F id 0T2*7F10+id$ reduce by T T*FT T*F 0T2+id$reduce by E T E T 0E1+id$shift 6 0E1+6id$shift 5 0E1+6id5$reduce by F id F id 0E1+6F3$reduce by T F T F 0E1+6T9$reduce by E E+TE E+T 0E1$accept
9
9 Constructing SLR Parsing Tables – LR(0) Item An LR(0) item of a grammar G is a production of G a dot at the some position of the right side. Ex:A aBb Possible LR(0) Items:A . aBb (four different possibility) A a. Bb (four different possibility) A a. Bb A aB. b A aB. b A aBb. A aBb. Sets of LR(0) items will be the states of action and goto table of the SLR parser. A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for constructing SLR parsers. Augmented Grammar: G’ is G with a new production rule S’ S where S’ is the new starting symbol.
10
10 The Closure Operation If I is a set of LR(0) items for a grammar G, then closure(I) is the set of LR(0) items constructed from I by the two rules: 1. Initially, every LR(0) item in I is added to closure(I). 2. If A . B is in closure(I) and B is a production rule of G; then B . will be in the closure(I). We will apply this rule until no more new LR(0) items can be added to closure(I).
11
11 The Closure Operation -- Example E’ E closure({E’ . E}) = E E+T { E’ . Ekernel items E TE . E+T T T*FE . T T FT . T*F F (E)T . F F idF . (E) F . id }
12
12 GOTO function Definition. Goto(I, X) = closure of the set of all items Definition. Goto(I, X) = closure of the set of all items A X. where A .X belongs to I Intuitively: Goto(I, X) set of all items that “reachable” from the items of I once X has been “seen.” E.g. consider I={E’ E., E E.+T} and compute Goto(I, +) Goto(I, +) = { E E+.T, T .T * F, T .F, F .( E ), F .id }
13
13 Construction of The Canonical LR(0) Collection To create the SLR parsing tables for a grammar G, we will create the canonical LR(0) collection of the grammar G’. Algorithm: C is { closure({S’ . S}) } repeat the followings until no more set of LR(0) items can be added to C. for each I in C and each grammar symbol X if goto(I,X) is not empty and not in C add goto(I,X) to C goto function is a DFA on the sets in C.
14
14 The Canonical LR(0) Collection -- Example I 0 : E’ .EI 1 : E’ E.I 6 : E E+.T I 9 : E E+T. E .E+T E E.+T T .T*F T T.*F E .E+T E E.+T T .T*F T T.*F E .T T .F E .T T .F T .T*FI 2 : E T. F .(E) I 10 : T T*F. T .T*FI 2 : E T. F .(E) I 10 : T T*F. T .F T T.*F F .id T .F T T.*F F .id F .(E) F .(E) F .id I 3 : T F.I 7 : T T*.FI 11 : F (E). F .id I 3 : T F.I 7 : T T*.FI 11 : F (E). F .(E) F .(E) I 4 : F (.E) F .id E .E+T E .E+T E .T I 8 : F (E.) E .T I 8 : F (E.) T .T*F E E.+T T .T*F E E.+T T .F T .F F .(E) F .(E) F .id F .id I 5 : F id.
15
15 Transition Diagram (DFA) of Goto Function I0I0 I1I2I3I4I5I1I2I3I4I5 I 6 I 7 I 8 to I 2 to I 3 to I 4 I 9 to I 3 to I 4 to I 5 I 10 to I 4 to I 5 I 11 to I 6 to I 7 id ( F * E E + T T T ) F F F ( ( * (
16
16 Constructing SLR Parsing Table (of an augumented grammar G’) 1. Construct the canonical collection of sets of LR(0) items for G’. C {I 0,...,I n } 2. Create the parsing action table as follows If a is a terminal, A .a in I i and goto(I i,a)=I j then action[i,a] is shift j. If A . is in I i, then action[i,a] is reduce A for all a in FOLLOW(A) where A S’. If S’ S. is in I i, then action[i,$] is accept. If any conflicting actions generated by these rules, the grammar is not SLR(1). 3. Create the parsing goto table for all non-terminals A, if goto(I i,A)=I j then goto[i,A]=j 4. All entries not defined by (2) and (3) are errors. 5. Initial state of the parser contains S’ .S
17
17 Parsing Tables of Expression Grammar stateid+*()$ETF 0s5s4123 1s6acc 2r2s7r2r2 3r4r4r4r4 4s5s4823 5r6r6r6r6 6s5s493 7s5s410 8s6s11 9r1s7r1r1 10r3r3r3r3 11r5r5r5r5 Action TableGoto Table 1-2 E E + T | T 3-4 T T * F | F 5-6 T ( E ) | id
18
18 SLR(1) Grammar An LR parser using SLR(1) parsing tables for a grammar G is called as the SLR(1) parser for G. If a grammar G has an SLR(1) parsing table, it is called SLR(1) grammar (or SLR grammar in short). Every SLR grammar is unambiguous, but every unambiguous grammar is not a SLR grammar.
19
19 shift/reduce and reduce/reduce conflicts If a state does not know whether it will make a shift operation or reduction for a terminal, we say that there is a shift/reduce conflict. If a state does not know whether it will make a reduction operation using the production rule i or j for a terminal, we say that there is a reduce/reduce conflict. If the SLR parsing table of a grammar G has a conflict, we say that that grammar is not SLR grammar.
20
20 Conflict Example S L=R I 0 : S’ .S I 1 :S’ S. I 6 :S L=.R S R S .L=RR .L L *R S .R I 2 :S L.=RL .*R L id L .*RR L.L .id R L L .id R .L I 3 :S R. R .L I 3 :S R. I 4 :L *.R I 7 :L *R. I 4 :L *.R I 7 :L *R. ProblemR .L ProblemR .L FOLLOW(R)={=,$}L .*R I 8 :R L. = shift 6L .id reduce by R L shift/reduce conflict I 5 :L id. I 9 : S L=R.
21
21 Conflict Example2 S AaAb I 0 :S’ .S S BbBaS .AaAb A S .BbBa B A . B . ProblemFOLLOW(A)={a,b}FOLLOW(B)={a,b} areduce by A breduce by A reduce by B reduce by B reduce/reduce conflictreduce/reduce conflict
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.