Why is it so complicated to create proper error messages with ANTLR4?

412 views
Skip to first unread message

Joshua Crotts

unread,
Jun 28, 2021, 6:45:59 PM6/28/21
to antlr-discussion
Hi all,

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!

Mike Lischke

unread,
Jun 29, 2021, 1:24:59 AM6/29/21
to ANTLR discussion group
Hi,
My answer on StackOverflow is probably the best you can get. The error messages I create with that approach are not 100% good, but good enough for most cases. If you have trouble understanding the idea just ask another question there with details that make problems for you.


rtm...@googlemail.com

unread,
Jul 2, 2021, 12:01:31 PM7/2/21
to antlr-discussion
I know nothing about errorListeners at the mo but the antlr book has a suggestion (P170), use a rule to match a faulty input. I tried it myself, it works. From my code

expr returns [Expr e] :
    [snipped]
    |
        '(' expr ')'
        {
            $e = new BracketedExpr($expr.e);
        }
    |
        '(' expr   // note: no closing paren token
        {
            ssErr("closing bracket missing");
        }


I indeed get "closing bracket missing" on malformed input.

I know something about logic and teaching it so may I politely give what may be this year's most useless Antlr answer ever - don't worry about syntax errors here. It's not difficult to compose WFFs and having their hands not held may help them learn to pick expressions apart and think about them. Save your ammo for when it gets hard, like with logical implication.

Feel free to ignore that though!

cheers

jan

rtm...@googlemail.com

unread,
Jul 2, 2021, 12:14:42 PM7/2/21
to antlr-discussion
That stackoverflow answer actually covers what I just suggested. Oh well.
Reply all
Reply to author
Forward
0 new messages