简体   繁体   中英

ANTLR4 - Returning specific rule objects

I would like to return an ExprData. ExprData is class inside my project. When i try to compile the grammar i get:

SASGrammarParser.java:684: error: cannot find symbol

It is a import problem. And how do i instantiate the ExprData?

expr returns [ExprData exprData]
    : expr AND expr                     #AndExpr
    | expr OR expr                          #OrExpr
    | expr IN '(' constant_list ')'     #InExpr
    | expr (EQ | ASSIGN) expr           #EqualExpr
    | expr op=(MULT | DIV) expr     #DivMultExpr
    | expr op=(PLUS | MINUS) expr       #PlusMinusExpr
    | expr LTEQ expr                 #LessEqualExpr     
    | expr LT expr                      #LessExpr
    | expr GT expr                      #GreaterExpr
    | expr GTEQ expr                        #GreaterEqualExpr
    | '-' expr                              #MinusExpr
    | '(' expr ')'                          #SimpleExpr                 
    | variable                              #VariableExp
    | constant                              #ConstantExp
    | function                              #FunctionExp
    ;

If you want to use some class in the grammar (and therefore in the generated parser) you need to import all of them in the grammar with

@parser::header {
import packageName.ExprData;
}

And I'm not sure on what do you mean on how to instantiate? exprData is the return variable here, so you can assign to it by referring it from the action with $exprData. Just form the top of my head (maybe that labels can't be used like this:

expr OR expr          #OrExpr {$exprData=someFuncitonThatReturnsExprDataObject();}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM