1 / 12

Parsing Using Logic Programing (well Prolog)

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)

Télécharger la présentation

Parsing Using Logic Programing (well Prolog)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lee McCluskey, room 2/07 Email lee@hud.ac.uk http://scom.hud.ac.uk/scomtlm/cis2326 Parsing Using Logic Programing (well Prolog)

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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).

  7. 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).

  8. 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.

  9. 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”

  10. Getting More From the Parsing Stage Other functions we might need .. - Return parse tree - interpret string - compile string

  11. 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)) ?

  12. 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 = [] ?

More Related