Dear Feliks,
Hi and thank you very much for your reply (and also thank you everyone else who replied) and sorry for taking so long to come back to you. What you suggest is indeed what I was looking for. I hope you like ice cream because now I owe you some :)
As usual I feel a bit thick because this is such an obvious solution. I keep forgetting that DCG notation is just normal Prolog, so I treat it as if it's some sort of arcane magic. I actually have some "auxiliary" nonterminals in my code, but it didn't occur to me to pass a compound term as an argument, or write a recursive grammar rule. I shall now smack myself... :smack:
Using your code, I made it work like this:
integer( [ Var | Vars ] ) -->
[ integer, Var ],
rest_of_declaration( Vars ),
[ ';' ].
rest_of_declaration( [] ) --> [] .
rest_of_declaration( [ Var | Vars ] ) -->
[ ',', Var ],
rest_of_declaration( Vars ).
By the way, all this started because I was "porting" (sorry) some code from LPA Prolog to Swi, and in LPA this is correct syntax:
lhside, [implied] --> rhside.
- which will cause the "implied" token to be ommited, more or less like in Feliks' code. Obviously this is not supported in SWI so my code broke, but then I expected it to since I 've read the part in the Swi manual where it says there is no ISO standard for DCG's. So I was wondering- does any one know why not? It seems strange, considering Prolog was created for language processing in the first place. On the other hand, it's also strange that in my one-year module working with Prolog at uni, we didn't cover DCG's and I understand that's the way it is in most courses. Possibly that has something to do with standardisation?
Also- where would be a good place to start in Swi's sources should one want to add such functionality? I had a look in pl\boot\dcg.pl and it looks like dcg_translate_rule/2 is the predicate that handles, well, dcg rule translation?
Obviously the last two questions aren't addressed to Feliks alone!
Regards,
- Stassa
--- On Mon, 27/6/11, Feliks Kluzniak <***@utdallas.edu> wrote:
From: Feliks Kluzniak <***@utdallas.edu>
Subject: RE: [SWIPL] How to hide implied tokens in DCG rules?
To: "'Stassa Patsantzis'" <***@yahoo.com>, swi-***@lists.iai.uni-bonn.de
Date: Monday, 27 June, 2011, 12:11
Hi Stassa,
It would seem to me that for the particular example of type checking it
might be more convenient to separate the parser from the type checker. The
conventional approach is to make the parser produce a convenient abstract
syntax tree, and then to make subsequent passes (like type checking) operate
on (and perhaps modify) that tree.
So, for example,
integer( integer_declarations[ Var | Vars ] ) -->
[ integer, Var ],
rest_of_declaration( Vars ),
[ (;) ].
rest_of_declaration( [ Var | Vars ] ) -->
[ (,) ],
!,
rest_of_declaration( Vars ).
rest_of_declaration( [] ) --> [] .
The technique of using an auxiliary nonterminal symbol for optional or
iterated stuff is also a standard one.
Is this relevant to your questions?
-- Feliks
-----Original Message-----
From: swi-prolog-bounces+feliks.kluzniak=***@lists.iai.uni-bonn.de
[mailto:swi-prolog-bounces+feliks.kluzniak=***@lists.iai.uni-bonn.d
e] On Behalf Of Stassa Patsantzis
Sent: Sunday, June 26, 2011 11:35 AM
To: swi-***@lists.iai.uni-bonn.de
Subject: [SWIPL] How to hide implied tokens in DCG rules?
--- On Sun, 26/6/11, Stassa Patsantzis <***@yahoo.com> wrote:
From: Stassa Patsantzis <***@yahoo.com>
Subject: Re: [SWIPL] How to hide implied tokens in DCG rules?
To: "Carlo Capelli" <***@gmail.com>
Date: Sunday, 26 June, 2011, 12:34
Hi Carlo!
Thank you for replying. It would indeed be very simple to use a
disjunction like you suggest, unfortunately, ike I said, I can't :)
The reason I can't do it is because I need to do some type-checking. A
different example would be declaring multiple variables using a single type
word, say:
integer x, y;
- instead of:
integer x;
integer y;
The type-checking is done by a rule like:
integer(X) --> [integer, X].
So I need a rule of the form:
integer(X,Y) --> integer(X), [','], integer(Y).
Only of course this will parse as:
integer x, integer y;
So I need to skip the second instance of "integer" somehow, but still
let integer(X,Y) parse. That's why I say it needs to be "implied".
Sorry -this still isn't a full example, but a) I'm trying to figure out
the general case and b) a literal example would be a bit too involved, I
fear.
Thanks again and have a great Sunday afternoon!
- Stassa
--- On Sun, 26/6/11, Carlo Capelli <***@gmail.com> wrote:
From: Carlo Capelli <***@gmail.com>
Subject: Re: [SWIPL] How to hide implied tokens in DCG rules?
To: "Stassa Patsantzis"
<***@yahoo.com>
Cc: swi-***@lists.iai.uni-bonn.de
Date: Sunday, 26 June, 2011, 7:34
2011/6/25 Stassa Patsantzis <***@yahoo.com>
Hi all!
I'm trying to find a way to hide implied tokens while parsing (or
generating) a phrase with DCGs. Here's what I mean. I want to parse, eg,
these two phrases:
"I like ice cream and I like jam".
"I like ice cream and jam"
- using the same DCG rule. The second "I like" is implied, but it's not
wrong to use it.
Because of everything else that's going on in the ruleset, it's a bit
complicated to just do, say:
like(X,Y) --> ['I', 'like', X, and], (['I', 'like', Y]; [Y]).
It would be very convenient if I could hide tokens, like I say above. So, is
there a way to do this?
- Thanks,
Stassa
a very simple change to your DCG could do:
like(X,Y) -->
[A, B, X, and], ([A, B, Y]; [Y]), {(A='I';A=you), B=like}.
Anyway, DCGs usually require you to make the patterns explicit.
bye Carlo
-------------- next part --------------
HTML attachment scrubbed and removed
_______________________________________________
SWI-Prolog mailing list
SWI-***@lists.iai.uni-bonn.de
https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
-------------- next part --------------
HTML attachment scrubbed and removed
_______________________________________________
SWI-Prolog mailing list
SWI-***@lists.iai.uni-bonn.de
https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
-------------- next part --------------
HTML attachment scrubbed and removed