Prolog 序言错误:demograph.grf:9:16:语法错误:文件意外结束

Prolog 序言错误:demograph.grf:9:16:语法错误:文件意外结束,prolog,swi-prolog,Prolog,Swi Prolog,我得到这个错误: 错误:demograph.grf:9:16:语法错误:文件意外结束 file_to_list(FILE,LIST) :- see(FILE), inquire([],R), % gather terms from file reverse(R,LIST), seen. inquire(IN,OUT):- read(Data), (Data == end_of_file -> % done OUT = IN

我得到这个错误: 错误:demograph.grf:9:16:语法错误:文件意外结束

file_to_list(FILE,LIST) :- 
   see(FILE), 
   inquire([],R), % gather terms from file
   reverse(R,LIST),
   seen.

inquire(IN,OUT):-
   read(Data), 
   (Data == end_of_file ->   % done
      OUT = IN 
        ;    % more
      inquire([Data|IN],OUT) ) .
输入文件

不完全是我想要的。输出应如下所示:

Lss =
[[0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 1, 1, 0],[0, 0, 0, 0, 0, 1, 0, 0, 0],[0, 1, 0, 0, 1, 0, 0, 1, 0],[0, 0, 0, 1, 0, 1, 0, 1, 1], [0, 0, 1, 0, 1, 0, 0, 0, 1],[0, 1, 0, 0, 0, 0, 0, 1, 0], [0, 1, 0, 1, 1, 0, 1, 0, 1],[0, 0, 0, 0, 1, 1, 0, 1, 0]] .

现在我一行一行地得到它

但它不是0,而是写48,一个写49,一个写32。它与0,1和空格之间的关系如何

process(File) :-
open(File, read, In),
read_line_to_codes(In, Char1),
process_stream(Char1, In),
close(In).

process_stream(end_of_file, _) :- !.
process_stream('\n', _) :- !.
process_stream(Char, In) :-
process_print(Char, In),
%get_char(In, Char2),
read_line_to_codes(In, Char2),
process_stream(Char2, In).
process_print(Char, _) :-
print(Char),print(' nextChar'),nl.

看见Prolog
read/1
希望从文件中读取一个Prolog术语。您正在读取的
.grf
文件没有完整的Prolog术语,因此在读取一个术语之前,它会到达文件的末尾。由于您的输入文件不包含Prolog术语,您应该使用逐字符读取。
请参阅
请参阅
get
这些都是Prolog中完全过时的内置程序,而且已经有20多年了。使用
open/3
close/1
和(如果需要)获取代码。
process(File) :-
open(File, read, In),
read_line_to_codes(In, Char1),
process_stream(Char1, In),
close(In).

process_stream(end_of_file, _) :- !.
process_stream('\n', _) :- !.
process_stream(Char, In) :-
process_print(Char, In),
%get_char(In, Char2),
read_line_to_codes(In, Char2),
process_stream(Char2, In).
process_print(Char, _) :-
print(Char),print(' nextChar'),nl.