Antlr4 - Implicit Definitions
Asked Answered
E

1

8

I am trying to create a simple for now only integer-arithmetic expression parser. For now i have:

grammar MyExpr;

input: (expr NEWLINE)+;


expr: '(' expr ')'
    | '-' expr
    | <assoc = right> expr '^' expr
    | expr ('*' | '/') expr
    | expr ('+' | '-') expr
    | ID '(' ExpressionList? ')'
    | INT;

ExpressionList : expr (',' expr)*; 


ID : [a-zA-Z]+;
INT : DIGIT+;
DIGIT: [0-9];
NEWLINE : '\r'?'\n';
WS : [\t]+ -> skip;

The rule ExpressionList seems to cause some problems. If i remove everything containing ExpressionList, everything compiles and seems to work out fine. But like above, i get errors like:

error(160): MyExpr.g4:14:17: reference to parser rule expr in lexer rule ExpressionList
error(126): MyExpr.g4:7:6: cannot create implicit token for string literal in non-combined grammar: '-'

I am using Eclipse and the Antlr4 Plugin. I try to orient myself on the cymbol grammar given in the antlr4-book.

Can someone tell me whats going wrong in my little grammar?

Enrique answered 29/4, 2015 at 14:23 Comment(0)
R
13

Found it out by myself:

Rules starting with capital letter refer to Lexer-rules. SO all I had to do was renaming my ExpressionList to expressionList.

Maybe someone else will find this useful some day ;)

Responsory answered 29/4, 2015 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.