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
16.68k stars 3.23k forks source link

ANTLR 4.2 and later fail to report error with mislocated 'assoc' #4629

Open blueglyph opened 1 month ago

blueglyph commented 1 month ago

(note: I'm using 4.13.1, though this isn't relevant)

Since 4.2, the location of <assoc=right> has changed: instead of being after the terminal, it must be at the beginning of the production.

Example before: expr: expr '^'<assoc=right> expr; Example since 4.2: expr: <assoc=right> expr '^' expr;

While I'm sure there was a good reason for this change, the compiler doesn't report an error if assoc is still after the terminal. It only gives a warning and ignores the attribute, resulting in a potentially undetected erroneous behaviour.

I had this problem, and it took a while for me to find out where it was coming from. It didn't help that The Definitive ANTLR4 Reference was mentioning the former syntax. Indeed, I would expect a change that isn't back-compatible to bump the major version number (too late for that now). Apparently, I'm not the only one to be confused by the problem:

=> I suggest therefore to make the compiler report an error when the assoc isn't at the correct place. By default of a major number bump, that's the only safe way to make sure the users WILL notice the change and update their grammar (or use an older version of ANTLR).