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

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

问题描述

// 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;



我能做些什么这是compilation_unit_return类型,提取它的根,它的类在x,其方法等?我一定要提取其适配器了呢?我怎么做?注意,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延伸并具有启动和停止对象,我不知道这是什么是...
此外,还有是ANTLR提供了这个TreeVisitor类看起来令人困惑。它需要一个适配器将作为参数传递给它的构造(如果没有它会使用默认的CommonTreeAdaptor),TT就是为什么我问生根粉如何获取适配器早期的教上。和其它问题太...
对于API,U可以参考 HTTP: //www.antlr.org/api/CSharp/annotated.html

现在我在这里打......如果妳做懂不懂,请帮助出。万分感谢。

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

推荐答案

我还没有与ANTLR工作从C#,但下面的链接API, BaseTree 显然不是一个接口 - 这是一个的,它具有公共属性:键入来获取类型节点,文本来获得(我假设),对应的源文本和孩子来获取子节点。还能做什么,你需要走呢?

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天全站免登陆