在 prolog 中编写语法但无法使其正常工作

Writting grammar in prolog and can't get it to work correctly

这是一项 class 作业,但我不知道如何解决我的问题。 我写了以下内容:

sentence(S0, S):- f(S0,S); t(S0, S1),n(S1, S2),t(S2, S).
f(S0, S):- termIf(S0, S1), pLeft(S1, S2), b(S2, S3), pRight(S3, S4), termThen(S4, S5), termBegin(S5, S6),sentence(S6, S7), termEnd(S7, S); termIf(S0, S1), pLeft(S1, S2), b(S2, S3), pRight(S3, S4), termThen(S4, S5),termBegin(S5, S6),sentence(S6, S7), termElse(S7, S8), sentence(S8, S9), termEnd(S9, S).
b(S0, S):- t(S0, S1), e(S1, S2), t(S2, S). 
termIf(S0, S) :- S0=[if|S].
termThen(S0, S):- S0=[then|S].
termBegin(S0, S):- S0=[begin|S].
termEnd(S0, S):- S0=[end|S].
termElse(S0, S):- S0=[else|S].
pLeft(S0, S):- S0=['('|S].
pRight(S0, S):- S0=[')'|S].
t(S0, S):- S0=[x|S].
t(S0, S):- S0=[y|S].
t(S0, S):- S0=[z|S].
t(S0, S):- S0=[1|S].
t(S0, S):- S0=[0|S].
e(S0, S):- S0=[>|S].
e(S0, S):- S0=[<|S].
n(S0, S):- S0=[+|S].
n(S0, S):- S0=[-|S].
n(S0, S):- S0=[=|S].

然后我做了追踪:

