ANTLR4 - What is the correct way to define an array type?
fragment LETTER: [[a-zA-Z\u0080-\u00FF_];
You're allowing [
as a letter (and thus as a character in identifiers), so in string[5]
, string[5
is interpreted as an identifier, which makes the parser think the subsequent ]
has no matching [
. Similarly in string [5]
, [5
is interpreted as an identifier, which makes the parser see two consecutive identifiers, which is also not allowed.
To fix this you should remove the [
from LETTER
.
As a general tip, when getting parse errors that you don't understand, you should try to look at which tokens are being generated and whether they match what you expect.
It's common for languages that want to be flexible with whitespace to have a rule, something like this:
WS: [ \t\r\n]+ -> skip; // or channel(HIDDEN)
It should address your problem.
This shuttles Whitespace off to the side so you don't have to be concerned with it in your parser rules.
Without that sort of approach, you'd still need to define a whitespace rule (same pattern as above), but, if you don't skip
it (or send it to eat HIDDEN
channel), you'll have to include it everywhere you want to allow for whitespace by inserting a WS?
. Clearly this has the potential to become quite tedious (and adds a lot of "noise" to both your grammar and the resulting parse trees).