简体   繁体   English

关于BNF文法和Prolog的DCG文法的一些疑惑

[英]Some doubts about BNF grammars and Prolog's DCG grammars

I am studying grammars in Prolog and I have a litle doubt about conversions from the classic BNF grammars to the Prolog DCG grammars form.我正在学习 Prolog 中的语法,我对从经典 BNF 语法到 Prolog DCG 语法形式的转换有点怀疑。

For example I have the following BNF grammar:例如,我有以下 BNF 语法:

<s> ::= a b
<s> ::= a <s> b

that, by rewriting, generates all strings of type:通过重写,生成所有类型的字符串:

ab
aabb
aaabbb
aaaabbbb
.....
.....
a^n b^n

Looking on the Ivan Bratko book Programming for Artificial Intelligence he convert this BNF grammar into DCG grammar in this way:查看 Ivan Bratko 的《人工智能编程》一书,他以这种方式将此 BNF 语法转换为 DCG 语法:

s --> [a],[b].
s --> [a],s,[b].

At a first look this seems to me very similar to the classic BNF grammar form but I have only a doubt related to the , symbol used in the DCG乍一看,这在我看来与经典的 BNF 语法形式非常相似,但我只怀疑 DCG 中使用的,符号

This is not the symbol of the logical OR in Prolog but it is only a separator from the character in the generated sequence.这不是 Prolog 中逻辑OR的符号,而只是生成序列中字符的分隔符。

Is it right?这样对吗?

You can read the , in DCGs as and then or concatenated with :你可以阅读,在DCG中的,然后或级联

s --> 
   [a],
   [b].

and

t -->
  [a,b].

is the same:是一样的:

?- phrase(s,X).
X = [a, b].

?- phrase(t,X).
X = [a, b].

It is different to , in a non-DCG/regular Prolog rule which means logical conjunction (AND):它不同于,在非 DCG/常规 Prolog 规则中,这意味着逻辑与 (AND):

a.
b.

u :-
 a,
 b.

?- u.
true.

ie u is true if a and b are true (which is the case here).即如果ab为真,则u为真(这里就是这种情况)。

Another difference is also that the predicate s/0 does not exist:另一个区别还在于谓词s/0不存在:

?- s.
ERROR: Undefined procedure: s/0
ERROR:     However, there are definitions for:
ERROR:         s/2
false.

The reason for this is that the grammar rule s is translated to a Prolog predicate, but this needs additional arguments.这样做的原因是语法规则s被转换为 Prolog 谓词,但这需要额外的参数。 The intended way to evaluate a grammar rule is to use phrase/2 as above ( phrase(startrule,List) ).评估语法规则的预期方法是使用上面的phrase/2phrase(startrule,List) )。 If you like, I can add explanations about a translation from DCG to plain rules, but I don't know if this is too confusing if you are a beginner in Prolog.如果您愿意,我可以添加有关从 DCG 到普通规则的翻译的解释,但是如果您是 Prolog 的初学者,我不知道这是否太混乱。

Addendum: An even better example would have been to define t as:附录:一个更好的例子是将t定义为:

t --> 
  [b],
  [a].

Where the evaluation with phrase results in the list [b,a] (which is definitely different from [a,b] ):带有短语的评估结果在列表[b,a] (这绝对不同于[a,b] ):

?- phrase(t,X).
X = [b, a].

But if we reorder the goals in a rule, the cases in which the predicate is true never changes (*), so in our case, defining但是如果我们对规则中的目标重新排序,谓词为真的情况永远不会改变 (*),因此在我们的情况下,定义

v :-
  b,
  a.

is equivalent to u .等价于u

(*) Because prolog uses depth-first search to find a solution, it might be the case that it might need to try infinitely many candidates before it would find the solution after reordering. (*) 因为 prolog 使用深度优先搜索来找到解决方案,所以它可能需要尝试无限多个候选者才能在重新排序后找到解决方案。 (In more technical terms, the solutions don't change but your search might not terminate if you reorder goals). (从更专业的角度来说,解决方案不会改变,但如果您重新排序目标,您的搜索可能不会终止)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM