我正在尝试学习如何在prolog中编写生产规则。 这就是我所拥有的。
paragraph --> sentence, paragraph ; [].
sentence --> proper_noun, [ ], verb, [ ], preposition, [ ], article, [ ], noun, period.
proper_noun --> [Jimmy] ; [Yancy] ; [Clementine] ; [Astrid].
verb --> [runs] ; [walks] ; [skips] ; [flies].
preposition --> [to] ; [at] ; [around] ; [through].
article --> [the] ; [a].
noun --> [school] ; [house] ; [car] ; [spaceship].
period -->[.].
我尝试使用
调用它 phrase( sentence, [Jimmy," ",walks," ",to," ",the," ",school], [] ), atom_codes( Output,[Jimmy," ",walks," ",to," ",the," ",school]).
它返回 false 作为输出。请帮助我了解我哪里出错了以及如何编写更好的语法。
答案 0 :(得分:1)
使用'Jimmy'
等(即将所有以大写字母开头的原子放入引号)。否则它们被解释为逻辑变量。你会得到很多"单身变量"警告。
你也错过了一些括号,并且更多的拼写错误:
paragraph --> sentence, ( paragraph ; []).
sentence --> proper_noun, verb, preposition, article, noun, period.
proper_noun --> ['Jimmy'] ; ['Yancy'] ; ['Clementine'] ; ['Astrid'].
verb --> [runs] ; [walks] ; [skips] ; [flies].
preposition --> [to] ; [at] ; [around] ; [through].
article --> [the] ; [a].
noun --> [school] ; [house] ; [car] ; [spaceship].
period -->[.].
测试它:
30 ?- phrase( sentence, ['Jimmy', walks, to, the, school, .], [] ).
true ;
false.