Upload
phu
View
221
Download
0
Embed Size (px)
Citation preview
7/26/2019 n phn tch c php theo PP Earley
1/19
HC VIN K THUT QUN SKHOA CNG NGH THNG TIN
---------------------o0o------------------
BI TP LN
Mn: L thuyt chng trnh dch
ti:Vit chng trnh phn tch c php theo phng php Earley.C m phng thc hin t!ng "#c.
Gio vi !"#$ %&:Ts H Ch Trung
T!'( !i)* Trn Quang V Nguyn Triu Ph
L#+* CHKH,T-K./B
7/26/2019 n phn tch c php theo PP Earley
2/19
TPHCM, thng 0 n!" #0$%
,0C L0C
12 T34 565...............................................................................................................3
.2 Gi7i 5!895 :;?...............................................................................................4
&'(h)i t*+............................................................................................................4
' Thu-t t+n.......................................................................................................
@ ' o......................................................................................................
@ 8?)5..........................................................................................................
@ HoD 5!i)..................................................................................................
E2 C!"F$ 5o +!"F$ +!+ :;
7/26/2019 n phn tch c php theo PP Earley
3/19
12 T34 565
%i&i 'hu(' )ar*+y * ,' gi&i 'hu(' / &n1 2 s5 67ng '/ng 28i rng r9i 'rong
h 'h8ng ;ho. VF >(y 'rong u
'rFnh ;hSi 'iBng Vi'1
,[i o Y 'h Y nhiDu h 'h 'E Mh nhau. VSi 2] 2i, 2u >o Uagi&i 'hu(' )ar*+y h\ * ,' Si ,' h 'h1 ;hSi 'iBng
Vi'. _ gi&i uyB' >In 2D ny1 hng 'Gi nh(n 'hIy 'rong h 'h 'E Vi' '`n
'Ai ]; h 'h gi8ng nhau 6anh sh 'E *oAi 2u 'i=n > h\ Mh nhau
;hn 2uGi Ua hng.
%i&i 'hu(' )ar*+y / &n1 gi; ng^i 2 Y 'h hFnh 6ung ,' h Mhi u' >D
gi&i 'hu(' ny.
3
7/26/2019 n phn tch c php theo PP Earley
4/19
2. Gii thut Earley
%i&i 'hu(' )ar*+y / &n 2 ;h' iu nh sau:
$%& '(o)Vbn ;hA, % c ON1 T1 d1 P1 'rong 2Y:
e N: '(; M hiu MhGng MB' 'h. e T: '(; M hiu MB' 'h.
e d: M hiu MhGng MB' 'h R' 2u.
e P: '(; *u(' ;h;.
fo c a"a$ ... an.
$%& ra)Ph
7/26/2019 n phn tch c php theo PP Earley
5/19
' Thu-t t+n
Thu(' 'on ;h
Hon 'hin OCo,;*+'+r 28i >Si ,[i dO.
@ ' o VSi ,i 'rAng 'hi 'rong dO: Of q e m j1 i1 'a 'h=, 'rAng 'hi Om q e k1
>o dO nBu Y *u(' s&n uI' m q k 'rong P.
@ 8?)5
NBu a * M hiu MB' 'h 'iB; 'h+o. VSi ,i 'rAng 'hi 'rong dO: Of q e a j1
i1 'a 'h=, 'rAng 'hi Of q a e j1 i >o dO{".
@ HoD 5!i)
VSi ,i 'rAng 'hi 'rong dO: Of q ke 1 i1 'a 'F, 'rong dOi 'rAng 'hi Om q ef j1 M1 sau 2Y 'h=, Om q f e j1 M >o dO.
| ,[i dO ;h&i Mi, 'ra +, 'rAng 'hi 29 Y ha 'rS Mhi 'h=, >o 2
'rnh 'rng *];.
_ ,inh ha ho 'hu(' 'on 'r=n1 hng 'a ;h
7/26/2019 n phn tch c php theo PP Earley
6/19
zo o Ua gi&i 'hu(' )ar*+y h\ *
,' Si ,' h 'h 'E n=n hng 'Gi ,inh ha gi&i 'hu(' )ar*+y >Si h
'h 'E 'rong 'r^ng h;
7/26/2019 n phn tch c php theo PP Earley
7/19
E2 C!"F$ 5o +!"F$ +!+ :;
7/26/2019 n phn tch c php theo PP Earley
8/19
,or&Mypair pair - parents'{
Node son $ ne% Node&pair.*ey';,or&State sparent - pair.values'{
sparent.parents&son';(nodeparent.siblins.add&son';
(
(
public Strin toStrin&'{
Strin out $ le,t / 01#0;,or&int * $ 2; * " riht.si3e&'; *//'{
i,&*$$current'out /$ 040;
out /$ riht.et&*';
(i,&riht.si3e&'$$current'
out /$ 040;
return 0&0/out/0+0/i/0'0;(
public boolean e5uals&6bject obj' {i,&obj instanceo, State'{
State s7 $ &State'obj;i,&i 8$ s7.i'
return ,alse;i,¤t 8$ s7.current'
return ,alse;i,&8le,t.e5uals&s7.le,t''
return ,alse;i,&riht.si3e&'8$s7.riht.si3e&''
return ,alse;,or&int * $ 2; * " riht.si3e&'; *//'
i,&8riht.et&*'.e5uals&s7.riht.et&*'''return ,alse;
return true;(return ,alse;
(
(
private Sentence %ords;private HashMap"Strin+ArrayList"ArrayList"Strin### rammar;private Strin start;private ArrayList"ArrayList"State## charts;private ArrayList"Node# trees;
public EarleyParser&Sentence %ords+ 9rammar rammar' {this.%ords $ %ords;
7/26/2019 n phn tch c php theo PP Earley
9/19
this.rammar $ rammar.et9rammar&';this.start $ rammar.etStartProduction&';this.charts $ ne%
ArrayList"ArrayList"State##&%ords.etSentence&'.si3e&'/:';,or&int i $ 2; i " %ords.etSentence&'.si3e&'/:; i//'{
this.charts.add&ne% ArrayList"State#&'';(
(
public ArrayList"Node# etrees&'{
return trees;(
public int run&'{
))
7/26/2019 n phn tch c php theo PP Earley
10/19
((
((
))?EEState laststate $ ne% State&0?660+:+rihtroot+2';ArrayList"State# array $ charts.et&charts.si3e&'1:';trees $ ne% ArrayList"Node#&';,or&State sroot - array'{
i,&sroot.e5uals&laststate''{
Node root $ ne% Node&0?660';sroot.parents&root';trees.add&root';
((
boolean r $ charts.et&charts.si3e&'1
:'.contains&laststate';i,&r'
return 2;else return 1:;
(
private void predictor&State s+ int j' {Strin B $ s.riht.et&s.current';ArrayList"ArrayList"Strin## rules $ rammar.et&B';,or&ArrayList"Strin# rule - rules'{
System.out.print&0Predictor Action0';State sne% $ ne% State&B+2+rule+j';add
7/26/2019 n phn tch c php theo PP Earley
11/19
( else i,&B.e5uals&%ords.etSentence&'.et&j'''{
System.out.print&0Scanner Action0';State sne% $ ne%
State&s.le,t+s.current/:+s.riht+s.i';State ne%Added $ add
7/26/2019 n phn tch c php theo PP Earley
12/19
,or&int i $ 2; i " s.parents.si3e&'; i//' ))both states havethe same number o, riht
{,or&State value - s.parents.et&i'.values'{
i,&8ne%Added.parents.et&i'.values.contains&value''
ne%Added.parents.et&i'.values.add&value';(
((
(
2r&""&r C/&11
import java.io.Bu,,ered?eader;import java.io.ile;import java.io.ileNotoundE!ception;import java.io.ile?eader;import java.io.
7/26/2019 n phn tch c php theo PP Earley
13/19
HashMap"Strin+ ArrayList"ArrayList"Strin### rammar $ ne%HashMap"Strin+ ArrayList"ArrayList"Strin###&';
Lin*edHashSet"Strin# productions $ ne% Lin*edHashSet"Strin#&';
Strin startProduction;
private int productioninde! $ :;
public 9rammar&Strin path' thro%s 9rammarErrorE!ception {,ilePath $ path;readile&';semanticAnalysis&';
(
public 9rammar&Strin te!t+ boolean test' thro%s9rammarErrorE!ception {
readStrin&te!t';semanticAnalysis&';
(
public 9rammar&' {(
public void readile&' thro%s 9rammarErrorE!ception {
ile , $ ne% ile&,ilePath';
i, &8,.e!ists&''thro% ne% 9rammarErrorE!ception&0ile doesnDt
e!ist80';
try {reader&ne% ile?eader&,'';
( catch &ileNotoundE!ception e' {e.printStac*race&';thro% ne% 9rammarErrorE!ception&0ile doesnDt
e!ist80';(
(
public void readStrin&Strin !' thro%s 9rammarErrorE!ception {reader&ne% Strin?eader&!'';
(
private void reader&?eader in' thro%s 9rammarErrorE!ception {try &4Suppressarnins&0resource0'
Bu,,ered?eader br $ ne% Bu,,ered?eader&in'' {Strin line $ br.readLine&';
int cont $ 2;
%hile &line 8$ null' {
System.out.println&0L
7/26/2019 n phn tch c php theo PP Earley
14/19
&.F'0'' { ))match ?ule- production --$ body
Strin head $line.substrin&2+line.inde!6,&0--$0' 1 :';
Strin body $line.substrin&line.inde!6,&0--$0' / ';
i,&cont $$ 2'startProduction $ head;
productions.add&head'; ))add head toproductions list
i, &rammar.containsOey&head'' {ArrayList"ArrayList"Strin## bodies
$ rammar.et&head';parseBody&body+ bodies+ cont/:';
( else {ArrayList"ArrayList"Strin## bodies
$ ne% ArrayList"ArrayList"Strin##&';rammar.put&head+ bodies';parseBody&body+ bodies+cont/:';
((else {
Strin abc $ 0Line 0 / &cont / :' /0- @D0/ line / 0@D doesnDt ,ollo%-@n Non1erminal --$ body0;
thro% ne% 9rammarErrorE!ception&abc';(
line $ br.readLine&';cont//;
(br.close&';
( catch &
7/26/2019 n phn tch c php theo PP Earley
15/19
System.out.println&0111# 0 / parentheses';F)
Pattern ree! $ Pattern.compile&?ESPL
7/26/2019 n phn tch c php theo PP Earley
16/19
)F,or&Strin j- tmp' {i,&j.charAt&2' 8$ D@0D' {
i,&8j.matches&0GA1>a13IGA1>a1321IFJG21IF0''
thro% ne%9rammarErrorE!ception&0
7/26/2019 n phn tch c php theo PP Earley
17/19
matchList.add&ree!Matcher.roup&'.trim&'';(return matchList;
(
)FFF 4return the rammarF)public HashMap"Strin+ ArrayList"ArrayList"Strin### et9rammar&'
{return rammar;
(
)FFF 4return the productionsF)public Lin*edHashSet"Strin# etProductions&' {
return productions;(
)FFF 4return the startProductionF)public Strin etStartProduction&' {
return startProduction;(
)FFF 4return the ,ilePathF)public Strin etilePath&' {
return ,ilePath;(
)FFF 4param ,ilePath the ,ilePath to setF)public void setilePath&Strin ,ilePath' {
this.,ilePath $ ,ilePath;(
(
"!
7/26/2019 n phn tch c php theo PP Earley
18/19
2 Gi;o %i) (!"F$ 5
7/26/2019 n phn tch c php theo PP Earley
19/19
2TDi =i)8 5!;4 !7o
". NgGn ngJ h 'h8ng > h/ng 'rFnh 6h
OH >in TQd
$. h'';:iMi;+6ia.o,3. h'';:"$36o.org
"#
http://wikipedia.com/http://wikipedia.com/