1

I've just started using antlr so Id really appreciate the help! Im just trying to make a variable declaration declaration rule but its not working! Ive put the files Im working with below, please lmk if you need anything else!

INPUT CODE:

var test;

GRAMMAR G4 FILE:


grammar treetwo;

program : (declaration | statement)+ EOF;

declaration :
            variable_declaration
            | variable_assignment
            ;

statement:
        expression
        | ifstmnt
        ;

variable_declaration:
                    VAR NAME SEMICOLON
                    ;

variable_assignment:
                    NAME '=' NUM SEMICOLON
                    | NAME '=' STRING SEMICOLON
                    | NAME '=' BOOLEAN SEMICOLON
                    ;

expression:
            operand operation operand SEMICOLON
            | expression operation expression SEMICOLON
            | operand operation expression SEMICOLON
            | expression operation operand SEMICOLON
            ;

ifstmnt:
        IF LPAREN term RPAREN LCURLY
        (declaration | statement)+
        RCURLY
        ;


term:
    | NUM EQUALITY NUM
    | NAME EQUALITY NUM
    | NUM EQUALITY NAME
    | NAME EQUALITY NAME
    ;

/*Tokens*/
NUM : '0' | '-'?[1-9][0-9]*;
STRING: [a-zA-Z]+;
BOOLEAN: 'true' | 'false';
VAR : 'var';
NAME : [a-zA-Z]+;


SEMICOLON : ';';
LPAREN: '(';
RPAREN: ')';
LCURLY: '{';
RCURLY: '}';
EQUALITY: '==' | '<' | '>' | '<=' | '>=' | '!=' ;

operation: '+' | '-' | '*' | '/';
operand: NUM;
IF: 'if';
WS  :  [ \t\r\n]+ -> skip;

Error I'm getting:

(line 1,char 0): mismatched input 'var' expecting {NUM, 'var', NAME, 'if'}

1 Answers1

0

Your STRING rule is the same as your NAME rule.

With the ANTLR lexer, if two lexer rules match the same input, the first one declared will be used. As a result, you’ll never see a NAME token.

Most tutorials will show you have to dump out the token stream. It’s usually a good idea to view the token stream and verify your Lexer rules before getting too far into your parser rules.

Mike Cargal
  • 6,610
  • 3
  • 21
  • 27
  • To dump out the token stream, you can use the `-tokens` option of the test rig tool `grun`. For the OP's example, this produces: `[@0,0:2='var',,1:0] [@1,4:7='test',,1:4] [@2,8:8=';',<';'>,1:8]` which shows that 'var' is lexed as a STRING, but the OP wanted VAR. – Chris Apr 15 '22 at 15:05