我想在Prolog(gnu)中编写一个小程序,它将接受用户输入并给出真实或错误的问题“这是这个语法中的有效句子吗?”我在寻找关于Prolog的可靠文档方面遇到了很多麻烦,如果有人有可靠的来源,我会感激。从我发现的情况来看,这个代码大部分都应该工作。当我试图追查执行情况时,我得到了我不明白的奇怪结果。Prolog循环重做的
我现在正在使用的测试用例是当用户输入'a =(b + c)'时。问题是,从列表中删除最后一项导致重做循环,我不确定原因。
% Grammar:
% <assign> → <id> = <expr>
% <expr> → <id> <op> <expr>
% | (<expr>)
% | <id>
% <op> → * | + |/| -
% <id> → a | b | c
op('+').
op('-').
op('*').
op('/').
id(a).
id(b).
id(c).
id('a').
id('b').
id('c').
id([a]).
id([b]).
id([c]).
%
% Determine if the Sentence is valid according to the grammar
%
% param (in): Sentence - A list of symbols that make up the sentence to be evaluated
%
assign(Sentence) :-
list(Sentence), length(Sentence, Length), >=(Length, 3), %precondition test
=(Sentence, [First, Second | Tail]),
id(First),
=(Second, '='),
expr(Tail).
% Test if the list of symbols is a valid expression
expr(X) :-
length(X, Length), Length >= 1,
=(X, [First | Tail]),
(Length = 1 ->
id(X)
; Length >= 3, =(First,'(') ->
=(Tail, [Second | Tail2]),
last(Last, Tail),
append(Middle, [_], Tail),
=(Last, ')'),
expr(Middle)
; =(Tail, [Second | Tail2]),
id(First),
op(Second),
expr(Tail2)
).
%
% Input a sentence, then decompose it into a list of symbols.
% User input should be quoted, e.g., Enter a sentence: 'A = B * C'
%
% param (out): SentenceList The list of symbols input by the user is bound to this variable
%
read_sentence(SentenceList) :-
print('Enter a sentence: '),
read_token(Input),
write_term_to_chars(InputList, Input, []),
delete(InputList, ' ', SentenceList).
%
% Start the program
%
start :-
read_sentence(Sentence),
assign(Sentence).
任何理由为什么你的目标没有用户操作的符号,那就是'长度= 1 '代替'=(长度,1)'等。 – false
我模仿我在课堂上教过的东西,没有语法上的理由。我想知道这两种方法之间是否存在显着差异,或者是否有人喜欢。 – user3308726
功能符号很难阅读。 – false