antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.12k stars 3.28k forks source link

Request for empty productions to be allowed only explicitly #2875

Open SimonSntPeter opened 4 years ago

SimonSntPeter commented 4 years ago

Accidental empty productions have been a major nuisance for me to the point I wrote a short python script to detect and report them and made it part of my automated workflow. It saved me a lot of time.

I propose that ANTLR disallow them, or at least have a flag that disallows them.

As they might sometimes necessary explicitly (though I doubt it given the standard * or ? operators), or just desirable for clarity, I use the convention

comma_or_nothing:
        COMMA
    |    empty_production
    ;

where

empty_production : ;

SimonSntPeter commented 3 years ago

Just noticed I asked for this 4 years ago and it was closed back then for good reason (https://github.com/antlr/antlr4/issues/1086 "...it could collide with legacy grammars"), however I'd really like this in some form or other, as an optional flag or something, I wrote a small python script for detecting these and I'm very glad indeed that I did, but it would be - I think - a very good think to somehow roll this into the Antlr, it's caused me much wasted time, and for others too.

SimonSntPeter commented 3 years ago

Just been bitten by this again though in a new form:

call_stack returns [CallStack cs] : { $cs = new CallStack(); } ;

Spot the problem. I got a baffling ambiguity error. It's quit right, it is ambiguous but ANTLR's tolerance of empty productions is disconcerting.