Antlr v3 error with parser/lexer rules

前端 未结 1 648
逝去的感伤
逝去的感伤 2020-12-12 00:49

I am having problems with my Antlr grammar. I\'m trying to write a parser rule for \'typedident\' which can accept the following inputs:

\'int a\' or \'char a\'

相关标签:
1条回答
  • 2020-12-12 01:01

    'int a' isn't accepted while 'int ab' is accepted. ... My guess why this works is because ALPH and DIGIT were overriding ...

    Yes, it appears ALPH was defined before the IDENT rule, in which case single letters were tokenized as ALPH tokens. If IDENT was defined before ALPH, it should all go okay (in your case).

    To summarize how ANTLR's lexer rules work:

    • lexer rules match as much characters as possible (greedy);
    • if 2 (or more) lexer rules match the same input, the rule defined first will "win".

    You must realize that the lexer does not produce tokens based on what the parser (at that time) needs. The lexer operates independently from the parser.

    0 讨论(0)
提交回复
热议问题