Suppose I want to write a custom operator for composing DCG rules in a way that would otherwise be repetitive.
For example, suppose I had I a DCG, ws
such that:
ws --> [].
ws --> " ", ws.
to match zero or more spaces. Obviously, if I want optional whitespace between each token in my grammar, it's obnoxious to have to put , ws
everywhere.
I could define a new operator to replace ,/2
.
:- op(1000, xfy, [ -- ]).
:- meta_predicate --(*,*,?,?).
--(L,R) --> ({callable(L)} -> call(L); L), ws, ({callable(R)} -> call(R); R).
This joines the left and right hand of --/2
via the optional whitespace rule. This mostly works fine, however certain things trip it up:
rule --> "foo" -- ("bar"; "quux").
If I try and execute this rule, I get an error saying that ;/4
is not defined. I have a vague idea of the problem here, but basically the question is: is there a way to define new operators for DCGs that work with the same generality as ,/2
?
2
in place of*
. That is::- meta_predicate --(2,2,?,?)
. – Rooks