ANTLR lexers fully assign unique types of tokens before the parser is ever used. When several types of tokens can coincide with a token, the first of them appears in the grammar - the one that is used. For your grammar, the token cannot be of type ID and type LRULEID at the same time. Since the input foo matches both of these lexer rules, the first one appears in the grammar, so your tokens are: ID , COLON , ID , SEMICOLON , <EOF> .
Since the ID token is not actually mentioned in the parser, I suggest one of the following changes. Any of these options will solve the problem that you described, so the choice depends entirely on how the final grammar looks.
Foreword
You need to change the space references from SPACE+ to SPACE* , or this rule will require at least one space between bar and ; .
Option 1
Delete rule rule lexer at all.
Option 2
Change the ID to the analyzer rule so that it does not try to assign an ID token type to all your identifiers.
id : LRULEID | PRULEID;
Update rule pRule1 with an ID .
pRule1 : ((id | LBRACE lRule1 PIPE lRule1 RBRACE) modificator? SPACE+)+ ;
Unrelated side note
You can read the grammar more easily if you remove the outer + closure inside the lRule and pRule1 , and instead add them to the rule links themselves. Please note that I have modified the SPACE links as described in the preface.
lRule : LRULEID COLON lRule1+ ; lRule1 : ((LRULEID | STRING | SET) | LBRACE lRule1 PIPE lRule1 RBRACE) modificator? SPACE* ; pRule : PRULEID COLON pRule1+ ; pRule1 : ((LRULEID | PRULEID) | LBRACE lRule1 PIPE lRule1 RBRACE) modificator? SPACE* ;
source share