帮助编译器设计
可能的重复:
学习编写编译器
我需要想出一种类似于 SQL 的虚拟语言,它具有功能非常有限。我以前从未做过任何编译器或解析工作。任何人都可以让我知道一个好的开始点可能是链接或相同的示例。我真是太无知了。
我将使用这种虚拟语言,并将 C/C++ 作为我的主要语言。
谢谢
Possible Duplicate:
Learning to write a compiler
I need to come up with a dummy SQL like language which has very limited features. I have never done any compiler or parsing stuff before. Can anyone let me know a good point to start may be a link or a example of the same. I am so clueless.
I will be using this dummy language with C/C++ as my primary language.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
龙之书通常被认为是一个很好的起点。不过,我还会推荐 ANTLR 书
The Dragon Book is often considered a good starting point. However, I will also recommend the ANTLR book
因此,您想要设计一种新语言,为其构建一个解析器,然后将结果转换为 SQL 调用。
您应该查看 解析器生成器,尤其是 解析器生成器的比较
在设计语言时,您希望使用最强大、最易于使用的解析器生成器测试你的语法,因为你会改变它很多。如果您选择弱解析器生成器,您会发现自己花费更多的精力来重塑语法以使解析器生成器工具满意,而不是考虑语法的意义。
但是一旦拥有了一种定义良好、可解析的语言,您就会发现解析之外还有很多东西。
当实现一种语言时,您需要捕获它的内部表示(例如,解析树或 AST),找到一种方法来分析它的特殊情况,以及将其转换为您的输出语言的方法。总的来说,解析器生成器工具在这里根本无法帮助您,但这却是问题的难点。自己构建所有这些额外的东西比大多数人想象的要多得多。
你真正想要的是一个集成的工具系统,可以解析、构建 AST、分析它们、翻译等等。这样的工具并不多。我们的 DMS 软件重组工具包提供了您所需的所有基础机制。您可能不想实际使用它,但您应该了解这种工具,以便您可以有意识地选择自己完成这一切。
So you want to design a new langauge, build a parser for it, and then translate the result to SQL calls.
You should check out Parser Generators, especially the link to comparision of parser generators
When designing a language, you want the strongest, easiest-to-use parser generator to test your grammars, because you will be changing it a lot. If you choose a weak parser generator, you can find yourself spending more energy reshaping the grammer to make the parser generator tool happy than you will thinking about what grammar makes sense.
But once having a well-defined, parseable language, you discover there is considerably life-beyond-parsing.
When implementing a language, you need to capture an internal representation of it (e.g., a parse tree or AST), find a way to analye it for special cases, and means to transform it to your output language. By and large, parser generator tools don't help you at all here, and yet this is the hard part of the problem. And building all this extra stuff yourself is much more work than most people imagine.
What you actually want is an integrated system of tools that parse, build ASTs, can analyze them, can translate, etc. There aren't many tools like this. Our DMS Software Reengineering Toolkit provides all the machinery you need as foundations. You might not want actually use it, but you should know about this kind of tool so you can make a conscious choice to do it all yourself.
我去年参加了编译器构建课程,我们使用了这本书
编译器由 Kenneth C. Louden 构建
它非常详细,具有良好的理论背景。同时作者给出了足够多的例子并使用了非常丰富的图表,让你在学习的过程中永远不会迷失方向。最终,一个玩具语言的 C 编译器将在后面的章节中列出。
我真的很喜欢它!
I did a compiler construction course last year and we used the book
Compiler Construction by Kenneth C. Louden
It is very detailed with a good theoretical background. At the same time the author gives enough examples and uses very informative figures, so that you're never lost while learning. Eventually a compiler in C for a toy language is listed in the later chapters.
I really liked it!