我正在研究一种新语言,在编写语法时,我希望能够测试语法的完整性、冲突和类似情况。我并不真正关心底层的解析器生成器(但是 .NET 的解析器生成器会更好),
所以简而言之,功能列表是:
- 文本编辑器构建功能
- 语法/语义错误报告
- 冲突报告
- 语法测试功能(即窗口
用预期的语法编写代码
以验证其正确性
语法定义)
一个名为 Irony 的 CodePlex 项目确实有与我所要求的类似的东西,但不支持将语法编写为所需的 BNF。
I'm working on a new language and while writting the grammar I'd like to be able to test the grammar for completeness, conflicts and similar. I'm not really concern about the underlaying parser generator (but one for .NET would be preferrable)
So feature list in short would be:
- text editor build functionality
- syntax/sematics error reporting
- conflicts reporting
- grammar test functionality (i.e. window for
writting code in the intended grammar
to verify the correctness of the
grammar definition)
A CodePlex project called Irony does have something simlar to what I'm asking for but does not support writing the grammar as BNF which is required.
发布评论
评论(4)
我推荐 ANTLR 作为解析器生成器。它的功能非常齐全,支持 C# 以及许多其他目标语言。
对于 IDE,有一个名为 ANTLR IDE 的 Eclipse 插件和一个名为 ANTLRWorks,两者都运行良好。
但请注意,ANTLR 使用 LL(*) 算法而不是 LR(k) 算法。尽管如此,它仍然非常好,ANTLRWorks 可以完成大部分必要的左因式分解。
I would recommend ANTLR as a parser generator. It's very feature complete and supports C# as well as a host of other target languages.
For IDEs, there's a plugin for Eclipse called ANTLR IDE and a standalone IDE called ANTLRWorks, both of which work well.
Note, however, that ANTLR uses an LL(*) algorithm instead of a LR(k) algorithm. Still, it's very nice and ANTLRWorks can do most of the necessary left factoring.
当“研究一种新语言”并尝试获得正确的参考 BNF 时,您可能不希望参考语法偏向于任何特定的解析器生成器。为 Bison (LALR(1)) 或 ANTLR(LL*) 编写测试语法的麻烦之一就是您正是这样做的。您也不想陷入“如何以使其实际解析的方式编码 BNF 规则”的问题,大概是因为您对语法感兴趣,而不是对解析器生成器感兴趣。
所以我建议使用完整的上下文无关解析器生成器。这将使您以最自然的形式以最少的努力编写语法。这可能意味着放弃“文本编辑器”、“编辑器测试窗口”……但根据我的经验(检查我的堆栈溢出简介),使用上下文无关的解析器生成器完全淹没了这些细节。编辑-保存-解析并不需要花费太多精力。
我知道 Bison 有一个 GLR 选项,可以提供上下文无关的解析器生成,并且是开源的,因此它可能只用于测试语法。
我们的DMS Software Reengineering Toolkit是商业的,并且还提供了一个GLR解析器,它具有已用于实现大约 30 多种完整语言,包括多种方言中的 C、C++ 和 COBOL 以及更现代的语言,如 Python、Ruby、PHP...。
DMS 和 Bison 之间的区别在于 DMS 的设计支持完整语言分析器/翻译器构建的各个方面(Unicode 词法分析、带错误报告和恢复的 GLR 解析、自动树构建、符号表构建、控制和数据流分析、转换、漂亮打印等)。如果你想认真评估你的“新语言”,你最终需要做所有这些事情,而 Bison 只是这条路上的一小步。 DMS 将全程陪伴您。
When "working on a new language" and trying to get a reference BNF right, you probably don't want to bias your reference grammar towards any particular parser generator. One of the troubles with writing a test grammar for Bison (LALR(1)) or ANTLR(LL*) is you do just exactly that. You also don't want to get hung up in "how do I code the BNF rules in such a way as make it actually parse" presumably because you are interested in working on the grammar, not working on the parser generator.
So I'd recommend using a full context free parser generator. This will let you write the grammar in the most natural form with the least effort. This might mean giving up "text editor", "editor test window", ... but in my experience (check my stack overflow bio) using a context free parser generator overwhelms those niceties completely. Edit-save-parse just doesn't take a lot of effort.
I understand Bison has a GLR option which would provide context-free parser generation, and is open source, and so it might do for just the testing out the grammar.
Our DMS Software Reengineering Toolkit is commercial and also provides a GLR parser, which has been used to implement some 30+ full langauges including C, C++, and COBOL in a number of dialects as well as more modern languages such as Python, Ruby, PHP, ....
The difference between DMS and Bison is that DMS is designed to support all aspects of the construction of a full language analyzer/translator (Unicode lexing, GLR parsing with error reporting and recovery, automatic tree construction, symbol table construction, control and data flow analysis, transformations, prettyprinting, ...). If you wanted to seriously evaluate your "new langauge", you'll eventually need to do all this stuff, and Bison is only a tiny step along this road. DMS will carry you the whole way.
也许您会发现这个工具很有用: Gold Parser Builder
不幸的是,它仅适用于 Windows。
May be you'll find this tool useful: Gold Parser Builder
Unfortunally it is windows only.
看一下 BNFC,它可以从标记为 BNF 的许多目标语言(例如 Haskell、OCaml、C、C++ 和 Java)生成工作代码和 makefile。您将获得一个漂亮的打印机、抽象语法检查器/打印机、您自己的编译器或解释器的框架代码以及 postcript 语言文档。
Have a look at BNFC, which can generate working code and the makefile, from labled BNF, for a number of target languages like: Haskell, OCaml, C, C++, and Java. You get a pretty printer, abstract syntax checker/printer, skeleton code for your own compiler or interpreter, and postcript language documentation.