120 likes | 277 Vues
Lee McCluskey, room 2/07 Email lee@hud.ac.uk http://scom.hud.ac.uk/scomtlm/cis2326. Parsing Using Logic Programing (well Prolog). Recap. Traditional procedures for creating PARSERS are to: Input a BNF Grammar – possibly with annotations – to a Parser Generator (PG)
E N D
Lee McCluskey, room 2/07 Email lee@hud.ac.uk http://scom.hud.ac.uk/scomtlm/cis2326 Parsing Using Logic Programing (well Prolog)
Recap • Traditional procedures for creating PARSERS are to: • Input a BNF Grammar – possibly with annotations – to a Parser Generator (PG) • Use the PG to create a Parsing Table • The PG's interpreter together with the Parsing Table forms the Parser. This can input a tokenised string (eg tokenised program) and parse it.
But who needs LR/SR Parsers or Parsing Tables when we have Prolog!? Polog looks a bit like BNF Grammar rules. Consider the Grammar from week 15 Z ::= d Z ::=XYZ Y ::= b Y ::= c X ::= Y X ::= aZ This looks like z :- d. z :- x,y,z. ETC Only thing to consider is the Data Flow – how a string is parsed.
Recall From Week 15 % Z ::= d z(I, O) :- I = [d | O]. % Z ::=XYZ z(I, O) :- x(I,S1), y(S1,S2), z(S2,O). % Y ::= b y(I, O) :- I = [b| O]. % Y ::= c. y(I, O) :- I = [c| O]. % X ::= aZ x(I, O) :- I = [a | O1], z(O1,O). % X ::= Y x(I, O) :- y(I,O). Eg | ?- z([a,d,b,d],[]). yes
Parsing with Prolog Grammar Rules Grammar Z ::= d Z ::=XYZ Y ::= b Y ::= c X ::= Y X ::= aZ Prolog Grammar Rules z --> [d]. z --> x,y,z. y --> [b]. y --> [c]. x --> y. x --> [a],z. This Grammar can now be loaded into Prolog and ACTS LIKE A PARSER.
Parsing with Prolog Grammar Rules Prolog Grammar Rules z --> [d]. z --> x,y,z. y --> [b]. y --> [c]. x --> y. x --> [a],z. z(A, B) :- A = [d | B]. z(A, B) :- x(A, C), y(C, D), z(D, B). y(A, B) :- A = [b| B]. y(A, B) :- A = [c | B]. x(A, B) :- y(A, B). x(A, B) :- A = [a | C], z(C, B).
Parsing with Prolog Grammar Rules – Expression Example exp --> var,[plus],exp. exp --> constant,[minus],exp. exp --> [openbr],exp,[closebr]. exp --> var. exp --> constant. var --> [x]. Constant --> [42]. test(X) :- exp([x,plus,x],X).
Parsing with Prolog Grammar Rules – Example from Natural Language sentence --> noun_phrase, verb_phrase. noun_phrase --> determiner, adjective, noun. noun_phrase --> adjective, noun. noun_phrase --> determiner, noun. verb_phrase --> verb, noun_phrase. verb_phrase --> verb, preposition, noun_phrase. determiner --> [a]. adjective --> [fruit]. noun --> [flies]. noun --> [banana]. noun --> [fruit]. noun --> [time]. noun --> [arrow]. verb --> [like]. verb --> [flies]. preposition --> [like]. This Grammar can now be loaded into Prolog and ACTS LIKE A PARSER.
Prolog Grammar Rules sentence(A, B) :- noun_phrase(A, C), verb_phrase(C, B). noun_phrase(A, B) :- determiner(A, C), adjective(C, D), noun(D, B). noun_phrase(A, B) :- adjective(A, C), noun(C, B). noun_phrase(A, B) :- determiner(A, C), noun(C, B). verb_phrase(A, B) :- verb(A, C), noun_phrase(C, B). verb_phrase(A, B) :- verb(A, C), preposition(C, D), noun_phrase(D, B). determiner(A,B) :- A = [a|B]. adjective(A,B) :- A = [fruit| B]. noun(A,B) :- A = [flies|B]. noun(A,B) :- A = [banana|B]. noun(A,B) :- A= [fruit|B]. noun(A,B) :- A = [time|B]. noun(A,B) :- A = [arrow|B]. verb(A,B) :- A = [like|B]. verb(A,B) :- A = [flies|B]. preposition(A,B) :- A = [like|B]. This is the Prolog Code that the Grammar Translates to. The variables implement a kind of “stream processing”
Getting More From the Parsing Stage Other functions we might need .. - Return parse tree - interpret string - compile string
Parsing + Returning a Parse Tree exp(exp(V,plus,E)) --> var(V),[plus],exp(E). exp(exp(C,plus,E)) --> constant(C),[plus],exp(E). exp(exp(openbr,E,closebr)) --> [openbr],exp(E),[closebr]. exp(var(V)) --> var(V). exp(con(C)) --> constant(C). var(var(x)) --> [x]. constant(con(42)) --> [42]. test1(T) :- exp(T, [x,plus,x], [] ). test2(T) :- exp(T, [x,plus,openbr,42,plus,x,closebr], [] ). | ?- test2(X). X = exp(var(x),plus,exp(openbr,exp(con(42),plus,var(var(x))),closebr)) ?
Parsing + Returning a Parse Tree /* syntax bit + No (-singular or plural) + Gender (-Masc, Fem or Inamin.)*/ sentence(sentence( NP, VP)) --> noun_phrase(NP, No, Gender), verb_phrase(VP, No, Gender). noun_phrase(noun_phrase([D,N]), No,Gender) --> d(D, No,Gender), n(N, No,Gender). verb_phrase(verb_phrase([V,N]), No,Gender) --> v(V, No,Gender), noun_phrase(N, _,_). /* no ref to no or gen of object*/ verb_phrase(verb_phrase([V]), No,Gender) --> v(V, No,Gender). d(determiner(the), No,Gender) --> [the]. d(determiner(a), singular,Gender) --> [a]. d(determiner(some), plural,Gender) --> [some]. d(determiner(all), plural,Gender) --> [all]. n(noun(apple), singular, inanimate) --> [apple]. n(noun(apples), plural, inanimate) --> [apples]. n(noun(man), singular, animate) --> [man]. n(noun(men), plural, animate) --> [men]. v(verb(eat), singular, animate) --> [eats]. v(verb(eats), plural, animate) --> [eat]. v(verb(sing), singular, animate) --> [sings]. v(verb(sings), plural, animate) --> [sing]. sentence(X,[the,man,eats,the,apples],Y). X = sentence( noun_phrase([determiner(the),noun(man)]), verb_phrase([verb(eat), noun_phrase([determiner(the),noun(apples)])])), Y = [] ?