Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3854998 • Letter: I

Question

I have a prolog 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.

Explanation / Answer

you can modify your code in the following way:

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

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