This answer: Very basic dcg prolog syntax helped me a little but [X] only gets the next character, I want the whole enchilada, read on!
I am using GNU Prolog to write a command option parser and I am stuck on a DCG point. I have this grammar rule which looks for "foo --as=json" for example, and I just cannot work out how to get my hands on the result of "anything", the code:
as_opt --> "--as=", anything, { c( as_opt )}, !.
anything --> [], {c(anything_match)}.
And the gprolog expansion of that is:
as_opt([45, 45, 97, 115, 61|A], B) :-
anything(A, C),
c(as_opt), !,
C = B.
anything(A, B) :-
c(anything_match), !,
A = B.
The "c()" predicate is simple and is just used to track that the rule executed with a format() to stdout so I could see what's going on as it runs. If I had written the code by hand I would do:
%% for completeness here!
c(Msg) :- format("Processed ~w~n", [Msg]).
as_opt([45, 45, 97, 115, 61|A], B) :-
anything(A, C),
c(as_opt), !,
C = B,
{ g_assign( gvValue, B )}. %% just for example
Going back to the original DCG:
as_opt --> "--as=", anything, { c( as_opt ), gassign( gvValue, ??? )}, !.
So what goes where the "???" is. Is it possible...it must be. I am going to re-read the gprolog rules on how it expands DCG rules again in case I am about to (facepalm) myself but in the meantime any assistance would be most welcome.
Thanks, Sean.
You are asking for one of the simplest DCG non-terminals, which describes any list:
To actually access the list that is being described, you introduce an argument:
You can use it like this: