ANTLR vs.Happy vs.其他解析器生成器 [英] ANTLR vs. Happy vs. other parser generators

查看:136
本文介绍了ANTLR vs.Happy vs.其他解析器生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写两种语言之间的翻译器,在互联网上阅读一番之后,我决定使用ANTLR.我不得不从头开始学习它,但是除了消除左递归带来的麻烦外,到目前为止一切都还不错.

I want to write a translator between two languages, and after some reading on the Internet I've decided to go with ANTLR. I had to learn it from scratch, but besides some trouble with eliminating left recursion everything went fine until now.

但是,今天有人告诉我检查一下基于Haskell的解析器生成器Happy.我没有Haskell知识,因此,如果Happy确实比ANTLR好,并且值得学习,我可以使用一些建议.

However, today some guy told me to check out Happy, a Haskell based parser generator. I have no Haskell knowledge, so I could use some advice, if Happy is indeed better than ANTLR and if it's worth learning it.

特别让我担心的是,我的翻译人员需要支持宏替换,而我还不知道在ANTLR中该如何做.也许在快乐中这更容易做到?

Specifically what concerns me is that my translator needs to support macro substitution, which I have no idea yet how to do in ANTLR. Maybe in Happy this is easier to do?

或者,如果认为其他解析器生成器更好,我很高兴听到它们的消息.

Or if think other parser generators are even better, I'd be glad to hear about them.

推荐答案

人们一直相信,如果他们只是得到一个解析器,他们就已经做到了 在构建语言工具时.那就错了.解析器将您带到山麓 喜马拉雅山脉的然后中,您需要开始认真攀爬.

People keep believing that if they just get a parser, they've got it made when building language tools. Thats just wrong. Parsers get you to the foothills of the Himalayas then you need start climbing seriously.

如果您想为构建语言翻译提供行业实力的支持,请参阅我们的 DMS软件再造工具包. DMS提供

If you want industrial-strength support for building language translators, see our DMS Software Reengineering Toolkit. DMS provides

  • 基于Unicode的词法分析器
  • 完整的无上下文解析器(左递归吗?没问题!任意先行吗?没问题.语法不明确?没问题)
  • 用于C,C#,COBOL,Java,C ++,JavaScript等的完整前端 (包括用于C和C ++的完整预处理器)
  • 自动构建AST
  • 支持使用任意作用域规则构建符号表
  • 属性语法评估,以构建利用树结构的分析器
  • 支持控制和数据流分析(以及针对完整的C,Java和COBOL的实现),
  • 使用源语言和目标语言的语法进行源到源转换
  • AST将源代码进行漂亮打印,以重现目标语言文本
  • Unicode-based lexers
  • full context-free parsers (left recursion? No problem! Arbitrary lookahead? No problem. Ambiguous grammars? No problem)
  • full front ends for C, C#, COBOL, Java, C++, JavaScript, ... (including full preprocessors for C and C++)
  • automatic construction of ASTs
  • support for building symbol tables with arbitrary scoping rules
  • attribute grammar evaluation, to build analyzers that leverage the tree structure
  • support for control and data flow analysis (as well realization of this for full C, Java and COBOL),
  • source-to-source transformations using the syntax of the source AND the target language
  • AST to source code prettyprinting, to reproduce target language text

关于OP处理宏的请求:我们的C,COBOL和C ++前端通过以下方式处理它们各自的语言预处理:a)传统的完全扩展方法或b)非扩展(在可行的情况下),以实现对宏的后解析转换宏本身.尽管DMS作为基础并没有专门实现宏处理,但它可以支持DMS的构建和转换.

Regarding the OP's request to handle macros: our C, COBOL and C++ front ends handle their respective language preprocessing by a) the traditional method of full expansion or b) non-expansion (where practical) to enable post-parsing transformation of the macros themselves. While DMS as a foundation doesn't specifically implement macro processing, it can support the construction and transformation of same.

作为使用DMS构建的翻译器的示例,请参见 转换 对于B-2轰炸机而言,对C而言很高兴.这是> 1 MSLOC的100%翻译 实时代码. [这可能会让您知道我们从来没有被允许看到正在翻译的实际程序(非常机密).]是的,JOVIAL具有预处理器,是的,我们将大多数JOVIAL宏转换为等效的C版本.

As an example of a translator built with DMS, see the discussion of converting JOVIAL to C for the B-2 bomber. This is 100% translation for > 1 MSLOC of hard real time code. [It may amuse you to know that we were never allowed to see the actual program being translated (top secret).]. And yes, JOVIAL has a preprocessor, and yes we translated most JOVIAL macros into equivalent C versions.

[Haskell是一种很酷的编程语言,但它本身并不会做任何类似的事情. 这与语言所表达的内容无关.它是关于弄清楚需要什么机器来支持程序操作的任务,以及 花了100个人年建造它.]

[Haskell is a cool programming language but it doesn't do anything like this by itself. This isn't about what's expressible in the language. Its about figuring out what machinery is required to support the task of manipulating programs, and spending 100 man-years building it.]

这篇关于ANTLR vs.Happy vs.其他解析器生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