[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: DCGs
From: |
Christopher Howard |
Subject: |
Re: DCGs |
Date: |
Sat, 17 Nov 2012 02:22:13 -0900 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120907 Thunderbird/15.0 |
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
signature.asc
Description: OpenPGP digital signature
- DCGs, Christopher Howard, 2012/11/17
- Re: DCGs, Daniel Diaz, 2012/11/17
- Re: DCGs,
Christopher Howard <=