在prolog中读取文件 [英] Reading a file in prolog
本文介绍了在prolog中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
在Prolog中逐行阅读文件
我找到了以下prolog代码,它一次读取一个字符并打印出来。
I found the following prolog code which reads one character at a time and prints out.
process(File) :-
open('C:/Users/BHARAT/Desktop/a.txt', read, In),
get_char(In, Char1),
process_stream(Char1, In),
close(In).
process_stream(end_of_file, _) :- !.
process_stream(Char, In) :-
print(Char),
get_char(In, Char2),
process_stream(Char2, In).
但是如果文件有多行,那么有一种方法可以一次读取1行,这样标记化很容易。
But if the file has multiple lines is there a way to read 1 whole line at a time so that it will be easy for tokenizing.
推荐答案
你说你想要对输入进行标记化 - 最好的方法是明确条款语法(DCG)。使用SWI中的库(pio)
,您可以直接使用语法来读取文件,如下所示:
You say you want to tokenize the input - the best way to do this are definite clause grammars (DCGs). With library(pio)
in SWI you can use the grammar directly to read in a file like so:
?- use_module(library(pio)).
?- phrase_from_file(seq(Xs),f).
seq([]) --> [].
seq([E|Es]) --> [E], seq(Es).
立即替换 seq // 1
更精细的标记器。
Replace now seq//1
by some more elaborate tokenizer.
这篇关于在prolog中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文