[trace] [5]  ?- sentence([if,'(', x, >, 0,')', then, begin, [x, =, 1], end], X).
   Call: (89) sentence([if, '(', x, >, 0, ')', then, begin|...], _66584) ? creep
   Call: (90) f([if, '(', x, >, 0, ')', then, begin|...], _66584) ? creep
   Call: (91) termIf([if, '(', x, >, 0, ')', then, begin|...], _67166) ? creep
   Call: (92) [if, '(', x, >, 0, ')', then, begin|...]=[if|_67158] ? creep
   Exit: (92) [if, '(', x, >, 0, ')', then, begin|...]=[if, '(', x, >, 0, ')', then, begin|...] ? creep
   Exit: (91) termIf([if, '(', x, >, 0, ')', then, begin|...], ['(', x, >, 0, ')', then, begin, [...|...]|...]) ? creep
   Call: (91) pLeft(['(', x, >, 0, ')', then, begin, [...|...]|...], _67348) ? creep
   Call: (92) ['(', x, >, 0, ')', then, begin, [...|...]|...]=['('|_67340] ? creep
   Exit: (92) ['(', x, >, 0, ')', then, begin, [...|...]|...]=['(', x, >, 0, ')', then, begin, [...|...]|...] ? creep
   Exit: (91) pLeft(['(', x, >, 0, ')', then, begin, [...|...]|...], [x, >, 0, ')', then, begin, [x|...], end]) ? creep
   Call: (91) b([x, >, 0, ')', then, begin, [x|...], end], _67530) ? creep
   Call: (92) t([x, >, 0, ')', then, begin, [x|...], end], _67574) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[x|_67566] ? creep
   Exit: (93) [x, >, 0, ')', then, begin, [x|...], end]=[x, >, 0, ')', then, begin, [x|...], end] ? creep
   Exit: (92) t([x, >, 0, ')', then, begin, [x|...], end], [>, 0, ')', then, begin, [x, =|...], end]) ? creep
   Call: (92) e([>, 0, ')', then, begin, [x, =|...], end], _67756) ? creep
   Call: (93) [>, 0, ')', then, begin, [x, =|...], end]=[>|_67748] ? creep
   Exit: (93) [>, 0, ')', then, begin, [x, =|...], end]=[>, 0, ')', then, begin, [x, =|...], end] ? creep
   Exit: (92) e([>, 0, ')', then, begin, [x, =|...], end], [0, ')', then, begin, [x, =, 1], end]) ? creep
   Call: (92) t([0, ')', then, begin, [x, =, 1], end], _67938) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[x|_67930] ? creep
   Fail: (93) [0, ')', then, begin, [x, =, 1], end]=[x|_67930] ? creep
   Redo: (92) t([0, ')', then, begin, [x, =, 1], end], _68076) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[y|_68068] ? creep
   Fail: (93) [0, ')', then, begin, [x, =, 1], end]=[y|_68068] ? creep
   Redo: (92) t([0, ')', then, begin, [x, =, 1], end], _68214) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[z|_68206] ? creep
   Fail: (93) [0, ')', then, begin, [x, =, 1], end]=[z|_68206] ? creep
   Redo: (92) t([0, ')', then, begin, [x, =, 1], end], _68352) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[1|_68344] ? creep
   Fail: (93) [0, ')', then, begin, [x, =, 1], end]=[1|_68344] ? creep
   Redo: (92) t([0, ')', then, begin, [x, =, 1], end], _68490) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[0|_68482] ? creep
   Exit: (93) [0, ')', then, begin, [x, =, 1], end]=[0, ')', then, begin, [x, =, 1], end] ? creep
   Exit: (92) t([0, ')', then, begin, [x, =, 1], end], [')', then, begin, [x, =, 1], end]) ? creep
   Exit: (91) b([x, >, 0, ')', then, begin, [x|...], end], [')', then, begin, [x, =, 1], end]) ? creep
   Call: (91) pRight([')', then, begin, [x, =, 1], end], _68716) ? creep
   Call: (92) [')', then, begin, [x, =, 1], end]=[')'|_68708] ? creep
   Exit: (92) [')', then, begin, [x, =, 1], end]=[')', then, begin, [x, =, 1], end] ? creep
   Exit: (91) pRight([')', then, begin, [x, =, 1], end], [then, begin, [x, =, 1], end]) ? creep
   Call: (91) termThen([then, begin, [x, =, 1], end], _68898) ? creep
   Call: (92) [then, begin, [x, =, 1], end]=[then|_68890] ? creep
   Exit: (92) [then, begin, [x, =, 1], end]=[then, begin, [x, =, 1], end] ? creep
   Exit: (91) termThen([then, begin, [x, =, 1], end], [begin, [x, =, 1], end]) ? creep
   Call: (91) termBegin([begin, [x, =, 1], end], _69080) ? creep
   Call: (92) [begin, [x, =, 1], end]=[begin|_69072] ? creep
   Exit: (92) [begin, [x, =, 1], end]=[begin, [x, =, 1], end] ? creep
   Exit: (91) termBegin([begin, [x, =, 1], end], [[x, =, 1], end]) ? creep
   Call: (91) sentence([[x, =, 1], end], _69262) ? creep
   Call: (92) f([[x, =, 1], end], _69306) ? creep
   Call: (93) termIf([[x, =, 1], end], _69350) ? creep
   Call: (94) [[x, =, 1], end]=[if|_69342] ? creep
   Fail: (94) [[x, =, 1], end]=[if|_69342] ? creep
   Fail: (93) termIf([[x, =, 1], end], _69488) ? creep
   Redo: (92) f([[x, =, 1], end], _69532) ? creep
   Call: (93) termIf([[x, =, 1], end], _69576) ? creep
   Call: (94) [[x, =, 1], end]=[if|_69568] ? creep
   Fail: (94) [[x, =, 1], end]=[if|_69568] ? creep
   Fail: (93) termIf([[x, =, 1], end], _69714) ? creep
   Fail: (92) f([[x, =, 1], end], _69758) ? creep
   Redo: (91) sentence([[x, =, 1], end], _69802) ? creep
   Call: (92) t([[x, =, 1], end], _69846) ? creep
   Call: (93) [[x, =, 1], end]=[x|_69838] ? creep
   Fail: (93) [[x, =, 1], end]=[x|_69838] ? creep
   Redo: (92) t([[x, =, 1], end], _69984) ? creep
   Call: (93) [[x, =, 1], end]=[y|_69976] ? creep
   Fail: (93) [[x, =, 1], end]=[y|_69976] ? creep
   Redo: (92) t([[x, =, 1], end], _70122) ? creep
   Call: (93) [[x, =, 1], end]=[z|_70114] ? creep
   Fail: (93) [[x, =, 1], end]=[z|_70114] ? creep
   Redo: (92) t([[x, =, 1], end], _70260) ? creep
 Call: (93) [[x, =, 1], end]=[1|_70252] ? creep
   Fail: (93) [[x, =, 1], end]=[1|_70252] ? creep
   Redo: (92) t([[x, =, 1], end], _70398) ? creep
   Call: (93) [[x, =, 1], end]=[0|_70390] ? creep
   Fail: (93) [[x, =, 1], end]=[0|_70390] ? creep
   Fail: (92) t([[x, =, 1], end], _70536) ? creep
   Fail: (91) sentence([[x, =, 1], end], _70580) ? creep
   Redo: (92) e([>, 0, ')', then, begin, [x, =|...], end], _70624) ? creep
   Call: (93) [>, 0, ')', then, begin, [x, =|...], end]=[<|_70616] ? creep
   Fail: (93) [>, 0, ')', then, begin, [x, =|...], end]=[<|_70616] ? creep
   Fail: (92) e([>, 0, ')', then, begin, [x, =|...], end], _70762) ? creep
   Redo: (92) t([x, >, 0, ')', then, begin, [x|...], end], _70806) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[y|_70798] ? creep
   Fail: (93) [x, >, 0, ')', then, begin, [x|...], end]=[y|_70798] ? creep
   Redo: (92) t([x, >, 0, ')', then, begin, [x|...], end], _70944) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[z|_70936] ? creep
   Fail: (93) [x, >, 0, ')', then, begin, [x|...], end]=[z|_70936] ? creep
   Redo: (92) t([x, >, 0, ')', then, begin, [x|...], end], _71082) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[1|_71074] ? creep
   Fail: (93) [x, >, 0, ')', then, begin, [x|...], end]=[1|_71074] ? creep
   Redo: (92) t([x, >, 0, ')', then, begin, [x|...], end], _71220) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[0|_71212] ? creep
   Fail: (93) [x, >, 0, ')', then, begin, [x|...], end]=[0|_71212] ? creep
   Fail: (92) t([x, >, 0, ')', then, begin, [x|...], end], _71358) ? creep
   Fail: (91) b([x, >, 0, ')', then, begin, [x|...], end], _71402) ? creep
   Redo: (90) f([if, '(', x, >, 0, ')', then, begin|...], _66584) ? creep
   Call: (91) termIf([if, '(', x, >, 0, ')', then, begin|...], _71490) ? creep
   Call: (92) [if, '(', x, >, 0, ')', then, begin|...]=[if|_71482] ? creep
   Exit: (92) [if, '(', x, >, 0, ')', then, begin|...]=[if, '(', x, >, 0, ')', then, begin|...] ? creep
   Exit: (91) termIf([if, '(', x, >, 0, ')', then, begin|...], ['(', x, >, 0, ')', then, begin, [...|...]|...]) ? creep
   Call: (91) pLeft(['(', x, >, 0, ')', then, begin, [...|...]|...], _71672) ? creep
   Call: (92) ['(', x, >, 0, ')', then, begin, [...|...]|...]=['('|_71664] ? creep
   Exit: (92) ['(', x, >, 0, ')', then, begin, [...|...]|...]=['(', x, >, 0, ')', then, begin, [...|...]|...] ? creep
   Exit: (91) pLeft(['(', x, >, 0, ')', then, begin, [...|...]|...], [x, >, 0, ')', then, begin, [x|...], end]) ? creep
   Call: (91) b([x, >, 0, ')', then, begin, [x|...], end], _71854) ? creep
   Call: (92) t([x, >, 0, ')', then, begin, [x|...], end], _71898) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[x|_71890] ? creep
   Exit: (93) [x, >, 0, ')', then, begin, [x|...], end]=[x, >, 0, ')', then, begin, [x|...], end] ? creep
   Exit: (92) t([x, >, 0, ')', then, begin, [x|...], end], [>, 0, ')', then, begin, [x, =|...], end]) ? creep
   Call: (92) e([>, 0, ')', then, begin, [x, =|...], end], _72080) ? creep
   Call: (93) [>, 0, ')', then, begin, [x, =|...], end]=[>|_72072] ? creep
   Exit: (93) [>, 0, ')', then, begin, [x, =|...], end]=[>, 0, ')', then, begin, [x, =|...], end] ? creep
   Exit: (92) e([>, 0, ')', then, begin, [x, =|...], end], [0, ')', then, begin, [x, =, 1], end]) ? creep
   Call: (92) t([0, ')', then, begin, [x, =, 1], end], _72262) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[x|_72254] ? creep
   Fail: (93) [0, ')', then, begin, [x, =, 1], end]=[x|_72254] ? creep
   Redo: (92) t([0, ')', then, begin, [x, =, 1], end], _72400) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[y|_72392] ? creep
   Fail: (93) [0, ')', then, begin, [x, =, 1], end]=[y|_72392] ? creep
   Redo: (92) t([0, ')', then, begin, [x, =, 1], end], _72538) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[z|_72530] ? creep
   Fail: (93) [0, ')', then, begin, [x, =, 1], end]=[z|_72530] ? creep
   Redo: (92) t([0, ')', then, begin, [x, =, 1], end], _72676) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[1|_72668] ? creep
   Fail: (93) [0, ')', then, begin, [x, =, 1], end]=[1|_72668] ? creep
   Redo: (92) t([0, ')', then, begin, [x, =, 1], end], _72814) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[0|_72806] ? creep
   Exit: (93) [0, ')', then, begin, [x, =, 1], end]=[0, ')', then, begin, [x, =, 1], end] ? creep
   Exit: (92) t([0, ')', then, begin, [x, =, 1], end], [')', then, begin, [x, =, 1], end]) ? creep
   Exit: (91) b([x, >, 0, ')', then, begin, [x|...], end], [')', then, begin, [x, =, 1], end]) ? creep
   Call: (91) pRight([')', then, begin, [x, =, 1], end], _73040) ? creep
   Call: (92) [')', then, begin, [x, =, 1], end]=[')'|_73032] ? creep
   Exit: (92) [')', then, begin, [x, =, 1], end]=[')', then, begin, [x, =, 1], end] ? creep
   Exit: (91) pRight([')', then, begin, [x, =, 1], end], [then, begin, [x, =, 1], end]) ? creep
   Call: (91) termThen([then, begin, [x, =, 1], end], _73222) ? creep
   Call: (92) [then, begin, [x, =, 1], end]=[then|_73214] ? creep
   Exit: (92) [then, begin, [x, =, 1], end]=[then, begin, [x, =, 1], end] ? creep
   Exit: (91) termThen([then, begin, [x, =, 1], end], [begin, [x, =, 1], end]) ? creep
   Call: (91) termBegin([begin, [x, =, 1], end], _73404) ? creep
   Call: (92) [begin, [x, =, 1], end]=[begin|_73396] ? creep
   Exit: (92) [begin, [x, =, 1], end]=[begin, [x, =, 1], end] ? creep
   Exit: (91) termBegin([begin, [x, =, 1], end], [[x, =, 1], end]) ? creep
   Call: (91) sentence([[x, =, 1], end], _73586) ? creep
   Call: (92) f([[x, =, 1], end], _73630)
