Asserting DCG clause

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:

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

I would like to assert the following DCG clauses:

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


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.

Thank you Jan. I tested it with swipl.

Looking forward to the next SWISH update

I did it like this with expand_term/2

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


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.

ok thank you Jan