Hi,
Relative newcomer to prolog. I’ve been experimenting with DCGs and hit
an oddity. As part of outputting the transformed input I wanted to add
the file and line where the input was found. I added:
lazy_list_location(Loc)
to my rule and Loc to the outputted term. At some point I discovered
the parser was occasionally hiccuping. Hours later I tracked it down
to using lazy_list_location//1. I read the docs to say that I could
call this at any time (but the use of “(error)” makes me wonder…)
Below is some sample code. Create an input file with 4 items. My sample
looks like (tab separated):
abcdefghi IN A 127.0.0.1
repeat the line so file size is > about 40K.
?- open(output,write,S),set_output(S),proc_file(test_data),close(S).
Search output file for “eh?” It’s notable, I suspect, that it loses track
very near 4K boundaries!
Happens on 8.0.1 and 7.6 both on Linux.
Is this one of those “surprising” things in Prolog? Is there a better
way to get file and location?
Cheers,
Eddie
a_space(Char):- char_type(Char, space).
ws --> [W], { a_space(W) }, ws.
ws --> [W], { a_space(W) }.
word_([H|T]) --> [H], {\+ a_space(H)}, word__(T).
word__([H|T]) --> [H], {\+ a_space(H)}, word__(T).
word__([]) --> [].
word(W) --> word_(W1), {atom_string(W1,W)}.
record --> [].
record --> word(A), ws, word(B), ws, word(C), ws, word(D), "\n",
lazy_list_location(Loc),
{writeln(record(A,B,C,D,Loc))}, record.
record --> [_], {writeln('eh?')}, record.
proc_file(F):- phrase_from_file(record, F).