简体   繁体   中英

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?

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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM