[m-users.] DCG-s in Mercury

Tomas By tomas at basun.net
Sat Apr 24 19:56:01 AEST 2021


On Sat, 24 Apr 2021 11:33:31 +0200, Sean Charles (emacstheviking) wrote:
> [...] now I have two sets of DCG rules to port from SWI Prolog and I
> just don’t know where to start. [...]


There are a couple of problems: partical data structures and
nondeterminism. If the DCG is for a formal language (ie programming
code) then it is better to rewrite the parser, for example using
`continuation passing' (I think it's called).

To implement a DCG in Mercury you need to have solutions/2 at the top,
and then make all the `grammar rules' nondet. Constructing the output
holistically, as is/was common in DCGs, does not work (unless it has
been added in the twenty years or so since I tried it).

But to begin with you can just remove all the output parameters from
the DCG, and then look at that problem after the parsing works.

There are some minor advantages compared to Prolog, e.g. you can write
Kleene operators (star, plus, question mark) to make things
prettier. This has been discussed here before I believe.

/Tomas


More information about the users mailing list