Ԁ   Fail: (93) [0, ')', then, begin, [x, =, 1], end]=[1|_72668] ? creep
   Redo: (92) t([0, ')', then, begin, [x, =, 1], end], _72814) ? creep
   Call: (93) [0, ')', then, begin, [x, =, 1], end]=[0|_72806] ? creep
   Exit: (93) [0, ')', then, begin, [x, =, 1], end]=[0, ')', then, begin, [x, =, 1], end] ? creep
   Exit: (92) t([0, ')', then, begin, [x, =, 1], end], [')', then, begin, [x, =, 1], end]) ? creep
   Exit: (91) b([x, >, 0, ')', then, begin, [x|...], end], [')', then, begin, [x, =, 1], end]) ? creep
   Call: (91) pRight([')', then, begin, [x, =, 1], end], _73040) ? creep
   Call: (92) [')', then, begin, [x, =, 1], end]=[')'|_73032] ? creep
   Exit: (92) [')', then, begin, [x, =, 1], end]=[')', then, begin, [x, =, 1], end] ? creep
   Exit: (91) pRight([')', then, begin, [x, =, 1], end], [then, begin, [x, =, 1], end]) ? creep
   Call: (91) termThen([then, begin, [x, =, 1], end], _73222) ? creep
   Call: (92) [then, begin, [x, =, 1], end]=[then|_73214] ? creep
   Exit: (92) [then, begin, [x, =, 1], end]=[then, begin, [x, =, 1], end] ? creep
   Exit: (91) termThen([then, begin, [x, =, 1], end], [begin, [x, =, 1], end]) ? creep
   Call: (91) termBegin([begin, [x, =, 1], end], _73404) ? creep
   Call: (92) [begin, [x, =, 1], end]=[begin|_73396] ? creep
   Exit: (92) [begin, [x, =, 1], end]=[begin, [x, =, 1], end] ? creep
   Exit: (91) termBegin([begin, [x, =, 1], end], [[x, =, 1], end]) ? creep
   Call: (91) sentence([[x, =, 1], end], _73586) ? creep
   Call: (92) f([[x, =, 1], end], _73630) ? creep
   Call: (93) termIf([[x, =, 1], end], _73674) ? creep
   Call: (94) [[x, =, 1], end]=[if|_73666] ? creep
   Fail: (94) [[x, =, 1], end]=[if|_73666] ? creep
   Fail: (93) termIf([[x, =, 1], end], _73812) ? creep
   Redo: (92) f([[x, =, 1], end], _73856) ? creep
   Call: (93) termIf([[x, =, 1], end], _73900) ? creep
   Call: (94) [[x, =, 1], end]=[if|_73892] ? creep
   Fail: (94) [[x, =, 1], end]=[if|_73892] ? creep
   Fail: (93) termIf([[x, =, 1], end], _74038) ? creep
   Fail: (92) f([[x, =, 1], end], _74082) ? creep
   Redo: (91) sentence([[x, =, 1], end], _74126) ? creep
   Call: (92) t([[x, =, 1], end], _74170) ? creep
   Call: (93) [[x, =, 1], end]=[x|_74162] ? creep
   Fail: (93) [[x, =, 1], end]=[x|_74162] ? creep
   Redo: (92) t([[x, =, 1], end], _74308) ? creep
   Call: (93) [[x, =, 1], end]=[y|_74300] ? creep
   Fail: (93) [[x, =, 1], end]=[y|_74300] ? creep
   Redo: (92) t([[x, =, 1], end], _74446) ? creep
   Call: (93) [[x, =, 1], end]=[z|_74438] ? creep
   Fail: (93) [[x, =, 1], end]=[z|_74438] ? creep
   Redo: (92) t([[x, =, 1], end], _74584) ? creep
   Call: (93) [[x, =, 1], end]=[1|_74576] ? creep
   Fail: (93) [[x, =, 1], end]=[1|_74576] ? creep
   Redo: (92) t([[x, =, 1], end], _74722) ? creep
   Call: (93) [[x, =, 1], end]=[0|_74714] ? creep
   Fail: (93) [[x, =, 1], end]=[0|_74714] ? creep
   Fail: (92) t([[x, =, 1], end], _74860) ? creep
   Fail: (91) sentence([[x, =, 1], end], _74904) ? creep
   Redo: (92) e([>, 0, ')', then, begin, [x, =|...], end], _74948) ? creep
   Call: (93) [>, 0, ')', then, begin, [x, =|...], end]=[<|_74940] ? creep
   Fail: (93) [>, 0, ')', then, begin, [x, =|...], end]=[<|_74940] ? creep
   Fail: (92) e([>, 0, ')', then, begin, [x, =|...], end], _75086) ? creep
   Redo: (92) t([x, >, 0, ')', then, begin, [x|...], end], _75130) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[y|_75122] ? creep
   Fail: (93) [x, >, 0, ')', then, begin, [x|...], end]=[y|_75122] ? creep
   Redo: (92) t([x, >, 0, ')', then, begin, [x|...], end], _75268) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[z|_75260] ? creep
   Fail: (93) [x, >, 0, ')', then, begin, [x|...], end]=[z|_75260] ? creep
   Redo: (92) t([x, >, 0, ')', then, begin, [x|...], end], _75406) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[1|_75398] ? creep
   Fail: (93) [x, >, 0, ')', then, begin, [x|...], end]=[1|_75398] ? creep
   Redo: (92) t([x, >, 0, ')', then, begin, [x|...], end], _75544) ? creep
   Call: (93) [x, >, 0, ')', then, begin, [x|...], end]=[0|_75536] ? creep
   Fail: (93) [x, >, 0, ')', then, begin, [x|...], end]=[0|_75536] ? creep
   Fail: (92) t([x, >, 0, ')', then, begin, [x|...], end], _75682) ? creep
   Fail: (91) b([x, >, 0, ')', then, begin, [x|...], end], _75726) ? creep
   Fail: (90) f([if, '(', x, >, 0, ')', then, begin|...], _66584) ? creep
   Redo: (89) sentence([if, '(', x, >, 0, ')', then, begin|...], _66584) ? creep
   Call: (90) t([if, '(', x, >, 0, ')', then, begin|...], _75858) ? creep
   Call: (91) [if, '(', x, >, 0, ')', then, begin|...]=[x|_75850] ? creep
   Fail: (91) [if, '(', x, >, 0, ')', then, begin|...]=[x|_75850] ? creep
   Redo: (90) t([if, '(', x, >, 0, ')', then, begin|...], _75996) ? creep
   Call: (91) [if, '(', x, >, 0, ')', then, begin|...]=[y|_75988] ? creep
   Fail: (91) [if, '(', x, >, 0, ')', then, begin|...]=[y|_75988] ? creep
   Redo: (90) t([if, '(', x, >, 0, ')', then, begin|...], _76134) ? creep
   Call: (91) [if, '(', x, >, 0, ')', then, begin|...]=[z|_76126]  
 Fail: (91) [if, '(', x, >, 0, ')', then, begin|...]=[z|_76126] ? creep
   Redo: (90) t([if, '(', x, >, 0, ')', then, begin|...], _76272) ? creep
   Call: (91) [if, '(', x, >, 0, ')', then, begin|...]=[1|_76264] ? creep
   Fail: (91) [if, '(', x, >, 0, ')', then, begin|...]=[1|_76264] ? creep
   Redo: (90) t([if, '(', x, >, 0, ')', then, begin|...], _76410) ? creep
   Call: (91) [if, '(', x, >, 0, ')', then, begin|...]=[0|_76402] ? creep
   Fail: (91) [if, '(', x, >, 0, ')', then, begin|...]=[0|_76402] ? creep
   Fail: (90) t([if, '(', x, >, 0, ')', then, begin|...], _76548) ? creep
   Fail: (89) sentence([if, '(', x, >, 0, ')', then, begin|...], _66584) ? creep
