:- module(dcg_mit_baumausgabe,[translate/2,'-->'/2]).

translate((L --> R),(Ltr --> Rtr)) :-
   translate1(L,Ltr,Baum),
   translate2(R,[],Rtr0,RevTrees),  % Acc = []
   % Rtr = (Rtr0, { reverse(RevTrees,Trees) }),
   % Baum = [L|Trees].
   Rtr = (Rtr0, { reverse(RevTrees,Trees),
                  Baum =.. [L|Trees] }). % bei functor(L,_,0)

% translate1(+Kategorie,-Term,?Baum)

translate1(Kategorie,Term,Baum) :-
    Kategorie =.. [Funktor|Argumente],
    append(Argumente,[Baum],MehrArgumente),
    Term =.. [Funktor|MehrArgumente].

% translate2(+Cat,+Akkum.Bäume,-CatTr,-AlleBäume)

translate2((A,B),Acc,(Atr,Btr),Trees) :-
    !,translate2(A,Acc, Atr,AccA),
      translate2(B,AccA,Btr,Trees).

translate2((A;B),Acc,(Atr;Btr),Trees) :-
    !,translate2(A,Acc,Atr0,AccA),
      translate2(B,Acc,Btr0,AccB),
      Atr = (Atr0,{ Trees=AccA }),
      Btr = (Btr0,{ Trees=AccB }).

translate2([Atom],Acc,[Atom],Trees) :-
    !,Trees = [Atom|Acc].

translate2({Prolog},Acc,{Prolog},Acc) :-
    !.

translate2(Cat,Acc,CatTr,Trees) :-
    !, translate1(Cat,CatTr,Baum),
    Trees = [Baum|Acc].

term_expansion(Term,Expandiert) :-
    translate(Term,Expandiert).

s --> np, vp.
vp --> v ; v, np.

np --> det, n.
det --> [der] ; [das].
n --> ['Programmierer'] ; ['Programm'].

v --> [steht].
v --> [schrieb].

