Prolog 谓词调用
在以下教程中: http://www.csupomona.edu/~jrfisher/ www/prolog_tutorial/7_3.html
有一部分:
test_parser :- repeat,
write('?? '),
read_line(X),
( c(F,X,[]) | q(F,X,[]) ),
nl, write(X), nl, write(F), nl, fail.
现在我对 c(F,X,[]) 和 q(F,X,[]) 部分非常困惑,因为它似乎与我所看到的任何东西都不匹配,c 仅从我所知道的情况中获取一个参数,而这些参数似乎对 q 没有意义。请帮助我了解这里发生了什么。
In the following tutorial: http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/7_3.html
There is the part:
test_parser :- repeat,
write('?? '),
read_line(X),
( c(F,X,[]) | q(F,X,[]) ),
nl, write(X), nl, write(F), nl, fail.
Now I'm extremely confused about the c(F,X,[]) and q(F,X,[]) part because it doesn't seem to match any thing that I have seen, c only takes one parameter from what I can tell and these parameters don't seem to make sense for q. Please help me understand what is going on here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
c//1 和 q//1 是下面定义的定语从句语法的入口点(也称为顶级产生式),您可以
在其中找到这种“call”样式不鼓励使用 DCG 入口点,通常最好调用phrase(Grammar, TextToAnalyze, TextAfterAnalysis),在本例中
phrase((c(F) ; q(F)), "一些text", "")...
-->
运算符通常会被重写,添加 2 个参数,这是您所关心的原因。编辑
即
<代码>c(L) --> Lead_in,arrange(L),end.
被重写为
c(L,X,Y) :- Lead_in(X,X1),arrange(L,X1,X2),end(X2,Y) )。
c//1 and q//1 are entry points (aka top level production) of the Definite Clauses Grammar defined below, where you find
This style of 'call' on a DCG entry point is discouraged, usually is better to invoke the phrase(Grammar, TextToAnalyze, TextAfterAnalysis), in this case
phrase((c(F) ; q(F)), "some text", "")...
The
-->
operator is usually rewritten adding 2 arguments, that are cause of your concern.EDIT
I.e.
c(L) --> lead_in,arrange(L),end.
is rewritten to
c(L,X,Y) :- lead_in(X,X1),arrange(L,X1,X2),end(X2,Y).
c
是用-->
定义的,它实际上向其中添加了两个隐藏参数。第一个是要由语法规则解析的列表;第二个是解析后的“剩下的”。c(F,X,[])
在列表X
上调用c
来获取结果F
,期望[]
被保留,即解析器应该消耗整个列表X
。c
is defined with-->
, which actually adds two hidden arguments to it. The first of these is a list to be parsed by the grammar rule; the second is "what's left" after the parse.c(F,X,[])
callsc
on the listX
to obtain a resultF
, expecting[]
to be left, i.e. the parser should consume the entire listX
.