false

我认为调用 sentence 时显然失败了,但它得到了一个无法处理的 [[x, =, 1], end]。我认为显而易见的答案是 [x, =, 1] 是列表的第一个元素,但我不知道如何将该元素传递给 sentence 以便它正确处理事情。我很郁闷

该代码需要一个简单的终端列表,但您在其中有一个嵌套的 [x,=,1]。你要的是

?- sentence([if, '(', x, >, 0, ')', then, begin, x, =, 1, end], X).
X = []
Yes (0.00s cpu, solution 1, maybe more)
No (0.00s cpu)

正如@jschimpf 指出的那样,问题在于我们在初始调用中混合了两个描述级别。列表是元数据,这更明显地重写了 DCG 符号中的语法。更正原始公式(sentence--> f; t,n,t.)后,测试 ?- g. 通过。


sentence--> f; [L], {phrase((t,n,t),L)}.
f--> termIf, pLeft, b, pRight, termThen, termBegin, sentence, termEnd.
f--> termIf, pLeft, b, pRight, termThen, termBegin, sentence, termElse, sentence, termEnd.
b--> t, e, t.
termIf--> [if].
termThen--> [then].
termBegin--> [begin].
termEnd--> [end].
termElse--> [else].
pLeft--> ['('].
pRight--> [')'].
t--> [x].
t--> [y].
t--> [z].
t--> [1].
t--> [0].
e--> [>].
e--> [<].
n--> [+].
n--> [-].
n--> [=].

