I'm working with ANTLR4 in Java to create a custom language parser. I've generated my lexer and parser using a grammar file (CCAL.g4), and I'm implementing a visitor class (EvalVisitor) extending CCALBaseVisitor. However, I'm encountering a cannot find symbol error related to the visit method in my EvalVisitor class. The error is as follows:
cannot find symbol
symbol: method visit(CCALParser.ExpressionContext)
location: class EvalVisitor
Here's a simplified version of my grammar file (CCAL.g4):
main : MAIN LBRACE decl_list statement_block RBRACE ;
statement_block : (statement)* ;
statement : ID ASSIGN expression SEMI #AssignStm
| ID LPAREN arg_list RPAREN SEMI #FunctionCallStm
| LBRACE statement_block RBRACE #BlockStm
| IF condition LBRACE statement_block RBRACE
ELSE LBRACE statement_block RBRACE #IfElseStm
| WHILE condition LBRACE statement_block RBRACE #WhileStm
| SKIP_STATEMENT SEMI #SkipStm
;
expression : expression_fragment binary_arith_op expression_fragment #AddMinusOp
| LPAREN expression RPAREN #Parens
| ID LPAREN arg_list RPAREN #FunctionCallOp
| expression_fragment #ExpressFrag
;
binary_arith_op : PLUS | MINUS ;
expression_fragment
: ID #ID
| MINUS ID #NegatedVariable
| NUMBER #NumberLiteral
| TRUE #TrueLiteral
| FALSE #FalseLiteral
| LPAREN expression RPAREN #NestedExpression
;
And here's the relevant part of my EvalVisitor class:
public class EvalVisitor extends CCALBaseVisitor<Object> {
// ... [Other methods]
@Override
public Object visitAssignStm(CCALParser.AssignStmContext ctx) {
String id = ctx.ID().getText();
Object value = visit(ctx.expression());
// ... [Rest of the method]
}
// ... [
}
I'm unsure why I'm receiving this error since I'm following the usual pattern of overriding visitor methods. Any insight into what might be causing this issue and how to resolve it would be greatly appreciated.
I have also tried adding antlr4 to my Classpath as I assume the error lies in antlr not working properly, here is my ~/.zshrc:
export CLASSPATH="/Users/ciansullivan/Documents/DCU-Coursework/CA4003/Antlr/lib/antlr-4.13.1-complete.jar;.;"
export PATH="$PATH:$HOME/scripts"
I can verify antlr is in the correct directory.
I have also tried using the following class path with no luck:
export CLASSPATH="/Users/ciansullivan/Documents/DCU-Coursework/CA4003/Antlr/lib/antlr-4.13.1-complete.jar"
I have tried what is shown above.
Without all your code, then I'm goin to pick up on what Bart Kiers said -- class path.
The second class path you listed won't work because that will give you Antler, but that won't provide your local classes. The first one is wrong because that's windows style.
Try this, assuming you've compiled locally. If you have a jar file produced, then replace that . with a path to the jar.
It might fix your runtime problem. Also, most people would move the Antlr lib into /usr/local/lib, but that's a side point.