# Translate from Prolog clause to DCG notation

**URL:** <https://swi-prolog.discourse.group/t/translate-from-prolog-clause-to-dcg-notation/2640>\
**Category:** Help!\
**Created:** [July 16, 2020, 12:05pm UTC](https://swi-prolog.discourse.group/t/translate-from-prolog-clause-to-dcg-notation/2640 "2020-07-16T12:05:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stassa.p](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/stassa.p/32/712_2.png) [@stassa.p](https://swi-prolog.discourse.group/u/stassa.p)\
**Post date:** [July 16, 2020, 12:05pm UTC](https://swi-prolog.discourse.group/t/translate-from-prolog-clause-to-dcg-notation/2640/1 "2020-07-16T12:05:04Z")

</div>

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`:

```prolog
?- 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.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [July 16, 2020, 1:01pm UTC](https://swi-prolog.discourse.group/t/translate-from-prolog-clause-to-dcg-notation/2640/2 "2020-07-16T13:01:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stassa.p](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/stassa.p/32/712_2.png) [@stassa.p](https://swi-prolog.discourse.group/u/stassa.p)\
**Post date:** [July 16, 2020, 1:34pm UTC](https://swi-prolog.discourse.group/t/translate-from-prolog-clause-to-dcg-notation/2640/3 "2020-07-16T13:34:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 16, 2020, 1:39pm UTC](https://swi-prolog.discourse.group/t/translate-from-prolog-clause-to-dcg-notation/2640/4 "2020-07-16T13:39:24Z")

</div>

Please share what you learn along the way.

There are a few categories that can be used.

1. [Nice to know](https://swi-prolog.discourse.group/c/nice-to-know/17)
2. [Wiki](https://swi-prolog.discourse.group/c/wiki/12)
3. [Useful Code](https://swi-prolog.discourse.group/c/useful-code/16)

---

<div class="post-metadata">

**Author:** ![stassa.p](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/stassa.p/32/712_2.png) [@stassa.p](https://swi-prolog.discourse.group/u/stassa.p)\
**Post date:** [July 16, 2020, 1:55pm UTC](https://swi-prolog.discourse.group/t/translate-from-prolog-clause-to-dcg-notation/2640/5 "2020-07-16T13:55:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 16, 2020, 2:19pm UTC](https://swi-prolog.discourse.group/t/translate-from-prolog-clause-to-dcg-notation/2640/6 "2020-07-16T14:19:29Z")

</div>

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](https://swi-prolog.discourse.group/c/wiki-discussion/18) talk about the wiki and drop notes, e.g. [Wiki Discussion: SWI-Prolog connecting to PostgreSQL via ODBC](https://swi-prolog.discourse.group/t/wiki-discussion-swi-prolog-connecting-to-postgresql-via-odbc/2405)
