0

Given the following grammar:

BoolExpr ::= BoolConj { "or" BoolConj }.
BoolConj ::= BoolLit { "and" BoolLit }.
BoolLit ::= [ "not" ] BoolPosLit.
BoolPosLit ::= "true"|  "false"|  "(" BoolExpr ")".

I want to write a DCG parser for the grammar above. The parser simply only have to accepts (succeeds) or rejects (fails) on a given input,

Here is what I have:

 boolepxr([or(O)|BoolExpr]) -->
    ['or'],
    boolconj(O),
    boolexpr(BoolExpr).

boolconj([and(A)|BoolConj]) -->
    ['and'],
    boollit(A),
    boolconj(BoolConj).

boollit([not(N)|BoolLit]) -->
    ['not'],
    boolps(N).

boolps([true(B)|BoolPs]) -->
    ['true'],
    boolexpr(B),
    boolps(BoolPS).

 boolps([false(B)|BoolPs]) -->
    ['false'],
    boolexpr(B),
    boolps(BoolPS).

However, when I run this program, I didn't get the appropriate output.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136

1 Answers1

2

since the DCG must only recognize the input, I have simplified:

boolexpr --> boolconj, [or], boolexpr.
boolexpr --> boolconj.

boolconj --> boollit, [and], boolconj.
boolconj --> boollit.

boollit --> [not], boolps.
boollit --> boolps.

boolps --> [true].
boolps --> [false].
boolps --> ['('], boolexpr, [')'].

yields

?- phrase(boolexpr, [true,or,false,and,not,true]).
true ;
false.
CapelliC
  • 59,646
  • 5
  • 47
  • 90