ast生成时antlr 4.7错误
antlr 4.7 error while ast generation
我正在尝试 运行 按照 - http://meri-stuff.blogspot.com/2011/08/antlr-tutorial-hello-word.html
的步骤说明进行 strp
根据上述网页的原始代码 -
public CommonTree compile(String expression) {
try {
//lexer splits input into tokens
ANTLRStringStream input = new ANTLRStringStream(expression);
TokenStream tokens = new CommonTokenStream( new S001HelloWordLexer( input ) );
//parser generates abstract syntax tree
S001HelloWordParser parser = new S001HelloWordParser(tokens);
S001HelloWordParser.expression_return ret = parser.expression();
//acquire parse result
CommonTree ast = (CommonTree) ret.tree;
printTree(ast);
return ast;
} catch (RecognitionException e) {
throw new IllegalStateException("Recognition exception is never thrown, only declared.");
}
}
我修改了下面部分代码:
public CommonTree compile(String expression) {
try {
//lexer splits input into tokens
ANTLRStringStream input = new ANTLRStringStream(expression);
TokenStream tokens = new CommonTokenStream( (TokenSource) new S001HelloWordLexer( (CharStream) input ) );
//parser generates abstract syntax tree
S001HelloWordParser parser = new S001HelloWordParser((org.antlr.v4.runtime.TokenStream) tokens);
S001HelloWordParser.expression_return ret = parser.expression();
//acquire parse result
CommonTree ast = (CommonTree) ret.tree;
printTree(ast);
return ast;
} catch (RecognitionException e) {
throw new IllegalStateException("Recognition exception is never thrown, only declared.");
}
}
问题出在我的 S001HelloWordParser 文件中,没有像 expression() 这样的方法,也没有静态 class 名称 'expression_return',所以我无法创建变量 'ret' 我可以对谁调用 tree() 方法。我在这里缺少什么吗?
我还能如何生成树?或者对使用 antlr 4.7 版本的过程有什么想法吗?
请帮忙。谢谢!
I am trying to run strp by step instruction as per - http://meri-stuff.blogspot.com/2011/08/antlr-tutorial-hello-word.html
不要。那个教程是关于 ANTLR 3 的,你需要找一个 ANTLR 4 教程。
The issue is in my S001HelloWordParser file, there is no method as expression() nor is there a static class names 'expression_return'
这意味着您的 S001HelloWord
语法不包含名为 expression
的解析器规则。
从源头开始:https://github.com/antlr/antlr4/blob/master/doc/getting-started.md
这是关于 ANTLR4 表达式解析器的问答(包含完整代码示例):If/else statements in ANTLR using listeners
我正在尝试 运行 按照 - http://meri-stuff.blogspot.com/2011/08/antlr-tutorial-hello-word.html
的步骤说明进行 strp根据上述网页的原始代码 -
public CommonTree compile(String expression) {
try {
//lexer splits input into tokens
ANTLRStringStream input = new ANTLRStringStream(expression);
TokenStream tokens = new CommonTokenStream( new S001HelloWordLexer( input ) );
//parser generates abstract syntax tree
S001HelloWordParser parser = new S001HelloWordParser(tokens);
S001HelloWordParser.expression_return ret = parser.expression();
//acquire parse result
CommonTree ast = (CommonTree) ret.tree;
printTree(ast);
return ast;
} catch (RecognitionException e) {
throw new IllegalStateException("Recognition exception is never thrown, only declared.");
}
}
我修改了下面部分代码:
public CommonTree compile(String expression) {
try {
//lexer splits input into tokens
ANTLRStringStream input = new ANTLRStringStream(expression);
TokenStream tokens = new CommonTokenStream( (TokenSource) new S001HelloWordLexer( (CharStream) input ) );
//parser generates abstract syntax tree
S001HelloWordParser parser = new S001HelloWordParser((org.antlr.v4.runtime.TokenStream) tokens);
S001HelloWordParser.expression_return ret = parser.expression();
//acquire parse result
CommonTree ast = (CommonTree) ret.tree;
printTree(ast);
return ast;
} catch (RecognitionException e) {
throw new IllegalStateException("Recognition exception is never thrown, only declared.");
}
}
问题出在我的 S001HelloWordParser 文件中,没有像 expression() 这样的方法,也没有静态 class 名称 'expression_return',所以我无法创建变量 'ret' 我可以对谁调用 tree() 方法。我在这里缺少什么吗?
我还能如何生成树?或者对使用 antlr 4.7 版本的过程有什么想法吗?
请帮忙。谢谢!
I am trying to run strp by step instruction as per - http://meri-stuff.blogspot.com/2011/08/antlr-tutorial-hello-word.html
不要。那个教程是关于 ANTLR 3 的,你需要找一个 ANTLR 4 教程。
The issue is in my S001HelloWordParser file, there is no method as expression() nor is there a static class names 'expression_return'
这意味着您的 S001HelloWord
语法不包含名为 expression
的解析器规则。
从源头开始:https://github.com/antlr/antlr4/blob/master/doc/getting-started.md
这是关于 ANTLR4 表达式解析器的问答(包含完整代码示例):If/else statements in ANTLR using listeners