Mr Macky
Mr Macky

Reputation: 11

SWI-Prolog parsing DCG predicate doesn't work

I have a predicate with the goal of parsing a grammar. The productions of the grammar are:

S-> B B->bB B->b

The predicate is intended to take in a list and determine whether or not the list belongs to the grammar. My code is as follows:

s(List,[]):- b(List,[]).
b(List,[]):-'C'(List,b,X), b(X,[]).
b([b|List],List).

The predicate seems to be able to determine that [b] is a solution, however, it is unable to detect that [b,b,b] is also a solution.

For instance,

?-s([b],[]).
 true.
?-s([b,b,b],[]).
 false.

I think the second line of my code isn't doing what I want it to, but I'm not sure how to fix it.

Upvotes: 1

Views: 120

Answers (1)

CapelliC
CapelliC

Reputation: 60034

If you are willing to understand the basics of Prolog and DCGs, see this solution:

s(List):- b(List,[]).
b([b|List],Rest):- b(List,Rest).
b([b],[]).

Upvotes: 2

Related Questions