使用 C# 和 gppg,如何构建抽象语法树?
有没有办法几乎开箱即用地做到这一点?
我可以编写一个大方法,使用收集到的标记来确定哪些叶子应该放在哪些分支中,并最终填充 TreeNode 对象,但由于 gppg 已经使用提供的正则表达式处理了所有内容,我想知道是否有更简单的方法吗? 即使没有,任何有关如何最好地解决创建 AST 问题的指示都将不胜感激。
如果我说了一些愚蠢的话,我很抱歉,我才刚刚开始玩编译器游戏。 :)
Is there a way to do this almost out-of-the-box?
I could go and write a big method that would use the collected tokens to figure out which leaves should be put in which branches and in the end populate a TreeNode object, but since gppg already handled everything by using supplied regular expressions, I was wondering if there's an easier way? Even if not, any pointers as to how best to approach the problem of creating an AST would be appreciated.
Apologies if I said anything silly, I'm only just beginning to play the compiler game. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅 MGrammar 和 Oslo...
http://msdn.microsoft.com/oslo
http://channel9.msdn.com/pdc2008/TL31/
See MGrammar and Oslo...
http://msdn.microsoft.com/oslo
http://channel9.msdn.com/pdc2008/TL31/
在语法文件中声明一个属性,该属性将保留 AST 的根:
<前><代码>{%
公共 BatchNode 批处理;
公共错误处理程序 yyhldr;
私有 TransformationContext _txContext = TransformationContext.Instance;
%}
开始使用构建 AST 节点的操作编写语法:
调用解析器:
In your syntax file declare a property which will keep the root of your AST:
Start writing your grammar with actions which build nodes of your AST:
Call parser:
看看 ANTLR,几年前我用 C# 编写了一个简单的 .NET 编译器。
Take a look at ANTLR, I did a simple .NET compiler written in C# with it few years back.