Download presentation
Presentation is loading. Please wait.
1
Compiler Lecture Note, LR Parsing
컴파일러 입문 제 8 장 LR 구문 분석
2
목 차 I. LR Parsers II. The Canonical Collection of LR(0) Items
Compiler Lecture Note, LR Parsing 목 차 I. LR Parsers II. The Canonical Collection of LR(0) Items III. Construction of LR Parsing Tables III.1 SLR Method III.2 CLR Method III.3 LALR Method IV. Deterministic Parsing of Ambiguous Grammars V. Compaction of LR Parsing Tables VI. Implementation of an LR Parser
3
Compiler Lecture Note, LR Parsing
. LR Parsers efficient Bottom-up parsers for a large and useful class of context-free grammars. the "L" stands for left-to-right scan of the input; the "R" for constructing a Rightmost derivation in reverse. The attractive reasons of LR parsers (1) LR parsers can be constructed for most programming languages. (2) LR parsing method is more general than LL parsing method. (3) LR parsers can detect syntactic errors as soon as possible. But, it is too much work to implement an LR parser by hand for a typical programming-language grammar. =====> Parser Generator
4
Parser Generating Systems Grammar Parsing Table
Compiler Lecture Note, LR Parsing Parser Generating Systems Grammar Parsing Table <BNF Notations> Input Output PGS Parsing Table Driver Routine * The driver routine is the same for all LR parsers; only the parsing table changes from one parser to another.
5
The techniques for producing LR parsing tables
Compiler Lecture Note, LR Parsing The techniques for producing LR parsing tables Simple LR(SLR) - LR(0) items, FOLLOW Canonical LR(CLR) - LR(1) items Lookahead LR(LALR) LR(1) items LR(0), Lookahead ` CLR LALR SLR
6
stack : S0X1S1X2 ••• XmSm, where Si : state and Xi V.
Compiler Lecture Note, LR Parsing LR parser stack : S0X1S1X2 ••• XmSm, where Si : state and Xi V. Configuration of an LR parser : (S0X1S1 ••• XmSm, aiai+1 ••• an$) stack contents unscanned input a1 ••• ai an $ : input Sm Parsing Table Driver Routine stack
7
<Nonterminals>
Compiler Lecture Note, LR Parsing Parsing Table(ACTION table + GOTO table) ACTION Table GOTO Table The LR parsing algorithm ::= same as the shift-reduce parsing algorithm. Four Actions : 1. shift 2. reduce 3. accept 4. error symbol states <Terminals> <Nonterminals> ••• ••• •••
8
::= (S0X1S1 XmSm, aiai+1 an$) (S0X1S1 XmSmaiS, ai+1 an$)
Compiler Lecture Note, LR Parsing 1. ACTION[Sm,ai] = shift S ::= (S0X1S1 XmSm, aiai+1 an$) (S0X1S1 XmSmaiS, ai+1 an$) 2. ACTION[Sm,ai] = reduce A and || = r ::= (S0X1S1 XmSm, aiai+1 an$) (S0X1S1 Xm-rSm-r, aiai+1 an$), GOTO(Sm-r , A) = S (S0X1S1 Xm-rSm-rAS, aiai+1 an$) 3. ACTION [Sm,ai] = accept, parsing is completed. 4. ACTION [Sm,ai] = error, the parser has discovered an error and calls an error recovery routine.
9
ex) G : 1. LIST LIST , ELEMENT 2. LIST ELEMENT 3. ELEMENT a
Compiler Lecture Note, LR Parsing ex) G : 1. LIST LIST , ELEMENT 2. LIST ELEMENT 3. ELEMENT a Parsing Table : where, sj means shift and stack state j, ri means reduce by production numbered i, acc means accept, and blank means error. symbols states a , $ LIST ELEMENT s3 1 2 1 s4 acc 2 r2 r2 3 r3 r3 4 s3 5 5 r1 r1
10
Input : = a, a STACK INPUT ACTION initial configuration a,a$ s3
Compiler Lecture Note, LR Parsing Input : = a, a STACK INPUT ACTION initial configuration a,a$ s3 0 a 3 ,a$ r GOTO 2 0 ELEMENT 2 r GOTO 1 0 LIST 1 s4 0 LIST 1, 4 a$ 0 LIST 1, 4 a 3 $ r GOTO 5 0 LIST 1, 4 ELEMENT 5 r GOTO 1 accept
11
. The Canonical Collection of LR(0) Items
Compiler Lecture Note, LR Parsing . The Canonical Collection of LR(0) Items The method for constructing an LR parsing table from a grammar ① SLR LR(0) items ② LALR ③ CLR Definition : an LR(0) item a production with a dot at some position of the right side. ex) A XYZ P, [A .XYZ] [A X.YZ] [A XY.Z] [A XYZ.] mark symbol ::= the symbol after the dot if it exists. kernel item ::= [A α.] if α, A = S'. closure item ::= [A .α] : the result of performing the CLOSURE operation.
12
Definition : Augmented Grammar
Compiler Lecture Note, LR Parsing [Aα.β] means that an input string derivable from α has just been seen, if next seeing an input string derivable from β, we may be able to reduce by the production A αβ. Definition : Augmented Grammar G = (VN, VT, P, S) G' = (VN {S’},VT, P {S' S}, S') where, S' is a new start symbol not in VN. The purpose of this new starting production is to indicate to the parser when it should stop parsing and announce acceptance of the input. That is, acceptance occurs when and only when the parser is about to reduce by S' S.
13
If S αAω αβ1β2ω, then αβ1 : viable prefix.
Compiler Lecture Note, LR Parsing If S αAω αβ1β2ω, then αβ1 : viable prefix. rm rm "viable prefix is a prefix of a right sentential form that does not continue past the right end of the handle of that sentential form." We say item [Aβ1.β2] is valid for a viable prefix if there is a derivation S αAω αβ1β2ω, rm rm "In general, an item will be valid for many viable prefixes." Canonical collection of LR(0) items ::= the set of valid items for each viable prefix that can appear on the stack of an LR parser. Computation : CLOSURE & GOTO function * *
14
The CLOSURE operation Definition : CLOSURE(I)
Compiler Lecture Note, LR Parsing The CLOSURE operation Definition : CLOSURE(I) = I {[B . ] | [A .B] CLOSURE(I), B P} Meaning : [A .B] in CLOSURE(I) indicates that, at some point in the parsing process, we next expect to see a substring derivable from B as input. If B is a production, we would also expect to see a substring from at this point. For this reason, we also include [B . ] in CLOSURE(I).
15
Algorithm CLOUSURE(I) ; begin CLOUSURE := I ; repeat
Compiler Lecture Note, LR Parsing Computing Algorithm: Algorithm CLOUSURE(I) ; begin CLOUSURE := I ; repeat if [A .B ] CLOSURE and B P then if [B .] CLOSURE then CLOSURE := CLOSURE ∪ {[B .]} fi until no change end.
16
= {[E' .E], [E .E+T], [E .T], [T .TF], [T .F],
Compiler Lecture Note, LR Parsing ex) E' E E E + T | T T T F | F F (E) | id CLOSURE ({[E' .E]}) = {[E' .E], [E .E+T], [E .T], [T .TF], [T .F], [F .(E)], [F .id]}. CLOSURE({[E E.+T]}) = { [E E.+T] }. ex) S AS | b A SA | a CLOSURE({[S A.S]}) = {[S A.S], [S .AS], [S .b], [A .SA], [A .a]}.
17
The GOTO operation Canonical Collection Definition : GOTO(I,X)
Compiler Lecture Note, LR Parsing The GOTO operation Definition : GOTO(I,X) = CLOSURE({[A X. ] | [A .X] I}). Meaning : If I is the set of items that are valid for some viable prefix , then GOTO(I,X) is the set of items that are valid for the viable prefix X. ex) I = {[E' E.], [E E.+T]} GOTO(I,+) = CLOSURE({[E E+.T]}) = {[E E+.T], [T .TF], [T .F], [F .(E)], [F .id]} Canonical Collection C0 = {CLOSURE ({[S' .S]})} ∪ {GOTO(I,X) | I ∈ C0, X ∈ V} We are now ready to give the algorithm to construct C0, the canonical collection of sets of LR(0) items for an augmented grammar; the algorithm is the following:
18
Construction algorithm of C0.
Compiler Lecture Note, LR Parsing Construction algorithm of C0. Algorithm Canonical_Collection; begin C0 := { CLOSURE({[S' . S]}) }; repeat for I ∈ C0 do Closure := CLOSURE(I); for each X ∈ MARK SYMBOL of Closure do J := GOTO(I,X); if Ji = J then GOTO[I,X] := Ji else GOTO[I,X] := J; C0 := C0 ∪ {J} fi end for until no change end.
19
ex) G : LIST LIST , ELEMENT LIST ELEMENT ELEMENT a
Compiler Lecture Note, LR Parsing ex) G : LIST LIST , ELEMENT LIST ELEMENT ELEMENT a Augmented Grammar G' : ACCEPT LIST LIST LIST , ELEMENT LIST ELEMENT ELEMENT a
20
Co : I0 : CLOSURE({[ACCEPT .LIST]})
Compiler Lecture Note, LR Parsing Co : I0 : CLOSURE({[ACCEPT .LIST]}) = {[ACCEPT .LIST], [LIST .LIST,ELEMEMT], [LIST .ELEMENT], [ELEMENT .a]}. GOTO(I0,LIST) = I1 = {[ACCEPT LIST.], [LIST LIST.,ELEMEMT]}. GOTO(I0,ELEMENT) = I2 = {[LIST ELEMENT.]}. GOTO(I0,a) = I3 = {[ELEMENT a.]}. GOTO(I1,,) = I4 = {[LIST LIST,.ELEMEMT], [ELEMENT .a]}. GOTO(I4,ELEMENT) = I5 = {[LIST LIST,ELEMEMT.]}. GOTO(I4,a) = I3.
21
Compiler Lecture Note, LR Parsing
GOTO graph ::= a directed graph in which the nodes are labeled by the sets of items and the edges by grammar symbol. Ex) I1 LIST , ELEMENT I0 ELEMENT I2 I4 I5 a a I3
22
Constructing C0 using GOTO graph
Compiler Lecture Note, LR Parsing Constructing C0 using GOTO graph ex) G : PR b DL ; SL e (PR P ) DL d ; DL | d (DL D ) SL s ; SL | s (SL S ) 교과서 317쪽 [예 8] renaming G : P b D ; S e D d ; D | d S s ; S | s -생성 규칙에 대한 LR(0) 아이템 [A->.]은 closure 아이템인 동시에reduce 아이템이 된다. 교과서 360쪽 연습문제 8.8 ex) G : S S + A | A A (S) | a(S) | a
23
C0 : [P' P.] P [P' .P] [P .bD;Se] b [P bD.;Se] [D d.;D] [D d.]
Compiler Lecture Note, LR Parsing C0 : [P' P.] I1 I0 P [P' .P] [P .bD;Se] b [P bD.;Se] I3 [D d.;D] [D d.] I4 [P b.D;Se] [D .d;D] [D .d] I2 D d ; ; [P bD;.Se] [S .s;S] [S .s] I5 d [D d;.D] [D .d;D] [D .d] I6 s [S s.;S] [S s.] I8 S D [P bD;S.e] I7 [D d;D.] I9 ; s [S .s;.S] [S .s;S] [S .s] I11 e [P bD;Se.] I8 [S s;S.] I12 S
24
III. Construction of LR Parsing Tables
Compiler Lecture Note, LR Parsing III. Construction of LR Parsing Tables Three methods SLR(simple LR) - C0, Follow CLR(Canonical LR) - C1 LALR(Lookahead LR) C1 C0. Lookahead Parsing Table Action Table GOTO Table symbols states VT {$} VN 1 2 3 : Shift reduce accept error GOTO
25
State i is constructed from Ii, where Ii ∈ C0.
Compiler Lecture Note, LR Parsing State i is constructed from Ii, where Ii ∈ C0. The size of parsing table depends on the number of states of C0. But, |C0| << |C1|. SLR : |V| * |C0| CLR : |V| * |C1| LALR : |V| * |C0|
26
III.1 Constructing an SLR parsing table
Compiler Lecture Note, LR Parsing III.1 Constructing an SLR parsing table ::= The method constructing the SLR parsing table from the C0. Constructing Algorithm: C0 = {I0,I1,I2,...,In} 1. ACTION[i,a] := "shift j" if [A .a ] ∈ Ii and GOTO(Ii,a) = Ij. 2. ACTION[i,a] := "reduce A α", for all a ∈ FOLLOW(A) if [A .] ∈ Ii . 3. ACTION[i,$] := "accept" if [S' S.] ∈ Ii . 4. GOTO[i,A] := j if GOTO(Ii, A) = Ij. 5. "error" for all undefined entries and initial state is i if [S' .S] ∈ Ii . reduce item에 대해 FOLLOW를 사용하여 resolve.
27
ex) G : 0. A L (A : ACCEPT, L : LIST, E : ELEMENT) 1. L L , E
Compiler Lecture Note, LR Parsing ex) G : 0. A L (A : ACCEPT, L : LIST, E : ELEMENT) 1. L L , E 2. L E 3. E a FOLLOW(A) = {$} FOLLOW(L) = {,,$} FOLLOW(E) = {,,$} I0 [A .L] [L .L,E] [L .E] [E .a] L a E I1 I2 I3 [A L.] [L L.,E] [L E.] [E a.] I4 , [L L,.E] [E .a] a E I5 [L L,E.]
28
Parsing Table : s4 s3 Action Table GOTO Table a E , L $ r3 5 r1 2 1
Compiler Lecture Note, LR Parsing Parsing Table : Action Table GOTO Table a E , L $ I3 r3 I4 s3 5 I5 r1 I0 2 1 I1 s4 acc I2 r2 Symbols states
29
ex) G: 1. S L = R 2. S R 4. L id 3. L R 5. R L C0 :
Compiler Lecture Note, LR Parsing ex) G: 1. S L = R 2. S R L id 3. L R R L C0 : I1 I0 I5 S [S .S] [S .L=R] [S .R] [L .R] [L .id] [R .L] [S S.] id [L id.] I2 [S L.=R] [R L.] L I4 id R = [S .R] [R .L] [L .R] [L .id] I6 I3 [S L=.R] [R .L] [L .R] [L .id] [S R.] I7 R [L R.] id R I9 I8 L [R L.] [S L=R.]
30
ACTION[2,=] := "reduce RL " (∵ = ∈ FOLLOW(R))
Compiler Lecture Note, LR Parsing Consider I2 : ACTION[2,=] := "shift 6 " ACTION[2,=] := "reduce RL " (∵ = ∈ FOLLOW(R)) shift/reduce conflict Not SLR(1)
31
III.2 Constructing CLR Parsing Tables
Compiler Lecture Note, LR Parsing III.2 Constructing CLR Parsing Tables In the SLR method, if [A .] Ii, then M[i,a] := reduce A for all a FOLLOW(A). But in some situations, a cannot be a follow symbol of A in State i. Thus, the reduction by A would be invalid on a in that state. To solve this problem, we must carry more information that will allow us to rule out some of these invalid reductions by A .This is called the lookahead of the item that is a state-dependent FOLLOW symbol.
32
LR(1) item ::= LR(0) + lookahead information
Compiler Lecture Note, LR Parsing LR(1) item ::= LR(0) + lookahead information form : [A .,a]. where A P and a VT {$}. 1. One in LR(1) is the length of the lookahead. 2. A . is called core. 3. a is called the lookahead of the item. The lookahead has no effect in an item of the form [A .,a], where , but an item of the form [A .,a] calls for a reduction by A only if the next symbol is a. The method for constructing the collection of sets of valid LR(1) item is essentially the same as C0 construction except CLOSURE operation.
33
CLOSURE operation of LR(1) item:
Compiler Lecture Note, LR Parsing CLOSURE operation of LR(1) item: CLOSURE(I) = I {[B .,b]|[A .B,a] CLOSURE(I), B P, b FIRST(a)}. ex) G : S' S S CC C cC C d CLOSURE({[S' .S,$]}) = {[S' .S,$], [S .CC,$], [C .cC,c/d], [C .d,c/d]}. We use the notation [C .cC,c/d] as a shorthand for the two items [C .cC,c] and [C .cC,d]. CLOSURE({[A .B,a]}) = {[A .B,a]} {[B .,b] | b FIRST(a)}.
34
ex) S [S' CC.,$] [S' S.,$] [S .S,$] [S .CC,$] [C .cC,c/d] C
Compiler Lecture Note, LR Parsing ex) [S' CC.,$] I5 I0 [S .S,$] [S .CC,$] [C .cC,c/d] [C .d,c/d] [S' S.,$] I1 S C [S C.C,$] [C .cC,$] [C .d,$] I2 C [C c.C,$] [C .cC,$] [C .d,$] I6 c c d c d [C c.C,c/d] [C .cC,c/d] [C .d,c/d] I3 d [C d.,$] I7 c C [C d.,c/d.] I4 d C [C cC.,c/d.] I8 [C cC.,$] I9 I6 differs from I3 only in second components.
35
Construction of CLR parsing table
Compiler Lecture Note, LR Parsing Construction of CLR parsing table ::= same as SLR except that ACTION[i,a] := reduce A if [A .,a] Ii. ex) G : S L = R | R G' : 0) S' S augmented L R | id =========> ) S L = R R L ) S R 3) L R 4) L id 5) R L C1 : I0 S = I1 : [S' S.,$] I0 L = I2 : [S L.=R,$] [R L.,$] I0 R = I3 : [S R.,$] I0 = I4 : [L .R,=] [R .L,=] [L .R,=] [L .id,=] I0 id = I5 : [L id.,=] I0 : [S' .S,$] [S .L=R,$] [S .R,$] [L .R,=] [L .id,=] [R .L,$]
36
I2 = = I6 : [S L=.R,$] [R .L,$] [L .R,$] [L .id,$]
Compiler Lecture Note, LR Parsing I2 = = I6 : [S L=.R,$] [R .L,$] [L .R,$] [L .id,$] I6 R = I9 : [S L=R.,$] I6 L = I10 : [R L.,$] I6 = I11 : [L .R,$] [R .L,$] [L .R,$] [L .id,$] I6 id= I12 : [L id.,$] I4 R = I7 : [L *R.,=] I4 L = I8 : [R L.,=] I4 = I4 I4 id = I5 I11 R = I13 : [L R.,$] I11 L = I10 I11 = I11 I11 id = I12
37
Parsing Table $ = id S L R Action Table Goto Table symbols states s4
Compiler Lecture Note, LR Parsing Parsing Table symbols states $ = id S L R s4 s5 1 2 3 r5 s6 r2 4 8 7 5 r4 6 s12 10 9 r3 r1 11 s11 13 12 Action Table Goto Table acc s11 13 r3
38
LR(1) Parsing ... ( S0, id = id $ )
Compiler Lecture Note, LR Parsing LR(1) Parsing ( S0, id = id $ ) S4 ===> ( S0 S4, id = id $ ) S5 ===> ( S0 S4 id S5, = id $ ) r4,Goto8 ===> ( S0 S4 L S8, = id $ ) r5,Goto7 ===> ( S0 S4 R S7, = id $ ) r3,Goto7 ===> ( S0 L S2, = id $ ) S6 ===> ( S0 L S2 = S6, id $ ) ...
39
III.3 Constructing LALR Parsing Tables
Compiler Lecture Note, LR Parsing III.3 Constructing LALR Parsing Tables Two methods C1 merge C0, lookahead The C1 method LR(1) item : [A ., a ] The general idea of the algorithm is to construct C1 and if core lookahead no conflicts arise, merge sets with common cores. In general, a core is a set of LR(0) item for the grammar at hand. Thus SLR and LALR tables for a grammar always have the same number of states.
40
Compiler Lecture Note, LR Parsing
ex) I3 + I6 I36: {[Cc.C,c/d/$],[C.cC,c/d/$],[C.d,c/d/$]}. I4 + I7 I47: {[C d.,c/d/$]}. I8 + I9 I89: {[C cC.,c/d/$]}. Parsing table Action Table Goto Table symbols states c d $ S C s36 s47 1 2 1 acc 2 s36 s47 5 3 6 s36 s47 8 9 4 7 r3 r3 r3 5 r1 8 9 r2 r2 r2
41
shift/reduce conflict : can not decide whether to shift
Compiler Lecture Note, LR Parsing The merging of states with common cores can never produce a shift-reduce conflict that was not present in one of the original states, because shift actions depend on the core , not the lookahead. It is possible, however, that a merger will produce a reduce-reduce conflict. shift/reduce conflict : can not decide whether to shift or to reduce reduce/reduce conflict : can not decide which of several reductions to make.
42
The C0 method complex but smaller time & space. references :
Compiler Lecture Note, LR Parsing The C0 method complex but smaller time & space. the C1 method : simple but time & space consuming method. references : 1. Korenjak, A.J. [1969]. "A Pratical Method for Constructing LR(k) Processors," CACM 12:11, pp 2. DeRemer, F.L. [1969]. Practical Translators for LR(k) Languages, Ph.D dissertation, MIT. 3. DeRemer, F.L. and T.J. Pennello [1982]. "Efficient Computation of LALR(1) Look-Ahead Sets," ACM TOPLAS, 4:4, PP C0, lookahead
43
Efficient Computation of Lookahead Sets
Compiler Lecture Note, LR Parsing Efficient Computation of Lookahead Sets Definition : LA(p, [A . ]) = {a | a FIRST(), S' A , accesses p}. where " accesses p" means that starting from the start state the scanning of the string will result in a sequence of state transitions, the last of which is state p. Computing formula : LA(p, [A .]) = FIRST(2) LA(q, [B 1.A2]). qPRED(p,) [B 1.A2]q PRED(p, ) = {q | p GOTO (q, )}. *
44
Computing Lookahead Sets by Recursive Calls.
Compiler Lecture Note, LR Parsing Computing Lookahead Sets by Recursive Calls. function LALR(p:state; I : item) : set of VT ; assume I = [A .]; LALR := {}; if A <> S' then for q PRED(p, ) do for [B 1.A 2] q do LALR := LALR FIRST(2); if FIRST(2) and MAP(q, [B 1.A 2]) then LALR := LALR LALR(q, [B 1.A 2]) fi end for end function lookahead of augmented rule: LA(I0,[S' .S]) = {$}.
45
ex) [S S.] S [S L.=R] [R L.] [L .*R] [L .id] [S L=R.]
Compiler Lecture Note, LR Parsing 교과서 336쪽 [예 15] [S S.] I1 ex) S [S L.=R] [R L.] [L .*R] [L .id] I6 [S L=R.] I9 [S L.=R] [R L.] I2 R = I0 [S .S] [S .L=R] [S .R] [R .L] [L .*R] [L .id] L . [S R.] I3 R * L [L *.R] [R .L] [L .*R] [L .id] I4 * [L *R.] I7 R id . . [R L.] I8 * L . id [L id.] I5 id
46
Construction of LALR parsing tables
Compiler Lecture Note, LR Parsing LA ( I2, [R L.] ) = FIRST() LA ( I0, [S .R] ) = LA ( I0, [S .R] ) = FIRST() LA ( I0, [S' .S] ) = {$} LA ( I5, [L id.] ) Construction of LALR parsing tables same as SLR method except that ACTION[p,a] := reduce A for all a∈LA(p,[A .]).
47
VI. Deterministic Parsing of Ambiguous Grammars
Compiler Lecture Note, LR Parsing VI. Deterministic Parsing of Ambiguous Grammars Reference : Aho, A.V. and Johnson, S.C., and Ullman, J.D. "Deterministic Parsing of Ambiguous Grammars," Comm. ACM 18:8, pp Every ambiguous grammar fails to be LR. So ambiguous grammars always arise the conflicts, shift-reduce or reduce-reduce. But some ambiguous grammars are quite useful in the specification of languages. And also they can reduce the speed of a parser.
48
shift-reduce conflict --- can not decide whether to shift
Compiler Lecture Note, LR Parsing shift-reduce conflict --- can not decide whether to shift or to reduce. reduce-reduce conflict --- can not decide which of several reductions to make. These conflicts can be resolved using the precedence and associativity information. Precedence : higher shift lower reduce Associativity : left reduce right shift
49
Ex) E E + E | E E | (E) | id I7,I8 : id * ) E I7 I8 [E .E]
Compiler Lecture Note, LR Parsing Ex) E E + E | E E | (E) | id I0 [E .E] [E .E+E] [E .EE] [E .(E)] [E .id] I1 [E E.] [E E.+E] [E E.E] E * I4 [E E+.E] [E .E+E] [E .EE] [E .(E)] [E .id] ( id [E id.] I3 id I7 [E E+E.] [E E.+E] [E E.E] + id E I2 [E (.E)] [E .E+E] [E .EE] [E .(E)] [E .id] ( * I5 [E .EE] [E .E+E] [E .(E)] [E .id] I8 [E EE.] [E E.+E] [E E.E] ( + id E ( E I6 [E (E.)] [E E.+E] [E E.E] * * [E (E).] I9 ) + I7,I8 : 상태 id + * ( ) $ E I7 r1,s4 r1,s5 r1 r1 I8 r2,s4 r2,s5 r2 r2
50
ex) The "Dangle-else" Ambiguity
Compiler Lecture Note, LR Parsing ex) The "Dangle-else" Ambiguity S' S S iSeS | iS | a [S i.SeS] [S i.S] [S .iSeS] [S .iS] [S .a] I2 [S' .S] I0 [S a.] I3 [S iSe.S] I5 [S iS.eS] [S iS.] I4 [S iSeS.] I6 [S' S.] I1 S i a e .
51
i e a $ S s2 s3 1 acc 4 r3 s5 r2 6 r1 else : right associativity
Compiler Lecture Note, LR Parsing else : right associativity shift symbols states i e a $ S s2 s3 1 acc 2 4 3 r3 s5 r2 5 6 r1 Action Table Goto Table string : iiaea
52
V. Compaction of LR Parsing Tables
Compiler Lecture Note, LR Parsing V. Compaction of LR Parsing Tables Parsing table the size of parsing table : |states | |V | a typical P.L grammar : |V| = 100, |states| = 300 the size of P.T. = 30,000 entries Symbols states Action Table GOTO Table 1 2 3 : Shift reduce accept error GOTO
53
Compaction methods s2 s3 acc r3 r3 s5 r2 r1 r1
Compiler Lecture Note, LR Parsing Compaction methods (1) Identical action entries can be represented by one entry and pointers can be used. ex) s2 s3 1 2 3 4 5 6 acc r3 r3 s5 r2 r1 r1 state ACTION
54
(2) By creating a list for the actions of each state, further
Compiler Lecture Note, LR Parsing (2) By creating a list for the actions of each state, further space efficiency can be achieved. ex) state 0,2,5 : (i,s2), (a,s3), (any,error) state 1 : ($,acc), (any,error) state 3 : (any,r3) state 4 : (e,s5), ($,r2), (any,error) state 6 : (any,r1) (3) Encoding the GOTO field. form : GOTO[current-state,A] = next-state, where A ∈ VN. making a list of pairs for each nonterminal.
55
1 2 3 4 5 6 7 8 9 ex) [그림 8.10] --- Text p.339 states VN S 1 L 2 8 8 R
Compiler Lecture Note, LR Parsing ex) [그림 8.10] --- Text p.339 states VN 1 2 3 4 5 6 7 8 9 S 1 L 2 8 8 R 3 7 9 S : (0,1), (any,error) L : (0,2), (4,8), (6,8), (any,error) R : (0,3), (4,7), (6,9), (any,error) (remarks) ======> Representation of sparse matrix. Use the dynamic storage.
56
VI. Implementation of an LR parser
Compiler Lecture Note, LR Parsing VI. Implementation of an LR parser LR parser for Mini-Pascal(Text pp ) Parsing Table ptbl[S,X] = > 0 : shift < 0 : reduce = 1 : accept = 0 : error Mini-Pascal Grammar(Text. pp ) (1) number of rules : 69 (2) number of symbols : 71 (3) number of states : 134 symbols states AST를 위한 문법
57
Compiler Lecture Note, LR Parsing
미니파스칼 문법 1. 부록 A : pp reduce 행동에 코드 생성하기에 적당한 형태 miniA.tbl #define NO_RULES 70 #define GOAL_RULE (NO_RULES + 1) #define NO_SYMBOLS 74 #define NO_STATES 131 절 : pp AST를 구성하기에 적당한 형태 miniB.tbl #define NO_RULES 69 #define NO_SYMBOLS 71 #define NO_STATES 134
58
perfect.pas Scanner right parse Parser Parsing table
Compiler Lecture Note, LR Parsing Programming Assignment #2 Implement an LR parser for Mini-Pascal grammar. Problem Specifications - input : perfect.pas(Text p. 568) - output : right parse - methods : (1) use the existing scanner for Mini-Pascal. (2) get the parsing table for Mini-Pascal at the web site. (3) program an LR parser(Text pp ) perfect.pas Scanner right parse Parser Parsing table
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.