java - Antlr4 中未找到 getType() 方法

标签 java parsing antlr antlr4

下面的规则是从较大规则中摘录的。请注意语法末尾的可选部分。解析输入后,我们遍历生成的解析树来评估表达式。下面还给出了第一条规则的监听器代码。

arithmeticTerm
   : arithmeticFactor (op=(MULT|DIVIDE) arithmeticFactor)*
   ;

arithmeticFactor: INTEGER        /* For now , let it be simple terminal */

监听器

import java.util.ArrayList;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeProperty;

public class PredicateEvaluator extends PredicateGrammarBaseListener {

    private ParseTreeProperty<Float> floatValues = new ParseTreeProperty<Float>();
    private ArrayList<Float> arithmeticTermFactorList = null;

    private void setFloatValue(ParseTree node, float value){ 
       floatValues.put(node, value); 
    }

    private float getFloatValue(ParseTree node){
       return floatValues.get(node); 
    }

    public void enterArithmeticTerm(
      @NotNull PredicateGrammarParser.ArithmeticTermContext ctx) 
    { 
       arithmeticTermFactorList = new ArrayList<Float>();
    }

目前,我正在用这种方式评估算术项。我需要检测类型操作并对因子进行除法或乘法。但是,我的编译器找不到 getType() 方法。我查看了antlr4生成的代码,但它不在那里。我正在关注antlr4的创建者的书,他在类似的场景中使用了getType()方法,但同样的事情在这里不起作用。我们将非常感谢您的帮助。

    public void exitArithmeticTerm(
      @NotNull PredicateGrammarParser.ArithmeticTermContext ctx) 
    { 
        float evaluatedValue = 0.0f;        
        if (ctx.op == null){
            evaluatedValue = getFloatValue(ctx.arithmeticFactor().get(0));
        }else{
            for(float value : arithmeticTermFactorList){
                if(ctx.op.getType() == PredicateGrammarLexer.MULT) {
                    evaluatedValue *= value;  
                }else{
                    evaluatedValue /= value;    
                }  
            }
        }
        arithmeticExprTermList.add(evaluatedValue);
    }

    public void exitArithmeticFactor(
      @NotNull PredicateGrammarParser.ArithmeticFactorContext ctx) 
    {  
      Float evaluatedValue = NEGATIVE * Integer.valueOf(ctx.INTEGER().getText());  
      arithmeticTermFactorList.add(evaluatedValue);
    }

}

最佳答案

而不是这样做:

arithmeticTerm
   : arithmeticFactor (op=(MULT|DIVIDE) arithmeticFactor)*
   ;

做这样的事情:

expression
 : MINUS expression              #unaryMinusExpression
 | expression MULT expression    #multExpression
 | expression DIVIDE expression  #divideExpression
 | expression ADD expression     #addExpression
 | expression MINUS expression   #minusExpression
 | INTEGER                       #integerExpression
 | '(' expression ')'            #parensExpression
 ;

然后在您的监听器中,您只需重写 #...Expression 方法:

@Override
public void enterMultExpression(@NotNull PredicateGrammarParser.MultExpressionContext ctx) 
{ 
    ...
}

请注意,MULTDIVIDE 表达式的优先级高于 ADDMINUS,因为它们是在之前定义的后者。

关于java - Antlr4 中未找到 getType() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25077236/

相关文章:

java - 请求 header 中存在多个 JSESSIONID

iOS后台队列检索数据两次?

java - 创建一个 ANTLR 语法规则,如果在函数声明上方找到 doctype 注释,则返回函数名称作为标记

java - 如何在 ANTLR 3 中处理字符串文字中的转义序列?

java - 错误转换解析时间 dd.MM.yyyy', '12 :00

Antlr 使用复合语法在 Antlrworks 中未定义导入

java - 分支预测不起作用?

java - 我的 RestController 不返回我的对象​​的所有属性

java - TomEE + OpenJPA : Error creating EntityManager using container managed DataSource

javascript - 显示价格时值(value)中的逗号不起作用