%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Patrick Blackburn, 1999.
% Kristina Striegnitz, 2002.
% Stephan Walter, 2003
%
% This file contains the code for a FSA parser based on 
% recognize (the recognizer from lecture 1 that could
% handle jump arcs).
%
% Usage: testparse(+Name,?Symbols,-Parse)
%        Name: an atom; name of the FSA to be used
%        Symbols: a list; input/output tape
%        Parse: a list; path through the states of the FSA
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% parse(+Name,+Node,?List,-List)
%%% 1st arg: name of the FSA to be used
%%% 2nd arg: current state of the FSA
%%% 3rd arg: rest of the input/output tape
%%% 4th arg: collects the states the FSA passes through

parse(A,Node,[],[Node]) :-
    final(A,Node).

parse(A,Node_1,String,[Node_1,Label|Path]) :-
    trans(A,Node_1,Node_2,Label),
    traverse(Label,String,NewString),
    parse(A,Node_2,NewString,Path).


%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% traverse(+Label,?List, -List)
%%% same as for recognize.pl

traverse('#',String,String).
traverse(Label,[Label|Symbols],Symbols).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% the driver predicates

testparse(A,Symbols,Parse) :-
    start(A,Node),
    parse(A,Node,Symbols,Parse).

generate(A,Symbols,Parse) :-
   testparse(A,Symbols,Parse).
