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:
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:
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$_]* ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.