0
votes

I'm trying to parse a simple integer declaration in antlr4.
The grammar I'm doing now is:

main  : 'int' var '=' NUMBER+ ;
var   : LETTER (LETTER | NUMBER)* ;
LETTER: [a-zA-Z_] ;
NUMBER: [0-9] ;
WS    : [ \t\r\n]+ -> skip ;

When I tried to test the main rule with int int_A = 0, I got an error:

extraneous input 'int' expecting LETTER.

I know it's because the variable name 'int_A' contains the keyword 'int', but how do I modify my grammar? Thanks.

1

1 Answers

0
votes

The lexer creates tokens with as much characters as possible. So int_A is being tokenised as the following 3 tokens:

  • 'int' (int keyword defined in parser)
  • LETTER (_)
  • LETTER (A)

So the parser cannot create a var with these tokens.

Instead of a parser rule var, make it a lexer rule:

main   : 'int' VAR '=' NUMBER+ ;
VAR    : [a-zA-Z_] ([a-zA-Z_] | [0-9])* ;
NUMBER : [0-9]+ ;
WS     : [ \t\r\n]+ -> skip ;