%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Patrick Blackburn, 1999.
% Kristina Striegnitz, 2002.
%
% This file contains the code for a finite state transducer
% with two tapes that can handle jump arcs and 
% categories/abreviations. The code is the same as for trans2.pl 
% except for the traverse predicate.
%
% Usage: testtrans_lex(Automaton,?List,?List)
%        The Lists represent the two tapes.
%
% Main predicate: transduce_lex(Automaton,+Node,?SymbolList,?SymbolList)
%                 SymbolList:  the two tapes
%                 Node:  number of the node you wish to start from
%
% This file furthermore defines:
% + the driver predicate testtrans_lex(Automaton,?SymbolList,?SymbolList)
% + traverse3/5
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% transduce3(+Node,?List,?List)
%%% Simulates the steps of the transducer. Evaluates to true, iff a
%%% final state is reached when both tapes have been read completely.

% the base case
transduce_lex(A,Node,[],[]) :-
	final(A,Node).

% the recursive clause: the transducer makes one step and then
% the predicate is called recursively with the rest of the tapes.
transduce_lex(A,Node_1,String_left,String_right) :-
	trans(A,Node_1,Node_2,Label),
	traverse_lex(A,Label,String_left,NewString_left,String_right,NewString_right),
	transduce_lex(A,Node_2,NewString_left,NewString_right).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% traverse_lex(+Symbol:Symbol,+List,-List,+List,-List)

% Neither  tape has a specialsymbol. 
traverse_lex(A,Sym_left:Sym_right,[Sym_left|Rest_left],Rest_left,
	[Sym_right|Rest_right],Rest_right) :-
	\+(special(A,Sym_left)), 
	\+(special(A,Sym_right)).

% The symbol is  a category symbol. 
traverse_lex(A,Cat,String_left,New_string_left,String_right,New_string_right) :-
	lex(A,NewLabel,Cat),
	traverse_lex(A,NewLabel,String_left,New_string_left,String_right,New_string_right).

% Both tapes have a jump symbol
traverse_lex(_A,'#':'#',String_left,String_left,String_right,String_right).

% The left tape has a jump symbol
traverse_lex(_A,'#':Sym_right,String_left,String_left,[Sym_right|Rest_right],Rest_right).

% The right tape has a jump symbol
traverse_lex(_A,Sym_left:'#',[Sym_left|Rest_left],Rest_left,String_right,String_right).


% We use the predicate special to distinguish those symbols that need a special
% treatment from 'normal' arc labels. This allows us to mix categories as arc 
% labels and normal arc labels.
special(_A,'#').
special(A,Category) :- lex(A,_,Category).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% the driver predicate

testtrans_lex(A,Left,Right) :-
	start(A,Node),
	transduce_lex(A,Node,Left,Right).
