Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR4: Cannot find symbol op

Tags:

antlr

antlr4

I am doing the Calc/Labeled Expression tutorial from the ANTLRv4 book and when I compile I get this:

EvalVisitor.java:33: error: cannot find symbol
        if ( ctx.op.getType() == CalcParser.MUL || ctx.op.getType() == CalcParser.MIDDOT ) {
                ^
  symbol:   variable op
  location: variable ctx of type MulDivContext

In fairness, I have modified it a bit playing around. My grammar looks like this:

expr    : expr ( STAR | FSLASH | DIVIDE | MIDDOT ) expr     # MulDiv

And those are defined in a CommonLexer file like so:

lexer grammar CommonLexerRules;

    ID                      :   [a-zAZ]+ ;
    INT                     :   [0-9]+ ;
    STAR                    :   '*';

This works fine with grun and a test file is lexed correctly. However, I think I have changed how it's working by defining some alternatives for *. That is, I don't want * to always mean Multiplication, I also want to parse MIDDOT '·' as multiplication.

My problem is, there doesn't seem to be any '.op' in the generated code?

What this looks like is something like this:

  *
 / \
a   b

Where a,b,and * are expr. So what I want, is to get access to * as some kind of token that I can compare angainst CalcParser.MUL and CalcParser.MIDDOT etc.

Any help would be appreciated.

/Jason

like image 999
J. Martin Avatar asked Dec 25 '22 10:12

J. Martin


1 Answers

The context it missing the op attribute (which you probably removed). Put it back and you should be okay:

expr    : expr op=( STAR | FSLASH | DIVIDE | MIDDOT ) expr     # MulDiv
//             ^
//             |
//             +--- this one

Btw, I also see you've declared an ID as follows:

[a-zAZ]+

perhaps you meant to do this instead:

[a-zA-Z]+

?

like image 155
Bart Kiers Avatar answered Mar 11 '23 20:03

Bart Kiers