2
votes

I am new to antlr4, even though touched antlr3 years ago. I have this JavaScript grammar file here, and it thinks break statement is expression statement, while break statement is defined earlier. How does the rule precedence work?

Here is my grammar file:

https://github.com/frankdu/minijs/blob/master/antlr/src/main/resources/org/minijs/parser/antlr/JavaScript.g4

The problem is with statement parsing. When it sees the break statement

break;

It parses it as Expression statement. Therefore, the below unit test fails and is marked ignored for now:

https://github.com/frankdu/minijs/blob/master/core/src/test/java/org/minijs/core/parser/BreakStatementTest.java

1

1 Answers

4
votes

How does the rule precedence work?

It works as you thought: alternatives inside a rule are matched in the order they're defined.

The real problem is because you defined the IDENTIFIER rule before your keyword rules. Because of that, the input 'break' is being tokenized as an IDENTIFIER and is therefor matched as a primaryExpression.

The solution is simple: place the IDENTIFIER rule after your keyword rules:

// Keywords
VAR:        'var';
IF:         'if';
WHILE:      'while';
DO:         'do';
FOR:        'for';
CONTINUE:   'continue';
BREAK:      'break';
RETURN:     'return';
FUNCTION:   'function';
NEW:        'new';
DELETE:     'delete';
IN:         'in';
INSTANCEOF: 'instanceof';

IDENTIFIER
    :   [a-zA-Z$_] [a-zA-Z0-9$_]*
    ;