g :- phrase(sentence, [if,'(', x, >, 0,')', then, begin, [x, =, 1], end]).

编辑

现在我们可以修复原始代码中的问题,其中 sentence/2 具有 DCG 分配给 phrase/2 的角色:

sentence(S0,S):-
  S0=[H|T],
  (  \+is_list(H)
  -> f(S0,S)
  ;  t(H,S1),n(S1,S2),t(S2,[]),
     S=T
  ).

编辑 我提出了我认为对 sentence/2 更有指导意义的修正,但我们可以避免 'impure' if/then/else 结构,例如

sentence(S0,S):-
  f(S0,S);S0=[H|S],H=[_|_],t(H,S1),n(S1,S2),t(S2,[]).

应该可以工作(未经测试...)

如何仅靠自己定位错误?一种可能性是 概括 您的查询。只要您的程序是纯粹且单调的,以下内容就成立:

if a generalized query fails, then also the more specialized query will fail

因此,此概括与失败相关

通过用 _ 替换术语,我获得了以下仍然失败的查询(另外,我将您的代码翻译成 DCG 形式)...

?- phrase(sentence,[if,'(', x, >, 0,')', then, begin, [x, =, 1], end]).
false.

?- phrase(sentence,[_ ,  _, _, _, _,  _,    _,     _, [_|  _  ]|   _]).
false.

sentence--> f ; t, n, t.

(加上 给出的其余规则)

从这个概括我们可以看出,没有一个句子的第 9 个元素是至少包含一个元素的列表。

另请注意,您现在可以枚举所有句子!

?- length(L,_), phrase(sentence,L).
   L = [x, +, x]
;  L = [x, +, y]
;  L = [x, +, z]
;  L = [x, +, 1]
;  L = [x, +, 0]
;  ...