Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Antlr4 - Implicit Definitions

Tags:

antlr

antlr4

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?

like image 205
NotMe NotYou Avatar asked Apr 29 '15 14:04

NotMe NotYou


1 Answers

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 ;)

like image 108
ggg Avatar answered Jan 02 '23 17:01

ggg