Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

antlr identifier name same as pre-defined function name cause MismatchedTokenException

Tags:

java

antlr

antlr3

I have defined a ANTLR grammer:

grammar test5;

stats_statement
:   
    STATS IDENT ASSIGN_SYM functions_stats

;

functions_stats 

:   COUNT LPAREN IDENT RPAREN   
;

STATS
:   'STATS'
;   

COUNT
:   'count'
;   

IDENT
:   (LETTER | '_') (LETTER | DIGIT | '_')*
;   

ASSIGN_SYM
: ':='
;

COMMA_SYM
: ','
;

SEMI_SYM
: ';'
;

LPAREN 
: '(' ;

RPAREN 
: ')' ;

fragment 
LETTER : ('a'..'z' | 'A'..'Z') ;

fragment 
DIGIT : '0'..'9';

which has one build-in function "count". But if I use the following test string:

STATS count:=count(col1)

the parser will return an error saying:

mismatched input 'count' expecting IDENT

any clue and/or hints on how to fix this problem?

Thanks Charles

like image 515
Aeris Avatar asked Jun 20 '26 01:06

Aeris


1 Answers

Create an ident rule that matched both IDENT and COUNT and use that rule in your parser rules (instead of using IDENT):

stats_statement
 : STATS ident ASSIGN_SYM functions_stats
 ;

functions_stats 
 :   COUNT LPAREN ident RPAREN   
 ;

ident
 : COUNT
 | IDENT
 ;
like image 153
Bart Kiers Avatar answered Jun 21 '26 15:06

Bart Kiers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!