% -------------------------------------------------------------
% top-down chart parser with agenda of Gazdar/Mellish, p.211/212
% -------------------------------------------------------------

% load predicates used by all agenda chart parsers: start_agenda/3,
% extend_edges/3, add_edge/3, add_edges/3, not/1, member/2
:- ensure_loaded(common_code).

% -------------------------------------------------------------

start_active(Category,Vertex,Edges) :-
	findall(edge(Vertex,Vertex,Category,Categories),
		rule(Category,Categories),
		Edges).

% -------------------------------------

new_edges(edge(V1,V2,Category1,[]),Chart,Edges) :-
	findall(edge(V0,V2,Category2,Categories),
		member(edge(V0,V1,Category2,[Category1|Categories]),Chart),
		Edges).

new_edges(edge(V1,V2,Category1,[Category2|Categories]),Chart,Edges) :-
	start_active(Category2,V2, Edges1),
	findall(edge(V1,V3,Category1,Categories),
		member(edge(V2,V3,Category2,[]),Chart),
		Edges2),
	append(Edges1,Edges2,Edges).

% -------------------------------------

test(String) :-
	initial(Symbol),
	start_agenda(String,0,Chart1),
	start_active(Symbol,0,Agenda),
	extend_edges(Agenda,Chart1,Chart2),
	nl,
	write(String), !,
	nl,
	found_parse(Chart2).

