使用ANTLR构建自己的C#编译器:编译单元 [英] Building own C# compiler using ANTLR: Compilation Unit

查看:375
本文介绍了使用ANTLR构建自己的C#编译器:编译单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//创建一个从传递给我们的输入流读取的扫描器
CSLexer lexer = new CSLexer(new ANTLRFileStream(f));
tokens.TokenSource = lexer;

// Create a scanner that reads from the input stream passed to us CSLexer lexer = new CSLexer(new ANTLRFileStream(f)); tokens.TokenSource = lexer;

// Create a parser that reads from the scanner
CSParser parser = new CSParser(tokens);

// start parsing at the compilationUnit rule
CSParser.compilation_unit_return x = parser.compilation_unit();
object ast = x.Tree;

我可以用x做的是compilation_unit_return类型,提取其根,其方法等?我必须提取其适配器吗?我怎么做?注意,compilation_unit_return在我的CSParser(由ANTLR自动生成)中定义为:

What can I do with the x which is of compilation_unit_return type, to extract its root , its classes, its methods etc? Do I have to extract its Adaptor out? How do I do that? Note that the compilation_unit_return is defined as such in my CSParser (which is automatically generated by ANTLR):

public class compilation_unit_return : ParserRuleReturnScope
    {
        private object tree;
        override public object Tree
        {
            get { return tree; }
            set { tree = (object) value; }
        }
    };

但是我得到的树是类型对象。我运行使用调试器,似乎看到它是类型BaseTree。但是BaseTree是一个接口!我不知道它如何与BaseTree相关,不知道如何从这棵树提取细节。
我需要写访问者访问其类,方法,变量....
ParserRuleReturn类从RuleReturnScope扩展,并有一个开始和停止对象,我不知道它是什么is ...
此外,有一个由ANTLR提供的TreeVisitor类,看起来很混乱。它需要一个适配器作为参数传递给它的构造函数(如果不是它将使用默认的CommonTreeAdaptor),tt为什么我问abt如何获取适配器eariler。还有其他问题...
对于API,u可以参考 http ://www.antlr.org/api/CSharp/annotated.html

However the tree I am getting is of the type object. I run using the debugger and seemed to see that it is of the type BaseTree. But BaseTree is an interface! I don't know how it relates to BaseTree and don't know how to extract details out from this tree. I need to write a visitor which has visit to its class, method, variables.... Tthe ParserRuleReturn class extends from RuleReturnScope and has a start and stop object, which I don't know what it is... Furthermore, there is this TreeVisitor class provided by ANTLR which looks confusing. It requires an Adaptor to be pass as a parameter to its constructor (if not it will use the default CommonTreeAdaptor), tt's why I asked abt the how to obtain the Adaptor eariler on. And other issues too... For the API, u can refer to http://www.antlr.org/api/CSharp/annotated.html

现在我在这里受到攻击...如果你知道什么,pls帮助。感谢一百万。

Now I am struck here... If u do know anything, pls help out. Thanks a million.

推荐答案

我从来没有使用过C#的ANTLR, $ c> BaseTree 显然不是一个界面 - 这是一个,并且它具有公共属性:键入以获取节点类型, Text 假设)对应于它的源文本,并且 Children 来获取子节点。你还需要什么呢?

I haven't ever worked with ANTLR from C#, but following your link to API, BaseTree is clearly not an interface - it's a class, and it has public properties: Type to get type of the node, Text to get (I assume) source text corresponding to it, and Children to get the child nodes. What else do you need to walk it?

这篇关于使用ANTLR构建自己的C#编译器:编译单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