Search code examples
javaantlr

ANTLR line 1:12 token recognition error at: '\n'


I have a grammar like that:

grammar ST;

program: statement+;

statement: assignment ';'
         | ifStatement
         | whileStatement
         | forStatement
         | functionCall ';';

assignment: IDENTIFIER ':=' expression;

ifStatement: 'IF' condition 'THEN' statement+ ('ELSE' statement+)? 'END_IF';

whileStatement: 'WHILE' condition 'DO' statement+ 'END_WHILE';

forStatement: 'FOR' IDENTIFIER ':=' expression 'TO' expression 'DO' statement+ 'END_FOR';

functionCall: IDENTIFIER '(' argumentList? ')';

argumentList: expression (',' expression)*;

expression: literal
          | IDENTIFIER
          | '(' expression ')'
          | expression operator expression;

condition: expression;

literal: INT | BOOL;

operator: '+' | '-' | '*' | '/' | '=' | '<' | '>';

IDENTIFIER: [a-zA-Z_][a-zA-Z0-9_]*;
INT: [0-9]+;
BOOL: 'TRUE' | 'FALSE';
WS: [ \\t\\r\\n]+ -> skip;

and getting exceptions from the question title for the values like that:

String code = """
            x := 10;
            y := 20;
            IF x < y THEN
                z := x + y;
            END_IF;
        """;

Solution

  • The problem was in missing this line in the grammar:

    WS: [ \t\r\n]+ -> skip;