michaeldrm / prop-logic-parser

Parser implementation that evaluates propositional logic formulas using lexical analysis (tokenization) and parsing techniques. It supports the recognition of atomic propositions, logical connectives (negation, conjunction, disjunction, implication, equivalence), and parentheses for grouping.
0 stars 0 forks source link

lack of negation precedence causing parser to not parse negations correctly #1

Open Projectattic opened 1 month ago

Projectattic commented 1 month ago

Hi, I was looking through your code and I can see that for the unary negation expressions (~p) for example, you don't seem to have the parser set with the correct precedence. From looking through here: https://www.dabeaz.com/ply/ply.html, I could see in section 6.6 Dealing With Ambiguous Grammars that you need to do something like this:

precedence = (
    ('right', 'NEG'),            # Unary minus operator
)

def p_expr_not(p):
    'expr : NEGATION expr  %prec NEG'
    p[0] = ("NOT", p[2])

for negation to work as it should. I think you might also need to look into the precedences in general but I'm not sure. I think this change breaks how you get variables (or at least it did for me) so that might be something else you have to change.

give it a test with

formula = "~p | q"

Projectattic commented 1 month ago

~"I think this change breaks how you get variables (or at least it did for me)"~

I just messed up, getting variables works fine with the precedence changed