Question - formal language in the prologue

I'm trying to create the DCG, which recognizes all the lists that correspond to this form: a^n b^2m c^2m d^n.
I wrote the following rules:
s --> [].
s --> ad.
ad --> a, ad, d.
ad --> bc.
bc --> b, b, bc, c, c.
bc --> [].
a --> [a].
b --> [b].
c --> [c].
d --> [d].

When I try to evaluate a string with these specifications, such as a list [a,b,b,c,c,d], it works. But when I try to evaluate the query phrase(s, X)so that I can see all the possible lines returned by this grammar, it ends ad infinitum.

Is there something wrong with the way I created DCG?

+6
source share
4 answers

You can list the lines with iterative deepening:

?- length(Ls, _), phrase(s, Ls).
Ls = [] ;
Ls = [] ;
Ls = [a, d] ;
Ls = [a, a, d, d] ;
Ls = [b, b, c, c] ;
etc.
+6
source

. , .

, , .

0

, . , .

ad --> a, ad, d. ad --> bc., ad --> a, ad, a. ad --> bc.. ad --> bc. ad --> a, ad, a.. bc --> b, b, bc, c, c. bc --> [].

, , .

, [] , s β†’ ad β†’ bc β†’ [] s --> []., .

:

s --> ad.
a --> [a].
b --> [b].
c --> [c].
d --> [d].
ad --> bc.
bc --> [].
ad --> a, ad, d.
bc --> b, b, bc, c, c.

:

, ( , ). , , , , :).

-

 phrase(s, X), length(X, 4).

4 ,

X = [a, a, d, d]
X = [b, b, c, c]

6, :

X = [a, a, a, d, d, d]
X = [a, b, b, c, c, d]

:

 phrase(s, X), length(X, Y), Y >= 4 , Y < 10, Y != 6.
0

@: DGC , .

-, :

ad --> a, ad, d.

, .

The second problem is that this formal language cannot be constructed as a context-independent grammar, so you need an extra variable like count.

s(Count) --> a(Count),b(Count),c(Count),d(Count).
a(0) --> [].
a(succ(Count)) --> [a],a(Count).
b(0) --> [].
b(succ(Count)) --> [b,b],b(Count).
c(0) --> [].
c(succ(Count)) --> [c,c],c(Count).
d(0) --> [].
d(succ(Count)) --> [d],d(Count).

Then request it using the following target s(_, L, []).

I know this is an old question, but maybe someone will find the correct answer useful from now on.

0
source

All Articles