Prolog语法产生式

Prolog grammar productions

我正在尝试学习如何在序言中编写生产规则。 这就是我的。

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 作为输出。请帮助我了解哪里出了问题,以及如何才能写出更好的语法。

使用 'Jimmy' 等(即将所有以大写字母开头的原子放入引号中)。否则它们被解释为逻辑变量。你确实收到了很多 "Singleton variables" 警告。

你还遗漏了一些括号,并且错别字更多:

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.