ANTLR访客类在C#中为解析树返回null [英] ANTLR Visitor Class is returning null for Parse tree in C#
问题描述
I am trying to build an AST using the visitor pattern on ANTLR Java Grammar (Java Grammar) in C# (.Net Core 3.1). I have created IJavaParserVisitor
, JavaParserbaseVisitor
, JavaLexer
, and JavaParser
file for grammar and created parse tree for java source file. But when I am trying to create AST using JavaParserBaseVisitor.Visit() I am getting null as a result.
AntlrFileStream stream = new AntlrFileStream(file);
ITokenSource lexer = new JavaLexer(stream);
ITokenStream tokens = new CommonTokenStream(lexer);
JavaParser parser = new JavaParser(tokens);
parser.BuildParseTree = true;
IParseTree tree = parser.compilationUnit();
var result = tree.ToStringTree(parser);
JavaParserBaseVisitor<JavaParser> visitor = new
JavaParserBaseVisitor<JavaParser>();
var ast= visitor.Visit(tree); -- **Its always NULL**
我不是这里想念的.
推荐答案
" {[]}"很好那就是"ToString()";解析树中节点的表示形式.如果不确定节点是什么,请尝试查看"tree.GetText()".和"tree.GetType()";在调试器中.
"{[]}" is fine. That's the "ToString()" representation of a node in the parse tree. If you aren't sure what a node is, trying looking at "tree.GetText()" and "tree.GetType()" in the debugger.
但是,您的访客代码看起来不正确.您需要类似公共类MyVisitor:JavaParserBaseVisitor< AST>"之类的内容. {...}",其中AST是用于表示抽象语法树中的节点的类或接口.那就是您从每个访问者那里返回的内容,而不是JavaParser或IParseTree返回的内容.在您的访问者类中,您将需要覆盖所需的所有实现,并计算并返回与该解析树节点相对应的AST(或仅返回一个特定孩子的AST).在每种访客方法中,您将需要为每个孩子呼叫访客.例如,VisitForInit(...){var r = VisitExpressionList(context.expressionList()); ...;返回新的ForAST(r); }.
But, your visitor code doesn't look right. You need something like "public class MyVisitor : JavaParserBaseVisitor<AST> {...}", where AST is a class or interface used to represent a node in the abstract syntax tree. That's what you're returning from each visitor, not a JavaParser, or an IParseTree. In your visitor class, you're going to need to override the implementations for everything you want, computing and returning the AST corresponding to that parse tree node (or just the AST for one particular child). In each visitor method, you will need to call the visitor for each child. For example, VisitForInit(...) { var r = VisitExpressionList(context.expressionList()); ... ; return new ForAST(r); }.
请注意,有些人会创建许多从AST子类化的AST节点类,它们代表不同的AST节点类型.例如,您可能需要一个用于语句,另一个需要变量decl等,如果要为所有Java创建完整的AST,则需要做很多工作.另外,您可以创建一个常规AST节点类,该类具有为解析树节点创建的子AST列表以及一个区分AST节点类型的枚举.
Note, some people create a number of AST node classes subclassed from AST which represent different AST node types. You might, for example, need one for statements, another for variable decls, etc. You have a lot of work ahead of you if you intend to create a complete AST for all of Java. Alternatively, you could create a general AST node class, which has a list of children ASTs that you created for the parse tree node and an enum to distinguish the type of the AST node.
If you like to see a real-world example of how this works, check out the visitor for constructing a XPath AST expression for an XPath engine I just ported to C# and extending to XPath 3.1. This constructs an AST (here just an object) for an expression.
这篇关于ANTLR访客类在C#中为解析树返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!