How to write a square bracket in a prolog?

It may seem strange, but it is used in the parser, I want to be able to parse something from the form

Foo [bar]

So this will be listed as:

[foo, [, bar, [] Perhaps such a word will 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 the prolog?

+3
source share
2 answers

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

How about for an 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 = [].
+4

"[" ( ) ?

0

Source: https://habr.com/ru/post/1773550/


All Articles