% Simple top-down parser
% parse(?Cat, ?S1, ?S2 )
% parse constituent of category Cat, using input string S1 and
% leaving string S2
% Covington (1994), NLP for Prolog Programmers, pp. 152-154
% extended by pronouns, number agreement and case

parse_s(List) :-            % extra predicate not defined in Covington
	parse(s,List,[]).


% Top-down Recognizer

parse(C,[Word|S],S) :-
	word(C,Word).

parse(C,S,S1) :-
	rule(C,Cs),
	parse_list(Cs,S,S1).

parse_list([C|Cs],S,S2) :-
	parse(C,S,S1),
	parse_list(Cs,S1,S2).

parse_list([],S,S).


rule(s,[np(nom,Num),vp(Num)]).
rule(np(Case,Num),[d(Num),n(Case,Num)]).
rule(np(Case,Num),[pron(Case,Num)]).
rule(d(pl),[]).
rule(vp(Num),[v(0,Num)]).
rule(vp(Num),[v(1,Num),np(acc,_)]).
rule(vp(Num),[v(1,Num),np(acc,_),pp]).
rule(vp(Num),[v(2,Num),np(acc,_),np(acc,_)]).
rule(pp,[p,np(acc,_)]).

word(d(_),the).
word(d(pl),all).
word(d(sg),every).
word(n(_,sg),cat).
word(n(_,pl),cats).
word(n(_,sg),dog).
word(n(_,pl),dogs).
word(n(_,sg),elephant).
word(n(_,pl),elephants).
word(p,near).
word(pron(nom,sg),she).
word(pron(acc,sg),her).
word(pron(nom,sg),he).
word(pron(acc,sg),him).
word(v(0,pl),walk).
word(v(0,sg),walks).
word(v(1,pl),amuse).
word(v(1,sg),amuses).
word(v(1,pl),chase).
word(v(1,sg),chases).
word(v(1,pl),see).
word(v(1,sg),sees).
word(v(2,pl),give).
word(v(2,sg),gives).