Search code examples
javaantlr4grammar

Is anyone aware about below error in antlr4 parsing?


Error Message:

line 8:95 missing 'check' at 'check'
trigger:
    TRIGGER_ (trigger_type)
;

trigger_type:
    TRIGGER_TYPE_CHECK

;

I am not able to figure out the error in my parser.g4, I have provided correct input string but still it shows above below message.


Solution

  • There are most probably 2 (or more) lexer rules that match the input "check". Assuming TRIGGER_TYPE_CHECK matches "check", then something like this likely happens:

    ID : [a-zA-Z]+;
    TRIGGER_TYPE_CHECK : 'check';
    

    In the case above, "check" will always become an ID and never a TRIGGER_TYPE_CHECK. You need to place TRIGGER_TYPE_CHECK above ID:

    TRIGGER_TYPE_CHECK : 'check';
    ID : [a-zA-Z]+;
    

    Note that all other keywords need to be placed above ID. Of course, "check" will now never become an ID. If you want "check" to be both an ID and a TRIGGER_TYPE_CHECK, you need to do something like this:

    id : ID | TRIGGER_TYPE_CHECK;
    
    TRIGGER_TYPE_CHECK : 'check';
    ID : [a-zA-Z]+;
    

    and then use id instead of ID in your parser rules.