我的 PROLOG 作业有一些问题。
即,我必须编写谓词accept(+Automaton, +Word),如果Word属于语言,则返回true(Automaton由Goto表,Action表和语法表示,但不是那么重要),否则返回false(它是必需的在我的任务中返回真或假)。我已经写了
accept(Automaton, Word) :-
parse(Automaton, Word, RightmostDeriv),
nl, print_list(RightmostDeriv), nl.
在哪里
谓词parse(+Automaton, +Word, -RightmostDeriv)
计算最右推导。如果 Word 无法解析,则RighmostDeriv == ['mistake']
如您所见,我可以打印最正确的推导,但我不知道如何返回 false 或 true。
我发现了这个https://csc.cdf.toronto.edu/mybb/showthread.php?tid=2100,我发现这里可能是个问题。
但也许这里有任何解决方案(例如,使用成员谓词,类似于 member('mistake', RightmostDeriv) )来返回真或假?
编辑:有
accept(Automaton, Word) :-
parse(Automaton, Word, RightmostDeriv),
nl, print(RightmostDeriv), nl.
我收到了(测试 2 - 来自维基百科http://en.wikipedia.org/wiki/LR_parser#Additional_Example_1.2B1的示例)
[4] ?- make.
=============== Test 1 ===================
[mistake]
=============== Test 2 ===================
[5,3,5,2]
true .
[4] ?-
并具有:
accept(Automaton, Word) :-
parse(Automaton, Word, RightmostDeriv),
RightmostDeriv \= ['mistake'].
我收到(无限循环)
?- make.
=============== Test 1 ===================
^CAction (h for help) ? break
% Break level 1
[1] ?-