% grammar rules: s(P1,P3,s(NP,VP)) :- np(P1,P2,NP), vp(P2,P3,VP). vp(P1,P3,vp(v(Verb),NP)) :- v(P1,P2,Verb), np(P2,P3,NP). np(P1,P2,np(name(Name))) :- proper(P1,P2,Name). np(P1,P3,np(det(Det),noun(Noun))) :- det(P1,P2,Det), noun(P2,P3,Noun). % lexicon entries: isname(john). isverb(fed). isdet(the). isnoun(numbat). % rules to do lexical analysis - that is to classify words according % to their part of speech: det(From, To, Word) :- word(Word, From, To), isdet(Word). noun(From, To, Word) :- word(Word, From, To), isnoun(Word). v(From, To, Word) :- word(Word, From, To), isverb(Word). proper(From, To, Word) :- word(Word, From, To), isname(Word). % An example sentence to parse: word(john, 1, 2). word(fed, 2, 3). word(the, 3, 4). word(numbat, 4, 5). % to use this program, save it in your own directory, say under % the name dcg2.pl, then do: % % % prolog -s dcg2.pl % ?- s(Start, Finish, Parse). % % The result should tell you Start = 1, Finish = 5, and Parse = a parse of the % sentence. See if you can extend the lexicon and maybe the grammar % so that you can parse some other sentences.