% The following is a slightly more realistic grammar for English % than was presented in class. % It shows how to handle, using difference lists, the cases of % 1) indefinite repetition (the Kleene *) % 2) rules with more than 2 symbols on their right-hand sides % 3) rules with a terminal symbol on their right-hand sides parse(S) :- s(S/[]). % For the rule S -> NP VP s(S/X) :- np(S/VP), vp(VP/X). % For the rule NP -> Det Adj* N np(NP/X) :- det(NP/Nbar), adjs(Nbar/N), n(N/X). adjs(Adjs/Adjs). % an empty list of adjectives adjs(Adjs/X):- % a nonempty list of adjectives adj(Adjs/Adj1), adjs(Adj1/X). % For the rule VP -> V NP PP* % (PP stands for "prepositional phrase") vp(VP/X):- v(VP/NP1), np(NP1/NP2), pps(NP2/X). pps(PPs/PPs). % an empty list of PPs pps(PPs/X) :- % a nonempty list of PPs pp(PPs/PP1), pps(PP1/X). pp(PP/X):- prep(PP/NP), np(NP/X). % For the rule NP -> that S np(NP/X):- that(NP/S), s(S/X). % the lexicon n([dog | X]/X). n([cat | X]/X). n([park | X]/X). n([city | X]/X). v([chased | X]/X). v([saw | X]/X). det([a | X]/X). det([the |X]/X). adj([little | X]/X). prep([in | X]/X). that([that | X]/X). % testing :- parse(['the','little','little','dog','chased','a','cat']), print('multiple adjectives are ok'), nl. :- parse(['the','cat','saw','that','the','dog','chased','a','cat']), print('that-NPs are ok'), nl. :- parse(['the','dog','chased','a','cat','in','the','park','in','the','city']), print('multiple prepositional phrases are ok'), nl.