Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果使用java发生任何错误,ANTLR4将抛出异常并退出_Java_Antlr_Antlr4 - Fatal编程技术网

如果使用java发生任何错误,ANTLR4将抛出异常并退出

如果使用java发生任何错误,ANTLR4将抛出异常并退出,java,antlr,antlr4,Java,Antlr,Antlr4,我正在编写这样的自定义函数。Functionconcat(functiondd(1,2),1) 下面是我正在使用的语法。 现在我已经更改了它正在工作的语法,并检查了param的返回类型(如果函数存在): grammar FunctionGrammer; INT : [0-9]+ ; ID : [a-zA-Z_] [a-zA-Z_0-9]* ; expr : add; string : 'Functionadd(' INT ',' INT ')' ; add : 'Function

我正在编写这样的自定义函数。
Functionconcat(functiondd(1,2),1)

下面是我正在使用的语法。 现在我已经更改了它正在工作的语法,并检查了param的返回类型(如果函数存在):

grammar FunctionGrammer;

INT
 : [0-9]+
 ;

ID
 : [a-zA-Z_] [a-zA-Z_0-9]*
 ; 

expr : add;
string : 'Functionadd(' INT ',' INT ')' ;
add : 'Functionadd(' INT ',' INT ')' ;
concat : 'Functionconcat(' (string|INT )','INT ')';
以下是测试方法:

public static void main(String[] args) {
    try {
        CodePointCharStream input = CharStreams.fromString("Functionconcat(Functionadd(m,2),1)");
        FunctionGrammerLexer lexer = new FunctionGrammerLexer(input);
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        FunctionGrammerParser parser = new FunctionGrammerParser(tokenStream);
        parser.setErrorHandler(new BailErrorStrategy());
        ParseTree tree = parser.concat();
        FunctionCompilerImpl visitor = new FunctionCompilerImpl();
        visitor.visit(tree);
    } catch (ParseCancellationException e) {
        e.printStackTrace();
    }

    }
它正在抛出异常,但我如何才能给出有意义的异常,如参数/参数数据类型不匹配异常或函数名不匹配异常

访客提示:

public class FunctionCompilerImpl extends FunctionGrammerBaseVisitor<String>{



    @Override
    public String visitConcat(FunctionGrammerParser.ConcatContext ctx) {
        super.visitConcat(ctx);
        System.out.println("==================="+ctx);
        return null;
    }

}
公共类FunctionCompilerImpl扩展FunctionGrammarBaseVisitor{
@凌驾
公共字符串visitConcat(FunctionGrammerParser.ConcatContext ctx){
超级visitConcat(ctx);
System.out.println(“=========================”+ctx);
返回null;
}
}

但是vistor没有打印
System.out.println(“======================”+ctx)

因为
visitConcat(FunctionGrammerParser.ConcatContext ctx)
方法从未被调用<代码>访问(语法树)
被调用。你以前已经问过这个问题了。看看你之前的问题。谢谢!!正如您所说,我已经为抛出异常实现了BailErrorStrategy,但我如何才能提供更有意义的信息呢。由于BailErrorStrategy生成的异常不会有任何关于默认情况下发生的错误的消息。我认为您没有抓住要点
BailErrorStrategy
用于通知解析阶段发生的异常。在您的情况下,您要做的是,您必须在prasing完成后遍历树,并验证解析的内容是否满足要求。使用传递到
visitConcat
方法中的
ctx
获取要验证的数据(函数、参数等),将其与所需结果进行比较,并在出现错误时手动抛出异常(使用
throw
关键字)。