Bratko, Chapter 17: Language Processing with Grammar Rules See 'grammar?.pl' files. 17.1 Grammar Rules in Prolog - Backus-Naur Form (BNF) - nonterminals enclosed in <>, otherwise terminal - e.g., ::= a b OR ::= a b | a b ::= a b - generates sentences of the form a^n b^n - Definite Clause Grammar (DCG) - nonterminals are Prolog atoms, terminals are one-element lists - e.g., s --> [a], [b]. s --> [a], s, [b]. - can query sentences (difference-list representation) - e.g., ?- s([a,a,b,b],[]) or s([a,a,b,b,c],[c]) or s(X,[]) - internally - s(A,B) :- 'C'(A,a,C), 'C'(C,b,B). s(A,B) :- 'C'(A,a,C), s(C,D), 'C'(D,b,B). - where 'C'([H|T],H,T). - for s --> [a], [b] | [a], s, [b]. s(A,B) :- ( 'C'(A,a,C), 'C'(C,b,B) ; 'C'(A,a,D), s(D,E), 'C'(E,b,B) ). - simple English grammar [grammar1.pl] - augmenting grammar - problem with number "the cats hates the mouse" - add Number argument - e.g., s(Number) --> np(Number), vp(Number). s(A,B,C) :- np(A,B,D), vp(A,D,C). - see [grammar1.pl] 17.2 Handling Meaning - parse trees - "the cat scares the mice" [Fig17.2, p440] - sample grammar [grammar1.pl] - extracting meaning from parse trees - augment grammar to construct parse tree - decompose parse tree to compute result - e.g., arithmetic expression parsing - see expr in [grammar2.pl] - adding semantics to DCGs - enclose Prolog statments in curly braces - place anywhere in grammar - e.g., expr(R) --> expr(R1), [plus], expr(R2), {R is R1 + R2}. - see expr1 [grammar2.pl] 17.3 Defining the Meaning of Natural Language - meaning in logic - "the wumpus is at 1 1" -> location(wumpus,[1,1]) -> wumpus_location(1,1) - simple grammar in [grammar3.pl] - attach meaning via augmentation - e.g., transverb(Actor,Object,ate(Actor,Object)) --> [ate]. prep(What,Where,at(What,Where)) --> [at]. - see wumpus grammar in [grammar3.pl] - quantifiers - existential ("a" "an" ["the"]) - "a wumpus" --> exists(X,wumpus(X)) - "a wumpus is at 1 1" --> exists(X,wumpus(X) and location(X,[1,1])) - universal ("every") - "every wumpus is dead" --> forall(X,wumpus(X) => dead(X)) - see determiner in [Fig17.6, p455] - relative clauses - "the wumpus THAT MOVES ate the agent" - grammar syntax - np --> det, noun, rel_clause. rel_clause --> [that], vp | []. - exists(X,wumpus(X) and moves(X) and ate(X,agent)) - semantics - see rel_clause [Fig17.6, p455]