我现在如何在Irony中使用AST? [英] How do I work with the AST in Irony now?
问题描述
我有一个可以在Irony控制台中正常工作和解析的语法,但是在AST树视图中我什么也没得到.我一直在关注BASIC-> Javascript文章,网址为: http://www.codeproject.com/Articles/25069/JSBasic-A-BASIC-to-JavaScript-Compiler ,但看来Ast的东西已经全部移走了.我找到了Irony.Interpreter .dll,其中包含一些Ast内容,但似乎都与Expression示例实现捆绑在一起.
我在这里想念什么?我想走树并生成源代码,但不确定从哪里开始.
我已经看到过使用访问者模式的一些提及,我很好用,但是我不知道如何实现并以讽刺喜欢的方式运行它.
查看适当命名的 Sarcasm 项目以获取参考实现基于Irony的语法,解析器和AST的概念.我发现了这个博客条目由作者提供,有助于构建AST.
以下是启动和运行AST的通用指南.
公共替代无效void Accept(IMyNodeVisitor visitor){visitor.Visit(this);}
- 覆盖
Init
(主要是在终端上)或InitChildren
(非-终端).这是AST魔术发生的地方.
为上一步中定义的每个类添加接口 IMyNodeVisitor
并添加 Visit
方法(示例)>
MyTerminal1.AstConfig.NodeType = typeof(MyDerivedNode1);
对于非终端-(示例)
var MyNonTerminal2 =新的NonTerminal("MyNonTerminal2",typeof(MyDerivedNode2));
在语法中启用AST创建:(示例)
LanguageFlags = LanguageFlags.CreateAst;
I have a grammar that works and parses in the Irony console just fine, but I don't get anything in the AST treeview. I was following along with the BASIC->Javascript article found here: http://www.codeproject.com/Articles/25069/JSBasic-A-BASIC-to-JavaScript-Compiler, but it seems that the Ast stuff has all been moved/removed. I found the Irony.Interpreter .dll, which has some Ast stuff in it, but it seems all tied up in the Expression sample implementation.
What am I missing here? I want to walk my tree and generate source code, and I'm not sure where to start.
I've seen some mention of using the visitor pattern, which I'm fine with, but I don't know how to implement it and run it in a way that Irony likes.
Check out the aptly named Sarcasm project for a reference implementation of a grammar, parser, and AST built on Irony. I found this blog entry by the author to be helpful in building the AST.
The following is a general purpose guide to getting the AST up and running.
- Define your grammar (example)
- Create an abstract base class (
MyBaseNode
) deriving fromAstNode
(example). Copy/Paste the methods from the example For each terminal and non-terminal create a new class derived from
MyBaseNode
and- Override
Accept
method (example):
public override void Accept(IMyNodeVisitor visitor) { visitor.Visit(this); }
- Override
Init
(mostly on terminals) orInitChildren
(non-terminals) as appropriate. This is where the AST magic happens.
- Override
Add an interface
IMyNodeVisitor
and add aVisit
method for each class defined in the previous step (example):void Visit(MyDerivedNode1 node);
Set the
ASTNodeType
for each of your terminals and non-terminals in your grammar from step 1.In the grammar enable AST creation: (example)
LanguageFlags = LanguageFlags.CreateAst;
这篇关于我现在如何在Irony中使用AST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!