Search code examples
prologgrammardcg

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


This is for a class assignment, but I can't figure out how to fix my problem. I wrote the following:

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].

And I did a trace:

[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

I think it is obviously failing when sentence is called but it is getting a [[x, =, 1], end] which it can't handle. I think the obvious answer is that [x, =, 1] is a first element of a list, but I can't figure out how to just pass that element to sentence so it will handle things correctly. I am very stumped.


Solution

  • The problem, as noted by @jschimpf, is that we are mixing two description levels in the initial call. Lists are metadata, and this is more evident rewriting the grammar in DCG notation. Once corrected the original formulation (was sentence--> f; t,n,t.) the test ?- g. passes.

    
    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]).
    

    edit

    Now we can fix the problem in the original code, where sentence/2 has the role that DCGs assign to phrase/2 :

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

    edit I have proposed what I think it's the more instructive correction of sentence/2, but we can avoid the 'impure' if/then/else construct, for instance

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

    should work (untested...)