% 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, case and parse output


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


% The Parser

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

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

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

parse_list([],S,S).


% PS Rules

rule(s,s(NP,VP), [np(nom,Num),NP, vp(Num),VP]).

rule(np(Case,Num),np(D,N), [d(Num),D, n(Case,Num),N]).
rule(np(Case,Num),np(Pron),[pron(Case,Num),Pron]).

rule(d(pl),d(e), []).

rule(vp(Num),vp(V),       [v(0,Num), V]).
rule(vp(Num),vp(V,NP),    [v(1,Num), V, np(acc,_), NP]).
rule(vp(Num),vp(V,NP,PP), [v(1,Num), V, np(acc,_), NP, pp, PP]).
rule(vp(Num),vp(V,NP,NP), [v(2,Num), V, np(acc,_), NP, np(acc,_), NP]).

rule(pp,pp(P,NP), [p, P, np(acc,_), NP]).


% Lexicon

word(d(_),  d(the),     the).
word(d(pl), d(all),     all).
word(d(sg), d(every),   every).

word(n(_,sg), n(cat),       cat).
word(n(_,pl), n(cats),      cats).
word(n(_,sg), n(dog),       dog).
word(n(_,pl), n(dogs),      dogs).
word(n(_,sg), n(elephant),  elephant).
word(n(_,pl), n(elephants), elephants).

word(p, p(near), near).

word(pron(nom,sg), pron(she), she).
word(pron(acc,sg), pron(her), her).
word(pron(nom,sg), pron(he),  he).
word(pron(acc,sg), pron(him), him).

word(v(0,pl), v(walk),   walk).
word(v(0,sg), v(walks),  walks).
word(v(1,pl), v(amuse),  amuse).
word(v(1,sg), v(amuses), amuses).
word(v(1,pl), v(chase),  chase).
word(v(1,sg), v(chases), chases).
word(v(1,pl), v(see),    see).
word(v(1,sg), v(sees),   sees).
word(v(2,pl), v(give),   give).
word(v(2,sg), v(gives),  gives).