/* READIN : Read in a sentence into a list of words

                                                UTILITY
                                                Lawrence
                                                Updated: 14 October 81

*/


        %%% READIN requires no other modules



read_in(P):-initread(L),words(P,L,[]),!.

initread([K1,K2|U]):-get(K1),get0(K2),readrest(K2,U).

readrest(46,L) :- !, possiblythere(46,L).
readrest(63,L) :- !, possiblythere(63,L).
readrest(33,L) :- !, possiblythere(33,L).
readrest(K,[K1|U]):-K=<32,!,get(K1),readrest(K1,U).
readrest(K1,[K2|U]):-get0(K2),readrest(K2,U).


  possiblythere(C,Rest)
        :- repeat,
           get0(Next),
           Next =\= 32,
           ( Next =:= 10,
             !,
             Rest = []          ;  Rest = [Next|More],
                                   readrest(Next,More)
           ).



words([V|U]) --> word(V),!,blanks,words(U).
words([]) --> [].

word(U1) --> [K],{ lc(K,K1) },!,alphanums(U2),{ name(U1,[K1|U2]) }.
word(N) --> [K],{ digit(K) },!,digits(U),{ name(N,[K|U]) }.
word(V) --> [K],{ name(V,[K]) }.

alphanums([K1|U]) --> [K],{ alphanum(K,K1) },!,alphanums(U).
alphanums([]) --> [].

alphanum(K,K1):-lc(K,K1).
alphanum(K,K):-digit(K).

digits([K|U]) --> [K],{ digit(K) },!,digits(U).
digits([]) --> [].

blanks--> [K],{ K=<32 },!,blanks.
blanks --> [].

digit(K):-K>47,K<58.

lc(K,K1):-K>64,K<91,!,K1 is K\/8'40.
lc(K,K):-K>96,K<123.

