Search code examples
antlr4

antrl4 why can not parse Compaire lexer but can parse compaire parser


grammar test;

parse:(expr|type)EOF ;

type: ID('<'ID'>')?;
expr:ID Compaire ID;

Compaire:'>' | '<';

ID:[a-z];

I am learning antlr4 , why can not parse Compaire lexer but can parse compaire parser

enter image description here

enter image description here


Solution

  • As already mentioned in the comments: when using tokens like '<' or '>' in parser rules, what really happens is this:

    type : ID ('<' ID '>')?;
    expr : ID Compaire ID;
    
    T__1     : '<';
    T__2     : '>';
    Compaire : '>' | '<';
    
    ID:[a-z];
    

    In other words, for all of the '...' tokens ANTLR encounters inside parser rules (that cannot be directly mapped to lexer rules!), ANTLR will create a lexer rule behind the scenes. And in your case, this causes that the token Compaire will never be created because T__1 and T__2 already match these chars.

    To fix it, you could do:

    // the parser now uses LT and GT, and will not create T__1 and T__2
    type : ID ('<' ID '>')?; 
    expr : ID compaire ID;
    
    compaire : GT | LT;
    
    GT : '>';
    LT : '<';
    

    The above will work, but I recommend to not use literal tokens inside your parser rules and always do:

    type : ID (LT ID GT)?; 
    expr : ID compaire ID;
    
    compaire : GT | LT;
    
    GT : '>';
    LT : '<';