% CPSC 312 2021W1
% Author: Steve Wolfman
%
% Grammar switched to using difference lists.
% As it goes, the grammar references both the text being parsed
% and the grammatical structure. It also avoids inefficient
% list traversals imposed by append/3.
%
% Released into the public domain.

s(Words) :- simple_s(Words).
s(Words) :- append(FirstWords, SecondWords, Words), append(ConjWords, RestWords, SecondWords), simple_s(FirstWords), conj(ConjWords), s(RestWords).
simple_s(Words) :- append(NPWords, VPWords, Words), np(NPWords), vp(VPWords).
np(Words) :- append(DetWords, NounWords, Words), det(DetWords), noun(NounWords).
vp(Words) :- append(VerbWords, NPWords, Words), verb(VerbWords), np(NPWords).

conj([and]).
det([a]). det([the]).
noun([student]). noun([course]). noun([teacher]). noun([puppy]).
verb([likes]). verb([teaches]). verb([studies]). verb([tackles]).
