Translate from Prolog clause to DCG notation

Is there a quick and easy way to translate the Prolog representation of a DCG production back to DCG notation? Ideally, I’d like to find a library that already does that, so that I don’t have to roll my own.

To clarify, suppose I translate the following production using dcg_translate_rule/2:

?- dcg_translate_rule(('S'-->'A','B'), P), numbervars(P). % for readability
P =  ('S'(A, B):-'A'(A, C), 'B'(C, B)).

Can I translate P back to ('S'-->'A','B') easily? Is there a library or pack that does that? A quick look did not turn up anything.

AFAIK, not. For a specific DCG translator this is probably possible. In general it might be hard as there are multiple valid translations for the same non-terminal rule.

I’d be happy with one that works with the Swi DCG library. I guess I’ll have to roll my own after all. It’s going to be a bit of work.

1 Like

Please share what you learn along the way.

There are a few categories that can be used.

  1. Nice to know
  2. Wiki
  3. Useful Code

Gladly, but the effort will probably be spread over a long time because it’s not something absolutel necessary- rather, it’s something that would be “nice to have” for a certain project of mine.

1 Like

If you want you can always drop some simple things into a few post and then latter they can be edited/deleted/moved/converted into another category/etc.

EDIT

Forgot to note that you can also start a Wiki that is empty and acts like a place holder then in the corresponding post in category Wiki Discussion talk about the wiki and drop notes, e.g. Wiki Discussion: SWI-Prolog connecting to PostgreSQL via ODBC