简体   繁体   English

ANTLR4-返回特定的规则对象

[英]ANTLR4 - Returning specific rule objects

I would like to return an ExprData. 我想返回一个ExprData。 ExprData is class inside my project. ExprData是我的项目中的类。 When i try to compile the grammar i get: 当我尝试编译语法时,我得到:

SASGrammarParser.java:684: error: cannot find symbol SASGrammarParser.java:684:错误:找不到符号

It is a import problem. 这是一个导入问题。 And how do i instantiate the ExprData? 以及如何实例化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. exprData是此处的返回变量,因此您可以通过使用$ exprData从操作中引用它来分配它。 Just form the top of my head (maybe that labels can't be used like this: 只是形成我的头顶(也许不能这样使用标签:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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