prolog中如何写方括号?

发布于 2024-10-01 01:37:38 字数 258 浏览 0 评论 0原文

这可能听起来很奇怪,但它是在解析器中使用的,我希望能够解析以下形式的内容

foo[栏]

因此这将在列表中表示为:

[富,[,酒吧,[] 或许这样一句话用DCG会写成:

x --> id [[] arg []]

问题是方括号是保留字符,那么我如何在序言中表示它?

This may sound strange, but it is used in a parser, I want to be able to parse something of the form

foo[bar]

So this would be represented in a list as:

[foo, [, bar, []
Maybe such a word would be written in DCG as:

x --> id [[] arg []]

The problem is that the square bracket is a reserved character, so how can I represent this in prolog?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作死小能手 2024-10-08 01:37:38

您不能将方括号与其他所有内容一起视为原子(即 '['']')吗?

举个例子:

label1(T) --> id(X), label2(Y), {T =.. [X, Y]}.
label2(Y) --> ['['], innerexp(Y), [']'].
id(X) --> [X].
innerexp(Y) --> [Y].

执行:

?- phrase(label1(T), [foo, '[', bar, ']'], Rem).
T = foo(bar),
Rem = [].

Can you not treat your square brackets as atoms (i.e., '[' and ']'), along with everything else?

How about, for example:

label1(T) --> id(X), label2(Y), {T =.. [X, Y]}.
label2(Y) --> ['['], innerexp(Y), [']'].
id(X) --> [X].
innerexp(Y) --> [Y].

Execution:

?- phrase(label1(T), [foo, '[', bar, ']'], Rem).
T = foo(bar),
Rem = [].
記憶穿過時間隧道 2024-10-08 01:37:38

"[" (在引号中)可以解决问题吗?

Does "[" (in quotes) do the trick?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文