% Thang Le's input routine, Fall 2005 % %%%% File tokenizer.pl run(Infile, Outfile) :- readfile(Infile, X), token(Z, X, []), trans(Z, T), !, program(T, [], Y), !, open(Outfile, write, S), write(S, Y), close(S). readfile(In, X) :- atom(In), var(X), open(In, read, S), getfile(S, X), close(S). writefile(Out, Y) :- atom(Out), nonvar(Y), open(Out, write, S), writeq(S, Y), put(S, 46), close(S). getfile(S, [X|Y]) :- get0(S, X0), (-1==X0 -> Y=[];getfile(S, Y)), changecode(X0, X). changecode(-1, 32) :- !. changecode(9, 32) :- !. changecode(10, 32) :- !. changecode(X, X). trans([], []). trans([=,=|X], [==|Y]) :- trans(X, Y), !. trans([<,=|X], [<=|Y]) :- trans(X, Y), !. trans([>,=|X], [>=|Y]) :- trans(X, Y), !. trans([!,=|X], ['!='|Y]) :- trans(X, Y), !. trans([H|X], [H|Y]) :- trans(X, Y). gather(Chars) --> [C], {alphaNumeric(C)}, gather(Rest), {Chars = [C|Rest]}. gather([]) --> {true}. alphaNumeric(C) :- 96 gather(Chars), {\+(Chars=[])}, token(RestResult), {name(N, Chars), Result = [N|RestResult]}. token(R)--> [C], {C<33}, token(R). token([N|R]) --> [C], {C>32}, {name(N, [C])}, token(R). token([]) --> [].