antlr4 issues with simple grammar

338 views
Skip to first unread message

ssarkaray...@gmail.com

unread,
Sep 7, 2016, 5:46:25 PM9/7/16
to antlr-discussion
Hello,

I am trying basic things to learn about antlr4 with the following grammar (Math.g4):

===============================

grammar Math;

compileUnit
    :   expr EOF
    ;

expr
    :   '(' expr ')'                         # parensExpr
    |   op=('+'|'-') expr                    # unaryExpr
    |   left=expr op=('*'|'/') right=expr    # infixExpr
    |   left=expr op=('+'|'-') right=expr    # infixExpr
    |   func=ID '(' expr ')'                 # funcExpr
    |   value=NUM                            # numberExpr
    ;

OP_ADD: '+';
OP_SUB: '-';
OP_MUL: '*';
OP_DIV: '/';

NUM :   [0-9]+ ('.' [0-9]+)? ([eE] [+-]? [0-9]+)?;
ID  :   [a-zA-Z]+;
WS  :   [ \t\r\n] -> channel(HIDDEN);

=====================

I did following things successfully:

antlr4 Math.g4
javac Math*.java

Then I tried:
grun Math parse ./math.1 -gui  

where math.1 contains simple expression (2 + 3)*6;  I got error:
No method for rule parse or it has arguments

I tried:
grun Math prog -qui math.1

I got:
No method for rule prog or it has arguments

Can I get some help how to resolve this ?

Thanks,
SS






Mike Lischke

unread,
Sep 8, 2016, 3:10:09 AM9/8/16
to antlr-di...@googlegroups.com

I tried:
grun Math prog -qui math.1

I got:
No method for rule prog or it has arguments

Your main rule is compileUnit, not prog.
Reply all
Reply to author
Forward
0 new messages