开发者

How to write a square bracket in prolog?

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 开发者_如何学Pythonin DCG as:

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

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


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 = [].


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