read_in(Sent) :-
	write('Please enter a sentence:'), nl,
        next_character(C),
        start_word(C,Sent).

next_character(C) :-	
        get0(C1),
        no_capitals(C1,C).

no_capitals(Upper_case,Lower_case) :-
        Upper_case > 64,                /* 'A' .. 'Z' */
        Upper_case < 91, !,
        Lower_case is Upper_case + 32.
no_capitals(Other_characters,Other_characters).

start_word(46,[]) :- !.
start_word(63,_) :- !,
        write(' '), nl,
        write('Command cancelled'), nl, 
        write(' '), nl,
        fail.
start_word(C,Sent) :-
        separator(C), !,
        next_character(C1),
        start_word(C1,Sent).
start_word(C,[Word|Words]) :-
        read_word(C,List,C1),
        name(Word,List),
        start_word(C1,Words).
read_word(C,[C],C1) :-
        single_char(C), !,
        next_character(C1).
read_word(36,[36|Cs],C2) :- !,
        next_character(C1),
        rest_string(C1,Cs,C2).
read_word(C,W,C2) :-
        two_in_one(C), !,
        next_character(C1),
        combine_two(C,C1,C2,W).
read_word(C,[C|Cs],C2) :-
        next_character(C1),
        rest_word(C1,Cs,C2).

rest_string(C,[],C) :-
        separator(C), !.
rest_string(C,[C|Cs],C2) :-
        next_character(C1),
        rest_string(C1,Cs,C2).

combine_two(92,61,C,[92,61]) :- !,
        next_character(C).
combine_two(C,C1,C2,[C,C1]) :-
        C is C1 + 1, !,
        next_character(C2).
combine_two(C,C1,C1,[C]).

rest_word(C,[],C) :-
        (separator(C) ; single_char(C) ; two_in_one(C) ; C = 63 ), !.
rest_word(C,[C|Cs],C2) :-
        next_character(C1),
        rest_word(C1,Cs,C2).

separator(32).          /* 'sp' */
separator(10).          /* 'cr' */
separator(13).          /* 'lf' */
separator(9).           /* 'tab' */
single_char(40).        /* '(' */
single_char(41).        /* ')' */
single_char(42).        /* '*' */
single_char(43).        /* '+' */
single_char(45).        /* '-' */
single_char(46).        /* '.' */
single_char(47).        /* '/' */
single_char(60).        /* '<' */
two_in_one(61).         /* '=' */
two_in_one(62).         /* '>' */
two_in_one(92).
end_of_file.

