Ask DCG Prolog for an answer

I have a DCG written in a prologue that tries to translate a string into a simple boolean proposition.

Current rules: + means OR, * means AND, - means NOT

g(or(X,Y)) -->
    f(X),
    "+",
    g(Y).
g(X) -->
    f(X).

f(and(X,Y)) -->
   e(X),
   "*",
   f(Y).
f(X) -->
   e(X).

e(not(X)) -->
   "-",
   d(X).
e(X) -->
   d(X).

d(X)-->
   "(",
   g(X),
   ")".    
d(a)-->
   "a".
d(b)-->
   "b".

      

However I am having problems with errors Out of local trace

.

This code should be correct, but how can I ask Prolog to give and(not(or(a,b)),or(b,not(a)))

as an answer to-(a+b) * (b + -a)

+3


source to share


1 answer


Your grammar seems fine, but you forgot to handle / skip spaces.

?- phrase(g(X), `-(a+b)*(b+-a)`).
X = and(not(or(a, b)), or(b, not(a))) ;
false.

      



(note: I manually removed all spaces and used SWI-Prolog extensions for the letter list.)

0


source







All Articles