ANTLR,步骤顺序
我正在尝试为 ANTLR 中的 C# 等语言设计一个编译器。但我不完全理解应该采取的正确步骤顺序。
我是这样看的:
- 首先,我定义 Lexer 标记,
- 然后是语法规则(使用重写规则来构建 AST),以及收集有关类和方法声明的信息的操作(以便我可以在下一步中解析方法调用),
- 最后,我创建“树语法”,它遍历 AST 树并调用生成(虚拟)机器语言操作码的规则。
这是正确的吗?第二步的角色是读取方法的声明并构建 AST 吗?
如何在不构建 AST 的情况下解析重载方法的声明? (回补?)
I am trying to design a compiler for a language like C# in ANTLR. But I don't fully comprehend the proper order of steps that should be undertaken.
This is how I see it:
- First I define Lexer tokens
- Then grammar rules (with rewrite rules to build AST) with actions that gather informations about classes and methods declarations (so that I can resolve method invocations in the next step)
- Finally, I create "tree grammar" which traverse AST tree and invokes rules that generate the opcodes of (virtual) machine language.
Is this correct? Is the second step's role reading methods' declarations and building AST?
How can I resolve overloaded methods' declarations without build AST ? (backpatching?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看语言实现模式它解释了如何创建您自己的语言(解释型和字节码/类似 VM)。目前,您的问题太广泛了,我认为没有人能够在论坛上发布答案来解释如何从头到尾创建自己的语言的所有细节。
当然,如果您有更具体的问题,请随时提出。
祝你好运!
Have a look at Language Implementation Patterns it explains how to create your own languages (both interpreted and byte-code/VM-like). At the moment, your questions are too broad, and I don't think anyone is able to post an answer in a forum that explains all the details of how to create your own language from start to finish.
Feel free to ask more specific questions when you have them, of course.
Good luck!