Title: ??S?* aA
1???(?)??????
- ??S? aAß and A??,??????a?ß??????A???
- ??S? aAß and A??,??????a?ß??????A???(??)??
- ??????????
- ????(????)?a??? G ???,??
- 1) aan?an-1??a2?a1S
- 2)???i(1in),ai-1????ai????????????,????
an,...,a1?a??????????????
24.6 ??????????
- ??????????????,?????????????,????????????????????
?,?????????????????????????,???????????????,??????
???????????????????????,????????????????,?????????
?????????????????????? - ??????????????????????,????????,???????????
- ?????????????????
34.6 ??????????
- ?????
- ???????????????????????(??S ? aAd?A
?ß????aßd?????ß?aßd???aAd,?????S?) - ????
- ????????????????,??????????
- ???????,??????????????
- ???????????????????????????
- ??,????????????????
44.6 ??????????
- ?????? ????????????????-??
- ??????-?????????????????????,?
- ????????????????(??),????????
- ?????????????,???????????????
- ????,???????????????,????????
- ????????,????4?,????????????
- ???????????????????
- ??????(??)??????????,????
- ????,?????????????????????
54.6 ??????????
- ??????
- ?????????????????????????
- ?,?????????????,????????????
- ????,??????????????
- ?????????????,????????????
- ?????????,???????????????????
- ????????????????????????????????LR(k)?????
64.6 ??????????
- ????
- ????????????
- ??????????,???????????
- ????????????????
- ????
- ???????????
- ???????????,??????????,?????????,?????????,??????
? - ???? ??? S,???????
74.6 ??????????
id id id
?????
E
???? ????
???????
?
???
??? ??????? ????
84.6 ??????????
S A A B a b b c d e
- ?????????
- ????
- S?aAcBe
- A?Abb
- B?d
- ??abbcde???
- abbcde
- ?aAbcde
- ? aAcde
- ?aAcBe
- ? S
94.6 ??????????
S
S?aAcBe
???????????
A
A?Ab
A
B
B?d
A?b
104.6 ??????????
?? ? ????? 1)
abbcde 2) ?? a bbcde 3)
?? ab bcde 4) ?? A?b aA
bcde 5) ?? aAb cde 6)
?? A?Ab aA cde 7) ?? aAc
de 8) ?? aAcd e 9)
?? B?d aAcB e 10) ??
aAcBe 11) ?? S?aAcBe S
12) ??
??????
114.6 ??????????
- ??????????????abbcde??,????????????(??)???????????
??(???5????aAb,????b???A???Ab???A?????????aAbcde(
???????????)??Ab???,??Ab???A)?????,??????????????
????????(?????????),????????????????????????,?????
???????????,???????????????????????????LR?????
124.7 ????????
- ???????????????????(?????)?????????(???????)?????
??(?????????,???????????,?????)? - ???????????GE
- E?EEE-EEEE/EE?E(E)-Eid
????id1id2id3????????? -
13????????? E EE E
Eid id id
4.7 ????????
- ?? ? ?????
- 1) id1id2id3
- 2) ?? id1 id2id3
- 3) ?? E?id E id2id3
- 4) ?? E id2id3
- 5) ?? Eid2 id3
- 6) ?? E?id EE id3
- 7) ?? EE id3
- 8) ?? EEid3
- 9) ?? E?id EEE
- 10) ?? E?EE EE
- 11) ?? E?EE E
- 12) ??
144.7 ????????
- ????
- ???????
- ?????(?????,????)?????????,?????????
- ??????????????
- ?1)???????????????????
- 2)????????????????????????
- ???????
- ???????????
- ??????
- ( ) ( ?????? )
- ??????
154.7 ????????
- ???????P146??4.3
- ??????G( VN,VT,P,S)??????A?e?
- A?aBCß????(??A,B,C??????, a, ß?
- ?????)??G??????
- ????? G ???????e????????????
- ???,??G??????
- ????????????,???????????
- ????????(??????????????P100)
164.7 ????????
- ????????
- ???G(VN,VT,P,S)??????,???
- a b ? A?ab?P?? A?aBb?P
- a b ? A?aB?P?B?b??B?Cb
- a b ? A?Bb?P?B?a??B?aC ?
- ????????????????????????
- (1) a b (2) a b (3) a b
??d?e?B(???) a,b???????, ?????
a,b????????, b??a???
a,b????????, a??b???
174.7 ????????
- ????????
- ????4.6??????????abbcde,???????????????????,???S
?aAcBe, - ???a c,c e???,??
- ?S?aAcBe?B?d,
- ???c
d,????? -
S?aAcBe?B?d,??? - d e?
184.7 ????????
- ?????????
- ???G(V,T,P,S)???????,??
- ?a,b?VT, a b,a b,a b ???????,??????????????e???
?????G?,????????????????????,??????????
194.7 ????????
- ??????????????????
- ??????,??
- ???????????????,??
- ß0a1ß1a2ß2anßn ,??ßi??e???????????(??????????,
????????????????????????)??? aißiai1ßi1aikßik
?????,?????ai-1 ai,ai ai1,ai1 ai2aik
aik1 - ?????????
- ????????? ?????
- ????P151? ???????????,???????,????????
204.7 ????????
- ?????????P152??4.4
- ???G???????,ß???aßd??A???(??S? aAd ? A? ß
)?ß?????????,??????????????????????????,??ß???aßd?
?A????????????????????????????????????????????????
???????? - ?????????E?ETT
- T?TFF
- F?(E)id ???? TTFid
,???????????????????????????
214.7 ????????
- ???
- TE
- TFT
- TTFE
- idF,T
- TTFidE
-
????? TE TFT idF
??? T
???? TFT idF
?????? TFT
1.????????????????? 2???????????? (?TE),?????????
?????(?FFT)?
224.7 ????????
- ??????????
- FIRSTVT()?LASTVT()???
- FIRSTVT(A)b A?b??A?Cb,b?VT,C ?VN
- ?A???????????????????
- LASTVT(A)bA?b??A?bC ,b?VT,C ?VN
- ?A????????????????????
- ??FIRSTVT(A)
- ??????A?a?A?Ca,?a?FIRSTVT(A)
- ??a?FIRSTVT(B),?????A?B?a?FIRSTVT(A)
- ??LASTVT(A)
- ??????A? a?A? aC ,?a?LASTVT(A)
- ??a?LASTVT(B),?????A? B,?a?LASTVT(A)
234.7 ????????
- ??????????
- ??????????????
- ?????????????A?X1X2Xn?
- ??XiXi1?VT? Xi Xi1
- ??XiXi1Xi2?VTVNVT?Xi Xi2
- ??XiXi1?VTVN??a?FIRSTVT(Xi1),Xi a
- ??XiXi1?VNVT??a?LASTVT(Xi), a Xi1
- ????a?FIRSTVT(S),? a
- ????a?LASTVT(S),?a
- ?
244.7 ????????
- ??????????
- ??????GE??
- E?ETT
- T?TFF
- F ?
(E)id???????? - ????????
- ?????????????FIRSTVT()?LASTVT()
(, id
,(, id
, ,(, id
), id
,), id
, ,), id
254.7 ????????
- ??????????FIRSTVT()?
- LASTVT()??????????
E?ETT T?TFF F ? (E)id
264.7 ????????
id id id
- ????????????(?????????ididid ???????)
?????? ???
id
id
E ? id
id
E ? id
E ? id
E ? E E
E ? E E
???????
27????(??)
- ??1????GS S?(L)aSa
-
L?L,SS???(S,(a))???? - ??????????????????????????
??a S,L SL (a)S S,(a)L
(S,(a))S ????aS SL ??S ???a ?????
a
28????(??)
- ??2???GR??????????
- R?AA(C) C?RRC A?a
????G,???????? ???FIRSTVT?LASTVT??
?? FIRSTVT(A)a FIRSTVT(R) a,(
FIRSTVT(C)a,(, LASTVT(A)a LASTVT(R)a,)
LASTVT(C)a,),
29????
S?(L) a L ?L,SS
- P205
- 4.14(a)?????????FIRSTVT?LASTVT
- (b)??????????????
S L
FIRSTVT() (,a ,,(,a
LASTVT() ),a ,,),a
30??????
- ??1????GS
- S?eTRT
- T?DRe
- R?dRe
- D?abd
- ??FIRST()?FOLLOW()??,????LL(1)??
- ??
- ??
S T R D
FIRST() a,b,d,e,e a,b, e d, e a,b
FOLLOW() a,b, d,
31??????
- SELECT(S?eT)e SELECT(S?RT)d,a,b,
- SELECT(T?DR)a,b SELECT(T?e)e,
- SELECT(R?dR)d SELECT(R?
e)e,a,b, - ????
a b d e
S S?RT S?RT S?RT S?eT S?RT
T T?DR T?DR T? e
R R? e R? e R? dR R? e
D D?a D?bd
32??????
- ??2P203 4.6
- ??????????
- S ?(L)a
- L ?SL
- L ?,SL e
- ?FIRST()?FOLLOW()??
S L L
FIRST() (,a (,a , , e
FOLLOW() ,,,) ) )
33??????
- SELECT(S?(L))( SELECT(S?a)a
- SELECT(L?SL)(,a SELECT(L?,SL),
- SELECT(L? e)e,)
- ????
( ) a ,
S S?(L) S?a
L L?SL L?SL
L L? e L?,SL
34??????
- Procedure S
- begin case SYM of
- ( ADVANCE
- L
- if SYM)
then ADVANCE else ERROR - a ADVANCE
- default ERROR
- endcase
- endbegin
35??????
- Procedure L
- begin case SYM of
- , ADVANCE
- S
- L
- ) BREAK
- default ERROR
- endcase
- endbegin
- Procedure L
- begin
- S
- L
- end
364.8 LR(k)????
- ????????????
- ???????????????,???????????
- ?????????????,???????????
- LR??????????(????)??????,?????,???,?????,???????
?????? - LR(k)??????LR(k)???????
- L ???????????
- R ????????????(?????)?
- k ??????????????????????
- ????????LR(0)?SLR(0)? LALR(1) ? LR(1)
374.8 LR(k)????
384.8 LR(k)????
- LR???
- goto(Si,X)Sj?????
- ???Si????????
- X??????Sj?X???
- ?????????
- action(Si,a)???????
- Si???????a????
- ??,???????
- ???Shift
- ?Sj goto(Si,X)????
- ??,?a?????????
394.8 LR(k)????
- LR???
- ???Reduce
- ?????????ß?,??ß???????????A,?????A?
ß???,?ß????r(? ßr)???????????????????r???,?????
?r,??A????????,Sj goto(Si,A)??????,??Si??????????
?? - ???acc
- ????????????????????S?,??????????????????,?????
?? - ???
- ?????????????????????????,???,???????????????????
404.8 LR(k)????
- LR ????????
- ?????
- S0
- a1a2an ???? a1a2an
????? s0s1 sm X1Xm aiai1an
???? X1Xmaiai1an
If actionsm,ai si(shift i) then ???? s0s1 sm
i X1Xmai ai1an
414.8 LR(k)????
If actionsm,ai ri(Reduce i) then
????i???? A?Xm-(k-1)Xm????,????
s0s1 sm-k X1Xm-kA aiai1an
?goto?,?gotosm-k,Ai then ???? s0s1 sm-k
i X1Xm-kA aiai1an
- If actionsm,aiacc then ????
424.8 LR(k)????
- ????? ip ?? ? ??????? s ?????, a ?
- ip?????? ????? ?????0
- ????????
- if actions,asi(shift i) then
- ??? a ???i?????
- ? ip ??????
- elseif actions,ari(reduce A?ß) then
- ????? 2ß???
- ? A ? gotos,A??????
- ????? A?ß
- elseif actions,aacc(accept) then
- return
- else
- error()
43 LR????
4.8 LR(k)????
???
- ?????????
- 1) S?aAcBe
- 2) A?b
- 3) A?Ab
- 4) B?d????abbcde???
?? Sn ???ai ????????n????? Rn ??n????????
444.8 LR(k)????
1) S?aAcBe 2) A?b 3) A?Ab 4) B?d
???
????abbcde?????
?? ??? ??? ??? ACTION GOTO
1
2
3
4
5
6
7
8
9
10
11
S2
S4
R2
3
S6
R3
3
S5
S8
R4
7
S9
R1
1
01
S
acc
454.8 LR(k)????
????????????,???????????? ????????????????????????
, ?????????????????????,? ??????????????? ab aAb
aAcd aAcBe?????????????????????? ????????,????????
???????? ?????(????????????????)
464.8 LR(k)????
- ????????
- ???????P145??4-2??
- ?S ?aA? ?aß????G????????,???????aß???,???????????
????????????aß????,???????????????????,????????S
?aAcBe ?aAcde ?aAbcde ?abbcde?????aAbcde??,e?a?aA?
aAb????????? - ???????
- ??????????????
- ????????????????????????????????????????????????,
???????????????????????????????????,???????,?????
DFA ????
474.8 LR(k)????
- LR(0)?????? LR??????????????????????????????????
????????DFA,????DFA???????? - LR(0)????????????????????????????????????????,??
????????????????(?S ?aAcBe ?aAcde ?aAbcde
?abbcde?????aAbcde??) - ?????????????? aAb
- ?????????????? aA
- ?????????????? a?e
- LR?????,??????????(?????????),???1???,?????????A?ß
??????ß??????,???????????????????????????????????
?A?ß1ß2????????ß1??????,???????????????ß2?????????
?3???????????????????????A?a???a???????? -
484.8 LR(k)????
- LR(0)??????G????????????????????G???LR(0)???????
?????,???????????A?ß. A?ß1.ß2 A?.a?????,A?e?????L
R(0)???A?. ????????????????????????,?LR(0)????????
- ???????A?a.aß????????????,?????????,????a??????
- ???????A?a.Bß?????????????,???????????????????B??
???????B,??????A???? - ???????A?a.??????????,???????????????,??????????
- ???????S?a., S?a?????,S???????????,???????????
,?????????????S?.a?????????????a????????,?S?S.
? -
494.8 LR(k)????
- ????
- ???G?????????S???? S?S,??S??S????????????????G?
????G??G???????????????S????????????,???????????
?? - LR(0)?????????????????
?????????? 1) S?S 2) S?aAcBe 3) A?b 4)
A?Ab 5) B?d
12 A?Ab.
6 S?aAcB.e
0 S?.S
????? ?????????? ?????????? ?????????? ???,??????
??????
1 S?S.
7 S?aAcBe.
13 B?.d
8 A?.b
2 S?.aAcBe
14 B?d.
9 A?b.
3 S?a.AcBe
10 A?.Ab
4 S?aA.cBe
11 A?A.b
5 S?aAc.Be
504.8 LR(k)????
- ????????DFA
- ??????????????????????????,???????????NFA,?????DF
A? - ??????????????,???????????NFA,?????DFA??
????? - ???????????????????NFA?????,??
S?????0?S?.S?NFA???,???????????????, - ??????????????,???????????????????
- ?????????????
- ?i???X?X1X2Xi-1.XiXn
- ?j???X?X1X2Xi-1Xi .Xi1 Xn ????I???J??????Xi
?????Xi ??????,????????????????????,?????i
X??.Ad j X??A .d?k A?.ß
514.8 LR(k)????
- ????I????e??????K,??A????????????????????I????????
?,??????e? - ??????????????G????????????????????????NFA,?????
?DFA???????NFA?
?
0
524.8 LR(k)????
?
0
534.8 LR(k)????
- ????????DFA
- ????????????????????S?.S???????,????????????,?
?LR(0)??????,?????????????????????????DFA??????? - a)???S?.S??????,???????closure(S?.S)?????????
- ???(????)??? closure( I )J P165??4.7
- JI
- repeat
- JJ?B?.?A?a.Bß?J, B???P
- until J????
544.8 LR(k)????
- ????????DFA
- b)????????????????????go(I,X)closure(J)?????J????
? - ????go(I,X)???? P165??4.8
- Go(I,X)closure(J),??
- I?G???LR(0)???,
- J????A?aX.ß??? A?a.Xß ?I
- c)????c?????????????
- ?????I0???????,?I0 closure(S?.S)S?.S,S?.aA
cBe - ?????????I0???????J
- go(I0 ,S)S?S.I1 ,go(I0 ,a)S?a.AcBe,A?.b,
A?.Ab I2 ?????????I1?I2?????????I3?I4
??,???????????????????????Ii????????LR(0)???????
554.8 LR(k)????
- ????G(VN,VT,P,S)
- ?????G???????
- ????DFA
- M(VN ?VT, C , I0, C , go) ,??
- I0CLOSURE(S ?.S
- CI0?I ?J?C,
- X? VN ?VT,Igo(J,X)
- C????????????
- ?????????????
- (?????????P170)
I0S?.S S?.aAcBe
564.8 LR(k)????
- LR(0)????????
- ?G?LR(0)???????CI0,I1,,In,?i????Ii????????(?
???DFA??) - 1?0?????
- 2??Ii?C
- if A?a.aß?Ii and go(Ii,a)Ij then actioni,asj
(???a?????,??j??????) - if A?a.Bß?Ii and go(Ii,B)Ij then gotoi,Bj
(???????i???????B??????j,??B???????,j??????) - if A?a.?Ii then for ?a?VT? do actioni,arj
(j????????????????????????????a???A,???????????
a???,?????A??????????) - if S?S.?Ii then actioni,acc(?????)
- 3? ????? error
574.8 LR(k)????
S?aAcBe1 A?b 2 A?Ab 3 B?d 4
1
S2
acc
S4
3
S6
S5
R2
R2
R2
R2
R2
R2
7
S8
R3
R3
R3
R3
R3
R3
S9
R4
R4
R4
R4
R4
R4
R1
R1
R1
R1
R1
R1
584.8 LR(k)????
- SLR(0)?????????I???????????,?
- ??-??????? I ???????,??????,?? I ?
- ??????(??I???A?a.aß,???B??.)
- ??-???? ??? I ??????????,?? I
???????(??I???A?ß. ,??B??.) - ?? I ?????????,?????????,?? I ????,??? I
?????? - ???G,??? I?C,?????,??G?LR(0)???
- ??,???LR(0)??????IX?a.bß,A??. ,B??.
???????????????-???? - 1.?ab,??? 2.?a?Follow(A),?????A????
- 3.?a?Follow(B),?????B???? 4.?????
594.8 LR(k)????
- ?????????SLR(1)???
- SLR(1)????????P171??4.7
- ?G?LR(0)???????CI0,I1,,In,?i????Ii????????(??
??DFA??) - 1?0?????
- 2??Ii?C
- if A?a.aß?Ii and go(Ii,a)Ij then actioni,asj
- if A?a.Bß?Ii and go(Ii,B)Ij then
actioni,Bsj - if A?a.?Ii then
- for ?a?FOLLOW(A)do actioni,arj
- if S'?S.?Ii then actioni,acc
- 3 ????? error
604.8 LR(k)????
- ???????G????????
- 0) E'?E
- 1) E?ET
- 2) E?T
- 3) T?TF
- 4) T?F
- 5) F?(E)
- 6) F?id
- ??????DFA?
61E
id
I1 E?E. E?E.T
I6 E?E.T T?.TF T?.F F?.(E) F?.id
I0 E?.E E?.ET E?.T T?.TF T?.F F?.(E) F?.id
T
I9 E?ET. T?T.F
F
I2 E?T. T?T.F
(
T
I7 T?T.F F?.(E) F?.id
I10 T?TF.
F
I3 T?F.
F
T
F
id
I4 F?(.E ) E?.ET E?.T T?.TF T?.F F?.(E) F?.id
(
I8 F? (E.) E?E.T
E
I5 F?id.
id
(
)
(
id
I11 F?(E).
624.8 LR(k)????
?LR(0)?????????-????
????????? FOLLOW()?? FOLLOW( E ) ), ,
FOLLOW( T ) ), , , FOLLOW( F )
), , ,
634.8 LR(k)????
???SLR(1)?????
64SLR(1) ?????
- ?????? LL(1)
- SLR(1)???Follow?????
- LL(1) ??????????
- SLR(1) ??
- ??CFG G?SLR(1)????????G?SLR(1) ??
65SLR(1)??????
- ?? SLR(1) ?????????(?????????????),????????
SLR(1) ?? - ????? LR(0) ???? FOLLOW ???????????
66? 4-16??????LR(0)???
0 1 0 2 6 9 0
3 0,4,6 4 7 0,4,6 5 0,4,6 2,8
- 0) S' ? S
- 1) S ? L R
- 2) S ? R
- 3) L ? R
- 4) L ? id
- 5) R ? L
67I0 S?.S S?.LR S?.R L?.R L?.id R?.L
I1 S?S.
I9 S?LR.
S
R
I6 S?L.R R?.L L?.R L?.id
I2 S?L.R R?L.
L
R
I3 S?R.
I7 L?R.
L
id
R
I4 L?.R R?.L L?.R L?.id
I8 R?L.
id
L
I5 L?id .
68SLR??????
- I2 S ? L.R, R ? L.
- ????? ?,?????????
- S ? L .R ? I2 and go(I2,)I6
- ? action2, Shift 6
- R ? L . ? I2 and ? FOLLOW(R),
- ? action2, Reduce R ? L
- ??????? SLR(1) ??,??????????????
69??LR(1)????
- LR(0)??????(???),SLR(1)??????????(???),??,????(???
)?????????? - ??????????????(???)?????????? A?a???,??????? A
??????????
70???(???)???
E
- ??????????????
- ?????????FOLLOW????
E T
( E )
T F
71LR(k) ??
- ??
- A?a.ß,a1a2ak
- ???? A?a.,a1a2ak
- ????A?a.aß,a1a2ak
- ????A?a.Bß,a1a2ak
- ??LR(k)????(??)LR(k)??(?),?k1?,?LR(1)??,??????LR(
1)??(?)
72?????
A
- closure(I)???
- (????A?a.Bß,a ?????/??)
- ????????????
- b ?FIRST( ßa )
a . B ß
b
. ?
73?????
- JI
- repeat
- JJ?B?.?,bA?a.Bß,a?J,
- b?FIRST(ßa)
- until J ????
- ?ß?e?,??ba???????,?????????
74?? I ????? X ?????
A
a . X . ß
?? I
?? J
- go(I,X)
- closure(A?aX.ß,aA?a.Xß,a?I)
75??LR(1)??????C????????
- CI0?I?J?C,X?V?T,Igo(J,X)??G?LR(1)??????(??
P181) - begin
- C closure( S'?.S,)
- repeat
- for ?I?C,? X ? V?T
- if go(I,X)?F go(I,X)?C then
- CC?go(I,X)
- until C???
- end.
76????????LR(1) ?DFA
- ????G(V,T,P,S)?????G???????DFA M(C, V?T, go,
I0, C) - I0CLOSURE(S ?.S
- ??CFG G?LALR(1)????????G?LALR(1)??
77LR(1) ??????
- ???P183
- ?LR(0)??????????????
- LR(0) ?????????
- SLR(1) ???? FOLLOW ?
- LR(1) ????? LR(1)???????
78??LALR(1)????
- LR(1)???C??
- ??????????/????
- ???LR(1)??????????LR(0)??,??????????
- ?????????????
- ??????????????LR(1)??/??
- ( lookahead-LR )
- ??????????????,????,?????
79LALR(1) ?????
- ?? SLR(1)
- ???????? FOLLOW ????
- ???
- ????????-????
- ??CFG G?LALR(1)????????G?LALR(1)??
80???????
- SLR(1)???
- LR(1)???
- ??????????
- ??????
- LALR(1)???
- ???????????
81????????
- ????
- ??????
- ??????????????
- ??????????????
- ????
- ?????????
- ?? LR ???(???????)
82LR???????
- 1???????,?Follow?
- 2??????????DFA
- 3???LR???
83Yacc??
- YSP(Yacc Specification)
- ????????????
- ????
- ???Token n1,n2,(???????)
- Token n1,i1(???????)
-
- Token nh,ih(???????)
- ???? type
- ????
- ???? ?????????
- ???? ??????????
- ??LALR(1)???
84- ??
- 1????????????LR(0)??????,????LR(0)???
- S ? A
- A ? B A ?
- B ? a B b
- 2???????? LR ????????????
- 3????? A ? a A d a A b ? ,????????
SLR(1) ??,??? SLR(1)??,??????????? ab ??????? - 4????????? LR(0) ??, ? SLR(1) ???
- S ? A
- A ? A b b B a
- B ? a A c a a A b
- ??P206P207 4.184.25