编写编译器
我基本上需要为 bibtex 文件制作一个编译器,以便可以查询给定的 bibtex 数据库。现在我熟悉了理论的某些方面,比如自动机、语法、SLR、LR(1) 和 LALR 解析。然而,我仍然发现所有这些都是理论和抽象的,因为我从未应用过它们。 如果有人能够概述构建编译器所需的可靠步骤,那将会有很大帮助。我可能会使用flex和bison/yacc,所以如果你能让我知道设计过程到底是如何进行的,在哪个阶段生成了哪些文件,每个阶段的输出是什么,以及一般情况下如何结合在一起,我也许可以更实际地了解事情是如何完成的......
谢谢!
I basically need to make a compiler for bibtex files, such that a given bibtex database can be queried. Now I'm familiar with certain aspects of theory, like automata, grammars, SLR,LR(1) and LALR parsing. However, I still find all of that theoretical and abstract, since I've never applied it.
It would help a lot if someone could outline the solid steps required to build a compiler. I'll be using flex and bison/yacc probably, so if you could let me know how exactly the design process goes, what files are generated at what stage, what is the output at each stage, and in general how things tie together, I can probably get a more practical view of how things are done...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定要将 bibtex 数据库编译为可执行文件吗?如果查询是您唯一想要的,那么将 bibtex 数据库转换为关系数据库并使用 SQL 查询会更有意义。当然你还是得先解析bibtex,然后生成SQL代码,有些人会称之为“编译”,但它远没有前面提到的龙书里的东西那么复杂。
Bibtex 语法非常简单,因此您可以选择任何解析方法。我什至不会为如此琐碎的语法使用解析器生成器,而是会写一个递归下降解析器。根据您选择的语言,这可能非常简单(例如,如果您使用带有 Parsec 的 Haskell,甚至 C#)。
如果您的额外目标是学习 bison 和 flex 等过时的工具,那么它们当然也能完成这项工作,但这是一种矫枉过正的做法。
编辑:关于经典 lex/yacc 方法的最佳实用读物是 http:// en.wikipedia.org/wiki/The_Unix_Programming_Environment
Are you sure you want to compile a bibtex database into something executable? If querying is the only thing you want, than it will make more sense to translate a bibtex database into a relational one, and query it with SQL. Of course you still have to parse bibtex first, and generate a SQL code out of it, and some will call it "compiling", but it is far less complicated than the stuff in the already mentioned Dragon Book.
Bibtex syntax is extremely trivial, so you can choose any parsing approach. I would not even bother using parser generators for such a trivial grammar, and will jot a recursive descent parser instead. Depending on the language of your choice, it can be really easy (e.g., if you're using Haskell with Parsec, or even a C#).
If your additional goal is to learn the outdated tools like bison and flex, then, of course, they'll do the job too, but it is an overkill.
Edit: the best practical reading on the classic lex/yacc approach is http://en.wikipedia.org/wiki/The_Unix_Programming_Environment
我不是编译器专家,但我确实知道这本书实际上被认为是任何想要编写编译器的人的必读书。是的,封面已经过时了,但从我读到的内容来看,它仍然有许多与代码编译相关的良好模式:
http://www.amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/0201100886
I am not a compiler expert but I do know that this book is actually regarded as necessary reading for anyone that wants to write a compiler. Yes the cover is antiquated but from what I have read it still has many good patterns relevant to code compilation:
http://www.amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/0201100886