Re: DCGs

Christopher Howard <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <[email protected]>
On 11/17/2012 01:52 AM, Daniel Diaz wrote:
> H,
> 
> Le 17 nov. 2012 à 08:05, Christopher Howard a écrit :
> 
> 
> "generates" does not seem to me the correct verb here. It would be "recognizes" (DCG are mainly done for parsing, even if they can be used to generate strings according to the grammar this is often very limited - as for any Prolog predicate).
> 
> 
> The problem with this grammar is that the number of 'a' and the number of 'd' are not related. Recall context-free grammars are grammars which works "by nesting" (using a stack) (see a more general course on context-free grammars for more information). 
> 
> Here is a grammar that works for your language:
> 
> s --> r.
> s --> [a], s, [d].
> 
> r --> [].
> r --> [b,b], r, [c,c].
> 
> you can check it with:
> 
> | ?- s([a,a,a,b,b,b,b,c,c,c,c,d,d,d], []).
> 
> true
> 
> NB: this grammar will not work well in generation since it will only generates word of the form b^2m c^2m (which are corrects, but no words of other forms can be generated).
> 
> 
> You are right. To include classical Prolog predicate call inside a DCG you have to put it inside curly brackets in the body of the DCG.
> E.g., you could write  s --> as,m,ds,{length(as,X),length(ds,Y),X =:= Y}.
> However, the arguments of length are wrong here (as and ds are Prolog atoms, not lists). You can then extend your DCG non-terminals (e.g., as, ds) with arguments (as(La), ds(Ls)) which can be any Prolog term. You will find online a lot of information on DCG interacting with Prolog code. But this is not needed in this case.
> 
> Daniel
> 
> 
> 

Je vous remercie. That was a very helpful response.

-- 
frigidcode.com

_______________________________________________
Users-prolog mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/users-prolog
signature.asc (application/pgp-signature, 551 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iQEcBAEBAgAGBQJQp3NlAAoJEI2DxlFxTtgdaRMH/A/VO8FlyusdyNIXfubhRsyL
UvsImrvR5s0RtnsnR9Nh8MkDlyZq30MRHYBh2sndTzEFcIK0COAXBxhoguap/g0G
88dXj+Uvo/urCSGKjP7N4WFgXbo+OS+4ldgr9qWerehoCTHzlpWDkxNbkI/Jw/H0
HdpI6BTcZ/YOLg35uIaBfglS712VC90UAPT9jfCKu7FBmP/2kQGhCzrawGoLaSfZ
/nRCMy1GqV93AAhzsl1rlvP2ROTYxitUIkXPWQqcljpFffDMCnwxq24s4vJ3rzjt
7wC6Bp9u8DgpPVTQj0Ox9GLx5v7zQVRzfVaogeuOoFbsOlUp2fmN0Nm7i1Crwos=
=quVY
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.