Search code examples
parsingantlrantlr4context-free-grammar

antlr4 grammar - Mismatched input error


I have a very simple grammar here:

grammar mygrammar;
prog  : E 'arrow' ('and' E PATTERN) ;
PATTERN : ( BPATTERN | ('and' PATTERN)+ | ('or' PATTERN)+ ) ;
BPATTERN : ( L | P ('and' E PATTERN) ) ;
L : ( E | 'not' E | U | 'not' U ) ;
P : 'p' ;
U : 'u' ;
E : 'e' ;
WS : [ \t\r\n]+ -> skip ;

All the input strings I parse, the error is always the same:

line 1:0 mismatched input 'e' expecting 'e'

If I change the 'e' token with 'x', I got the same but with 'x' inside, so the problem seems to be related to the non-terminal E. Does anyone know where is the error?


Solution

  • That is because the input "e" is being tokenized as an L token. You'll need to make L a parser rule instead of a lexer rule.

    Parser rules start with a lower case letter, so you could try something like this:

    grammar mygrammar;
    
    // parser rules
    prog     : E 'arrow' 'and' E pattern ;
    
    pattern  : bpattern 
             | ('and' pattern)+ 
             | ('or' pattern)+
             ;
    
    bpattern : l 
             | P 'and' E pattern
             ;
    
    l        : 'not'? ( E | U ) ;
    
    // lexer rules (tokens)
    P : 'p' ;
    U : 'u' ;
    E : 'e' ;
    
    // skipped tokens
    WS : [ \t\r\n]+ -> skip ;