Using Prolog I'm trying to write a predicate that recognizes context free grammar and returns true
if the input list matches the CFG.
The alphabet of the input consists only of a,b
.
The CFG i'm trying to match is
S-> TT
T -> aTb | ab
I'm not quite sure how to implement this, mainly the T rule.
s(S0,S):-t(S0,S),t(S1,S).
t(S0,S):-S0 = a, t(S1,S), S1 = b; S0 = a, S1 = b.
match([H|T] :- s(H,T).
So if I query [a, a, b, b]
it should return true
.
However, I'm just getting an infinite loop.
I'm not quite sure how to implement the a^n b^n
rule.