I'm trying to write a propositional logic program in Java that determines if a formula is well-formed or not. Obviously, ANTLR is a good choice for doing this since I can write all the rules in a CFG. All of that is nice, and I have my grammar working perfectly (it detects when a formula is or is not well-formed correctly). However, this program is aimed for beginners in the subject, and I want to be able to tell them where they went wrong with typing the formula in.
If I type in the formula (A -> B
, the parser recognizes that this is not well-formed because it is missing a closing parenthesis. However, the error it generates is lackluster to say the least: line 1:5 no viable alternative at input '(A -> B'
(this is slightly modified from vanilla ANTLR to include the line numbers iirc). I've seen people embed rules in the grammar with notifyErrorListener(...), but this means that all of my rules must be non-left recursive. For instance, I have the rule
propImpRule: OPEN_PAREN propWff IMPLIES propWff CLOSE_PAREN;
propImpRule: OPEN_PAREN propWff IMPLIES propWff {notifyErrorListener("Missing ')'");};
If I want to do the same with the opening parenthesis (or both for that matter), I can't because of how ANTLR's parser works (and I really don't want to have to go through the trouble of converting every rule to be non-left recursive. Plus, embedding the rules in the grammar seems cumbersome and a way around the actual problem).
I've tried to follow examples from past StackOverflow answers and by reading the ANTLR manual/documentation, but nothing really provides what I want or need (or enough documentation to do so). Can anyone point me in the right direction? Thanks!