如何用Java编写某些语法的LALR解析器?
我想编写 Java 代码来为我的语法构建 LALR 解析器。有人可以推荐一些书籍或一些链接,让我可以学习如何为 LALR 解析器编写 Java 代码吗?
I want to write Java code to build a LALR parser for my grammar. Can someone please suggest some books or some links where I can learn how to write Java code for a LALR parser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
手动编写 LALR 解析器很困难,但他可以做到。如果您想了解手动构建解析器背后的理论,请考虑查看 Grune 和 Jacobs 撰写的“解析技术:实用指南”。这是一本关于一般解析技术的优秀书籍,其中关于 LR 解析的章节特别好。
如果您对使用 Java 编写的 LALR 解析器更感兴趣,请考虑研究 Java CUP,它是 Java 的通用解析器生成器。
希望这有帮助!
Writing a LALR parser by hand is difficult, but it can he done. If you want to learn the theory behind constructing parsers for them by hand, consider looking into "Parsing Techniques: A Practical Guide" by Grune and Jacobs. It's an excellent book on general parsing techniques, and the chapter on LR parsing is particularly good.
If you're more interested in just getting a LALR parser that is written in Java, consider looking into Java CUP, which is a general purpose parser generator for Java.
Hope this helps!
您可以将 LALR 功能分为两部分:准备表和解析输入。
第一部分很复杂且容易出错,因此即使您想知道它是如何工作的,我也建议对 LALR 状态(以及分词器 DFA)使用经过验证的工作表生成器。
第二部分包括使用一些非常简单的算法来使用这些表来标记并将输入处理为解析树/具体语法树。如果您愿意的话,您自己实现会更容易,并且您仍然可以完全控制它的工作原理和用途。
在执行解析任务时,我个人使用免费的 GOLD 解析系统,它有一个很好的 UI,用于创建和调试语法,它还生成表文件,然后可以由现有引擎或您自己的实现加载和处理这些文件(这些 CGT 文件的文件格式有详细记录)。
You can split the LALR functionality in two parts: preparation of the tables and parsing the input.
The first part is complex and errorprone, so even if you like knowing how it works I suggest to use a proven working table generator for the LALR states (and for the tokenizer DFA as well).
The second part consists of consuming those tables using some quite simple algorithms to tokenize and process the input into a parse tree/concrete syntax tree. This is easier to implement yourself if you like to do so, and you still have full control over how it works and what it does.
When doing parsing tasks, I personally use the free GOLD Parsing System, which has a nice UI for creating and debugging the grammar and it does also generate table files which can then be loaded and processed by an existing engine or your own implementation (the file format for these CGT files is well documented).
如前所述,您始终会使用解析器生成器来生成 LALAR 解析器。一些这样的 Java 工具是:
As previously stated, you would always use a parser-generator to produce an LALAR parser. A few such tools for Java are:
只是想提一下我的项目 CookCC ( http://coconut2015.github.io/cookcc/ ) 是一个 LALR(1) 解析器 + Lexer(很像 flex)。
CookCC 的独特之处在于您可以使用 Java 注释在 Java 中编写词法分析器和解析器。请参阅此处的计算器示例:https://github。 com/coconut2015/cookcc/blob/master/tests/javaap/calc/Calculator.java
Just want to mention that my project CookCC ( http://coconut2015.github.io/cookcc/ ) is a LALR(1) parser + Lexer (much like flex).
The unique feature of CookCC is that you can write your lexer and parser in Java using Java annotations. See the calculator example here: https://github.com/coconut2015/cookcc/blob/master/tests/javaap/calc/Calculator.java