1

I'm trying to "translate" a file that contains dcg's. in particular i am trying to transform all dcg into normal definite clauses using expand_term/2, however i would like to avoid manually translating all dcgs, so i would try to translate all clauses of the file at once (e.g. passing the whole file) . is there any way to do it?

for example, suppose we have a temp.pl file which contains some dcg:

a(X,Y) --> b(X), c(Y).
d([A | Ax]) --> b(A).
....
....

instead of using expand_term/2 individually for each term, like:

?- expand_term((a(X,Y) --> b(X), c(Y)), Clause).
Clause = [(:-non_terminal(user:a/4)),  (a(X, Y, _A, _B):-b(X, _A, _C), c(Y, _C, _B))].

And then replace the dcg with definite clause in file.

I would like to pass for example the whole file (to a clause for example ) which contains the dcg, and translate all the dcg at once and print in a file or as an output, I don't know.

Chr
  • 11
  • 3
  • what I mean is. I have a file .pl where there are several DCG, so far I have manually translated them one by one using ```expand_term/2```, my question was if it is possible to translate all the DCG in the file with a single comand or single clauses (that is able to do so), without translating one by one. – Chr Dec 07 '21 at 09:43
  • I changed the question, I hope it is more understandable now. – Chr Dec 07 '21 at 10:22

1 Answers1

2

If I understand your question correctly then you are missing the obvious which is that what you seek is already in the SWI-Prolog code. The translation is done as the module is loaded. If you use listing/1 on a DCG predicate the code will list as normal Prolog and not DCGs.

See dcg.pl
load.pl
expand.pl
apply_macros.pl


Demonstration of listing/1 with DCG

Directory: C:/Users/Groot
File: example.pl (Based on this SO answer )

:- module(example,
    [
        monlangage/2
    ]).

:- set_prolog_flag(double_quotes, chars).

monlangage --> "ab" | "a", monlangage.

Example run

Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.3)
...

?- working_directory(_,'C:/Users/Groot').
true.

?- [example].
true.

?- listing(example:_).

monlangage(A, B) :-
    (   A=[a, b|B]
    ;   A=[a|C],
        monlangage(C, B)
    ).
true.

Notice that the source code is DCG with --> and the listing is not DCG with :-. Also notice that the rewritten clause has two extra arguments.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • thank you very much for the answer, but I didn't understand one thing: if you don't specify anything the clause translates strings and characters with the list notation (for example, `"a" = [97]`, or `"+-" = [43, 45]`). from the point of view of compilation, having one style rather than another changes something, or in both cases (so in any way I write a string or a character) the compiler will understand that we are referring to that character / string, in this case to the "a" character. – Chr Dec 07 '21 at 11:12
  • 1
    when I translate, through `listing/1`, a clause that contains a character in the form `"a"` or `0'a`, `listing/1` replaces the character with the list, that is, for example, `"a"` becomes `[97]`. I know that it is possible to force the compiler to keep the form with the double qoutes, but I wanted to understand if both forms are valid, that is, whether I write `"a"` rather than `[97]` the prolog compiler interprets them in the same way? – Chr Dec 07 '21 at 11:44