# Asserting DCG clause

**URL:** <https://swi-prolog.discourse.group/t/asserting-dcg-clause/4496>\
**Category:** Help!\
**Created:** [September 29, 2021, 5:14am UTC](https://swi-prolog.discourse.group/t/asserting-dcg-clause/4496 "2021-09-29T05:14:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [September 29, 2021, 5:14am UTC](https://swi-prolog.discourse.group/t/asserting-dcg-clause/4496/1 "2021-09-29T05:14:20Z")

</div>

I’m using: swish

What is the most elegant way to generate a DCG from a compound term representing a parse tree?  
For example, from this compound term:

```prolog
 s(np(det(le), noun(chat)), vp(v(mange), np(det(une), noun(souris))))

```

I would like to assert the following DCG clauses:

```prolog
s --> np,vp
np --> det, noun
vp --> v,np
v --> [mange]
det --> [le]
det --> [une]
noun --> [chat]
noun --> [souris]

```

Thx

---

<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:** [September 29, 2021, 7:28am UTC](https://swi-prolog.discourse.group/t/asserting-dcg-clause/4496/2 "2021-09-29T07:28:46Z")

</div>

In normal Prolog the way is to use expand\_term/2. It is a bit hard to guarantee the safety thereof. SWI-Prolog also has dcg\_translate\_rule/2 that is eventually used for DCG rules. That failed to pass the sandbox as well. Added to the set of safe predicates. Will be activated on the next Prolog update for SWISH.

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [September 30, 2021, 1:31am UTC](https://swi-prolog.discourse.group/t/asserting-dcg-clause/4496/3 "2021-09-30T01:31:15Z")

</div>

Thank you Jan. I tested it with swipl.

Looking forward to the next SWISH update

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [October 1, 2021, 1:16am UTC](https://swi-prolog.discourse.group/t/asserting-dcg-clause/4496/4 "2021-10-01T01:16:09Z")

</div>

I did it like this with expand\_term/2

```prolog
 expand_term((adv --> [quand]), Clause), maplist(assertz, Clause).
Clause = [(:-non_terminal(user:adv/2)), (adv([quand|_A], _A):-true)].

adv([quand],[]).
true.

```

I hardly understand the content of the Clause variable. Is asserting `(:-non_terminal(user:adv/2))` necessary ?

---

<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:** [October 1, 2021, 6:44am UTC](https://swi-prolog.discourse.group/t/asserting-dcg-clause/4496/5 "2021-10-01T06:44:29Z")

</div>

> [@albangabillon](#):
>
> I hardly understand the content of the Clause variable. Is asserting `(:-non_terminal(user:adv/2))` necessary ?

No. Term expansion produces both a the clause and a _directive_ that registers the predicate as a DCG non-terminal. That is just a flag that is accessible through predicate\_property/2. Not much is done with this flag. It can be used to improve reporting, code analysis, etc.

In general one must expect expand\_term/2 to either produce a single term or a list. The individual terms are either clauses or directives.

---

<div class="post-metadata">

**Author:** ![albangabillon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/albangabillon/32/3689_2.png) [@albangabillon](https://swi-prolog.discourse.group/u/albangabillon)\
**Post date:** [October 2, 2021, 7:26pm UTC](https://swi-prolog.discourse.group/t/asserting-dcg-clause/4496/6 "2021-10-02T19:26:39Z")

</div>

ok thank you Jan
