[“3″;”+”;”4″;”-“]
[“3”]
[“3″;”+”;”4″]
[“+”;”4″;”-“]
[“3″;”+”;”4″]
Some
awkish_grammar
[“3”]
None
[“-“]
None
(“+”::t) | _ -> None)
“+”
[“3″;”+”;”4″]
(function | “+”::t -> Some
“-” “+”
[“3”]
. xfierp eht no deeccus dluow ti tub ) ton , htiw snigeb xffius gnidnopserroc eht ecnis(
xfierp eht no liaf ot rehctam elpmaxe eht esuac dluow rotpecca na hcuS . htiw gninnigeb sexffius ylno stpecca tub tsil elur yna stpecca
rotpecca eht ,elpmaxe roF . gninruter yb xffius dna tsil elur eht stcejer rotpecca ehT .rotcurtsnoc eht edisni depparw eulav emosgninruterybxffiusadnatsilelurastpeccatahtnoitcnufasirotpeccanA
.yella dnilb a si eno reilrae na fi evitanretla retal a ot kcartkcab ot dna sevitanretla elpitlum yrtotdeensemitemossrehctam,elpmaxeehtgnitucexeyllatnemybeesnacuoysA
. eulav laiceps eht snruter ti ,sexfierp gnihctam on sdnfi rehctam a fI .snruter rotpecca eht revetahw nruter lliw dna ,
xffius gnidnopserroc eht htiw gnola , xfierp dnoces eht ot rotpecca eht ylppa lliw rehctam eht ,esiwrehtO .snruter rotpecca eht revetahw nruter lliw rehctam
eht ,detpecca si siht fI . xffius gnidnopserroc eht htiw gnola ,
xfierp tsrfi eht ot rotpecca eht ylppa tsrfi lliw rehctam ehT . dna yleman ,hctam taht sexfierp elbissop owt dnfi dna
gnirts eht tcepsni thgim woleb rof rehctam a ,elpmaxe roF .xffius gnidnopserroc eht no sdeeccus rotpecca nevig a rehtehw gnitset yb elbatpecca si hctam eht rehtehw skcehc neht dna ,rammarg a fo lobmys lanimretnon a ot sdnopserroc taht xfierp a rof hctam a dnfi ot slanimret fo gnirts nevig a stcepsni taht noitcnuf a si rehctam A .rehctam a fo taht si tnemngissa siht fo noiton yek ehT
.margorp dilav a si gnirts eht fo xfierp on fi noitacidni rorre na ro ,xffius dehctamnu gnidnopserroc eht snruter ti ,esrap ot margorp a si xfierp esohw gnirts a nevig si resrap siht nehW .resrap a si taht noitcnuf a etareneg lliw margorp ruoy ,1 krowemoH fo elyts ehtnirammarganeviG.rotarenegresrapelpmisaetirwotediceduoyoS.tcerrocni era stcejorp tneduts eht esac ni noinipo dnoces a ekil d’uoy yawyna dna tey enod t’nerastcejorpesohttub,stcejorp231SClautcanotitsetotsiyawenO.stcejorp 231 SC rof sesac tset sa desoporp gnieb era taht srammarg tset ot ekil d’uoY
srammarg eerf txetnocfognisrapeviaN.2krowemoH
noitavitoM
Some x
awkish_grammar
| Expr ->
[[N Term; N Binop; N Expr];
[N Term]]
[[]]
[“3”; “+”; “4”; “xyzzy”]
None Some
Some s
s
[]
eht dessap nehw( tpecca taht hcus garf fo p xfierp a sehctam rehctam A .garf tnemgarf a dna tpecca rotpecca na :stnemugra owt htiw noitcnuf deirruc a
rehctam .x eulav emos rof x snruter ti esiwrehto ; snruter ti ,elbatpecca
ton si tnemgarf eht fI .garf tnemgarf a si tnemugra esohw noitcnuf a
rotpecca . ,.g.e ,slobmys lanimret fo tsil a
tnemgarf .eulav lanimretnon a si lobmys trats ehT .noitcnuf noitcudorp a dna lobmys trats a fo gnitsisnoc ,riap A
rammarg .lanimretnontahtroftsilevitanretla s’rammarg a snruter tI .eulav lanimretnon a si tnemugra esohw noitcnuf A
noitcnuf noitcudorp .gnirts lobmys ytpme eht gniniatnoc tsil notelgnis a erew
ti fi sa detaert si tsil evitanretla ytpme na ,noitnevnoc yB .lobmys lanimretnon nevig a rof selur s’rammarg a fo lla ot sdnopserroc tI .sedis dnah thgir fo tsil A
.tnemgarf tupni sti ot sdnopserroc taht eert esrap a sdleiy hcihw ,resrap a dliub ot ysae ylevitaler eb dluohs ti ,rehctam a dliub nac uoy fI
.”mreT → rpxE” elur relpmis eht esu ot gnitpmetta erofeb “rpxE poniB mreT → rpxE” elur eht esu ot tpmetta dluohs rehctam ruoy ,erofereht dna
:siht sniatnoc woleb ,elpmaxe roF .thgir-ot-tfel meht yrt syawla dluohs uoy ,lanimretnon a rof yrt ot selur lareves era ereht revenehW
.deeccus ot eulav a nruter ot ylno sdeen rotpecca level -pot a :smargorp tset yb deilppus srotpecca level-pot rof deriuqer ton si ti ,edoc ruoy yb desu srotpecca lanretni eht rof laicurc si roivaheb siht hguohtlA .)strats xffius eht erofeb tsuj sdne ti ecnis( sdne xfierp dehctam eht erehw fo noitacidni na eveirter ot rellac s’rehctam eht swolla sihT .)xffius eht fo emos demusnoc erofereht sah dna ,tupni eht fo erom desrap evah yam rotpecca eht esuaceb( xffius tupni eht fo liat a si erehw , snruter lufsseccus si taht rotpecca na ,noitnevnoc yB
tsilevitanretla .1 krowemoH ni sa emas
elur ,edis dnah thgir ,lobmys snoitinfieD
type (‘nonterminal, ‘terminal) parse_tree =
| Node of ‘nonterminal * (‘nonterminal, ‘terminal) parse_tree list
| Leaf of ‘terminal
tree
make_matcher
None
make_parser
awksub_grammar parse_tree_leaves tree
None
convert_grammar
convert_grammar
awksub_grammar_2 = convert_grammar awksub_grammar
awksub_grammar_2
let
resrap eht ,)dne ot gninnigeb morf ,si taht( yleritne desrap eb tonnac garf fI .eert esrap lanoitpo na snruter resrap eht ,garf tnemgarf a ot deilppa nehW
.marg rammarg eht rof resrap a snruter taht marg noitcnuf a etirW .4 . snruter rehctam eht ,dnuof si hctam elbatpecca on fI .denruter rotpecca eht revetahw snruter rehctam eht ,sneppah siht nehW .xfierp gnihctam eht swollof yletaidemmi taht tnemgarf xffius eht nevig nehw sdeeccus tpecca fi elbatpecca eb ot deredisnoc
si hctam A .hctam elbatpecca tsegnol eht ro tsetrohs eht ylirassecen ton
si siht ;garf fo xfierp gnihctam elbatpecca tsrfi eht ot gnidnopserroc xffius eht no tpecca gnillac fo tluser eht nruter dna redro ni selur rammarg eht yrt tsum rehctam eht ,garf tnemgarf a dna tpecca rotpecca na ot deilppa nehW .marg
rammarg eht rof rehctam a snruter taht marg noitcnuf a etirW .3 .deretnuocne sevael eht fo tsil a sdleiy dna thgir ot tfel eert esrap
ehtsesrevarttaht noitcnufaetirw,pumrawrehtonasA.2 . rammarg elyts-1 krowemoH
eht ot tnelaviuqe si taht rammarg elyts-2 krowemoH a ot dnib dluohs
noitinfied level-pot eht ,elpmaxe roF .1 krowemoH ni nevig srammarg tset eht no fo noitatnemelpmi ruoy tseT .1marg rammarg elyts-1 krowemoH eht morf detrevnoc si hcihw ,rammarg elyts-2 krowemoH a snruter taht 1marg noitcnuf a etirW .1 krowemoH susrev
,tnemngissa siht ni tnereffid si srammarg fo tamrof eht taht eciton ,pu mraw oT .1 tnemngissA
.tupni eht fo xfierp laitini na ylno emusnoc yam hcihw ,srehctam ekilnu ,tupni eritne eht emusnoc sresraP .seert esrap ot stnemgarf morf noitcnuf a
resrap .redro noitavired tsomtfel
a ni desrevart ,rammarg eht ni elur a ot sdnopserroc eert esrap eht fo edon lanretni hcae dna ,tnemgarf desrap eht sa slobmys lanimret emas eht niatnoc retnuocne uoy sevael eht ,thgir ot tfel redroerp ni eert esrap a esrevart uoy fI
:epyt lmaCO gniwollof eht sah tI .yaw lausu eht ni eert esrap a gnitneserper erutcurts atad a
eert esrap . snruter ti esiwrehto ;snruter tpecca revetahw snruter rehctam eht ,hctam a hcus si ereht fI .)devomer si p retfa sniamer taht garf fo xffius eht ,.e.i( xffius gnidnopserroc eht stpecca )xffius gnidnopserroc
None
Some tree tree
parse_tree_leaves make_parser gram frag
frag
make_parser
make_parser_test
Some tree
make_parser make_parser gram
parse_tree_leaves tree
make_matcher
make_matcher
make_matcher_test
make_matcher
Pervasives List
/usr/local/cs/bin
.1 krowemoH ni sa euqinhcet emas eht gnisu ,htap ruoy fo trats eht ta si taht erus ekam os ,erofeb sa srevres xuniL tenSAES eht no margorp ruoy tset lliw eW
.os od ot ysae si ti nehw ecaps dna emit yrassecennu gnisu diova dluohs edoc ruoy tub ,ycneicffie naht tnatropmi erom si yticilpmiS .stceffe edis fo eerf eb dluohs edoc ruoY .seludom rehto on esu dluohs titub,seludom dna ehtesuyamedocruoy,1krowemoHhtiwsA
.htiwelbuortevahlliwti srammarg fo tros hcihw wonk ot ekil dluow ew tub ,srammarg elbissop lla htiw krow lliw noitatnemelpmi ruoy taht tcepxe t’nod ew ,elpmaxe roF .meht tuoba klat dluohs tnemssessa ruoy os ,ereh sessenkaew emos gnitcepxe era ew ,1 krowemoH ekilnU
.ereh rof gnikool er’ew troper fo dnik elpmis eht rof llikrevo si ereht ecivda eht fo hcum yldettimda ;stnemssessa etirw ot woh no ecivda rof stroper nettirw dna snoitatneserp larorofsecruoseReeS.)esaelp,senil05tsaeltadna,enilrepsnmuloc 08dnasenil001tsomta(osroegapasemusnoctahtelfitxetnialpIICSA elpmis a eb dluohs troper sihT .noitatnemelpmi ruoy htiw liaf taht sesac tset yb sessenkaew etartsulli ,elbissop fI .noitacilppa dednetni sti fo txetnoc eht ni noitulos ruoy ni sessenkaew yna nialpxe ,oslA .snoitcnuf owt eht ni noitacilpud diova ot koot uoy taht hcaorppa eht nialpxe yfleirb neht “rehtien” s’ti fi dna ;rehtien ro ,asrev eciv ro , fo smret ni etirw ot dediced
uoy yhw snialpxe taht troper noitca-retfa na gnitirw yb krow ruoy ssessA .7 . slauqe
taht ni ,
neht , snruter
fo esrevni eht esnes emos ni si
nehw
taht kcehc dluohs tset sihT .rammarg tset emas ruoy gnisu noitcnuf ruoy rof esac tset doog a etirw ,ylralimiS .6
.nwo ruoy fo rammarg a tset dluohs esac tset ruoY . deman eb dluohs esac tset ruoY .saera melborp tnereffid revoc dluohs tub ,woleb nevig sesac tset eht fo elyts eht ni
eb dluohs tI .noitcnuf ruoy rof esac tset laivirtnon ,doog eno etirW .5 . sa redro emas eht
ni selur rammarg yrt dluohs resrap ruoY .tnemgarf tupni eht ot gnidnopserroc eert esrap eht si erehw snruter ti ,esiwrehtO . snruter
:selfi eerht timbuS
timbuS
hw2.ml convert_grammar parse_tree_leaves make_matcher make_parser
make_matcher
hw2test.ml
hw2.txt
let accept_all string = Some string
let accept_empty_suffix = function
| _::_ -> None
| x -> Some x
(* An example grammar for a small subset of Awk.
This grammar is not the same as Homework 1; it is
instead the same as the grammar under
“Theoretical background” above. *)
type awksub_nonterminals =
| Expr | Term | Lvalue | Incrop | Binop | Num
let awkish_grammar =
(Expr,
function
| Expr ->
[[N Term; N Binop; N Expr];
[N Term]]
| Term ->
[[N Num];
[N Lvalue];
[N Incrop; N Lvalue];
[N Lvalue; N Incrop];
[T”(“; N Expr; T”)”]]
| Lvalue ->
[[T”$”; N Expr]]
| Incrop ->
[[T”++”];
[T”–“]]
| Binop ->
[[T”+”];
[T”-“]]
| Num ->
[[T”0″]; [T”1″]; [T”2″]; [T”3″]; [T”4″];
[T”5″]; [T”6″]; [T”7″]; [T”8″]; [T”9”]])
let test0 =
((make_matcher awkish_grammar accept_all [“ouch”]) = None)
let test1 =
((make_matcher awkish_grammar accept_all [“9”])
= Some []
let test2 =
rof deen seirailixua yna htiw gnola sesac tset ruoy niatnoc dluohs .
enfied ot dedeen snoitcnuf dna sepyt yrailixua yna htiw gnola
dna , , enfied dluohs
sesactsetelpmaS
.tnemssessa ruoy dloh dluohs
.meht
((make_matcher awkish_grammar accept_all [“9”; “+”; “$”; “1”; “+”])
= Some [“+”]
let test3 =
((make_matcher awkish_grammar accept_empty_suffix [“9”; “+”; “$”; “1”; “+”])
= None)
(* This one might take a bit longer…. *)
let test4 =
((make_matcher awkish_grammar accept_all
[“(“; “$”; “8”; “)”; “-“; “$”; “++”; “$”; “–“; “$”; “9”; “+”;
“(“; “$”; “++”; “$”; “2”; “+”; “(“; “8”; “)”; “-“; “9”; “)”;
“-“; “(“; “$”; “$”; “$”; “$”; “$”; “++”; “$”; “$”; “5”; “++”;
“++”; “–“; “)”; “-“; “++”; “$”; “$”; “(“; “$”; “8”; “++”; “)”;
“++”; “+”; “0”])
= Some [])
let test5 =
(parse_tree_leaves (Node (“+”, [Leaf 3; Node (“*”, [Leaf 4; Leaf 5])]))
= [3; 4; 5])
let small_awk_frag = [“$”; “1”; “++”; “-“; “2”]
let test6 =
((make_parser awkish_grammar small_awk_frag)
= Some (Node (Expr,
[Node (Term,
[Node (Lvalue,
[Leaf “$”;
Node (Expr,
[Node (Term,
[Node (Num,
[Leaf “1”])])])]);
Node (Incrop, [Leaf “++”])]);
Node (Binop,
[Leaf “-“]);
Node (Term,
[Node (Num,
[Leaf “2”])])])))
let test7 =
match make_parser awkish_grammar small_awk_frag with
| Some tree -> parse_tree_leaves tree = small_awk_frag
| _ -> false
$ ocaml
OCaml version 4.07.1
hw2sample.ml
hw2.ml
#use “hw2test.ml”;;
nur dluohs
tenSAES eht no noitulos
.noitulos ruoy no sesac tset nwo ruoy dnammoc eht ,ylralimiS .lmaCO fo noitatnemelpmi ruoy tset ot gniwollof eht eki gnihtemos htiw ti elfi a otni sesac tset elpmas eht tup uoy fI
esu ot elba eb dluohs uoy ,
sesactsetfoesuelpmaS
# #use “hw2.ml”;;
…
val parse_tree_leaves : (‘a, ‘b) parse_tree -> ‘b list =
…
val make_matcher :
‘a * (‘a -> (‘a, ‘b) symbol list list) ->
(‘b list -> ‘c option) ->
‘b list -> ‘c option =
…
val make_parser :
‘a * (‘a -> (‘a, ‘b) symbol list list) ->
‘b list ->
(‘a, ‘b) parse_tree option =
…
# #use “hw2sample.ml”;;
val accept_all : ‘a -> ‘a option =
val accept_empty_suffix : ‘a list -> ‘b list option =
type awksub_nonterminals = …
val awkish_grammar :
awksub_nonterminals *
(awksub_nonterminals -> (awksub_nonterminals, string) symbol list list) =
(Expr,
val test0 : bool = true
val test1 : bool = true
val test2 : bool = true
val test3 : bool = true
val test4 : bool = true
val test5 : bool = true
…
val test6 : bool = true
val test7 : bool = true
#
$pxEtregge10:21:3222/10/910207.1v,lmth.2wh:dI$ .selurgniypoceeS.treggEluaP9102,7102–4102,2102–6002,4002,3002©
.dne eht ta noitulos elpmas eht rof kooL .saedi nommoc emos era ereht tub melborp emas eht ton si dna krowemoh hguot a si tI .tnih a sa 2 krowemoH suoiverp a esu nac uoY
tniH