:- module(tokenizer,[read_sentence/3,tokenize/2,
                     satzende/0,parse/0,parsed/0]).

% read_sentence(+Stream,-Sentence,+Seen)
read_sentence(Stream,Sentence,Seen) :-
    get0(Stream,Char),  
    read_sentence(Stream,Sentence,Char,Seen).

read_sentence(Stream,Sentence,Char,Seen) :-
    ((Seen = [C|Cs], satzende([C,Char]))
    -> reverse(Cs,Sentence)
    ; read_sentence(Stream,Sentence,[Char|Seen])).

satzende([46,10]).  % <Punkt><Return>

% tokenize(+String,-Atomlist).
tokenize(String,Atomlist) :-
    name(Atom,String),
    concat_atom(AtomlistK,' ',Atom),
    entferne_komma(AtomlistK,Atomlist).

entferne_komma([AtomK|AtomeK],[Atom|Atome]) :-
    (atom_concat(Atom,',',AtomK)
    -> true
    ; Atom = AtomK),
    entferne_komma(AtomeK,Atome).
entferne_komma([],[]).

satzende :-
    write('Beende die Eingabe mit <Punkt><Return>'),
    nl,read_sentence(user,Sentence,[]),
    name(Satz,Sentence),
    nl,write('Eingabe:   '),writeq(Satz),
    tokenize(Sentence,Atomlist),
    nl,write('Atomliste: '),writeq(user,Atomlist).

parse :-
    write('Beende die Eingabe mit <Punkt><Return>'),
    nl,read_sentence(user,Sentence,[]),
    tokenize(Sentence,Atomlist),
    startsymbol(S),
    addTree:translate1(S,Term,Baum),
    Term =.. [F|Args],
    append(Args,[Atomlist,[]],NewArgs),
    NewTerm =.. [F|NewArgs],
    nl,write('Aufruf: '), portray_clause(NewTerm),
    call(NewTerm), write('Baum:   '), writeq(Baum),nl,
    fail.
parse.
parsed :-
    write('Beende die Eingabe mit <Punkt><Return>'),
    nl,read_sentence(user,Sentence,[]),
    tokenize(Sentence,Atomlist),
    setof(Baum, parse(Atomlist, Baum), Trees),
    displayTrees(Trees).

parse(Atomlist,Baum) :-
    startsymbol(S),
    addTree:translate1(S,Term,Baum),
    Term =.. [F|Args],
    append(Args,[Atomlist,[]],NewArgs),
    NewTerm =.. [F|NewArgs],
    call(NewTerm).
    
