用于 Java 的 Earley 解析器生成器
我正在寻找一个 Earley 解析器 能够生成 Java 输出代码的生成器, 即为词法分析器和解析器生成 Java 代码,并允许包含操作 (实现为 Java 代码)针对语法规则执行。
我查看了两个生成 Java 代码的 Earley 解析器生成器 (Pep 和 PEN) 但它们似乎都不允许将动作嵌入到语法中。
I'm looking for an Earley parser generator that is able to generate Java output code,
i.e. that generates Java code for the lexer and parser, and allows to include actions
(realised as Java code) that are performed for the grammar rules.
I looked at two Earley parser generators that generate Java code (Pep and PEN)
but none of them seems to allow embedding actions into the grammar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解你的问题,那么“在语法中嵌入动作”是指在语法中插入语义动作,以便它们“内联”执行(例如,在解析阶段,当解析输入时)。
Earley 解析器不适合这样做,因为它们允许任何上下文无关的语言语法(甚至是不明确的语法)。 请参阅: http://en.wikipedia.org/wiki/Earley_algorithm
基本上:对于给定的执行和给定状态 Earley 解析器包含所有可能的解析状态。 传统的方法(例如Yacc/Bison)是在规则或部分输入匹配后执行语义动作。 但是,当解析不明确的语法(例如具有Reduce/Reduce 冲突的语法)时,Earley 解析器将同时处理“归约”,并且由于其不明确性,将不知道应该执行哪个操作。
使用 Earley 解析器的通常方法是解析输入并最终得到一个解析树的森林,稍后您可以在其中执行所需的操作(例如,丢弃那些您知道无效的内容,或者应用一些语义)对它们采取的行动或转变)。
然而,有一些关于这个主题的研究试图执行一些内联操作(抱歉,只找到了这个链接)https://link.springer.com/article/10.1007/s00236-009-0107-6
If I understood your question, by "embed actions in the grammar" you mean inserting semantic actions within the grammar so they are executed "inline" (e.g. during the parsing phase, as the input is being parsed).
Earley parsers are not suitable for this because they allow any context-free language grammars (even ambiguous ones). See: http://en.wikipedia.org/wiki/Earley_algorithm
Basically: for a given execution and a given state the Earley parser contains all possible parsing states. The traditional approach (e.g. Yacc/Bison) is to execute a semantic action after a rule or partial input is matched. But when parsing an ambiguous grammar (e.g. one with a Reduce/Reduce conflict) an Earley parser will take care of both "reductions" and, due to its ambiguity, won't know which action should be executed.
The usual way to use Earley parsers is to parse the input and end up having a forest of parse trees on which you later perform the desired actions (e.g. discarding those you know are not valid, or applying some semantic actions or transformations on them).
However, there's been some research on this topic trying to execute some actions inline (sorry, only found this link) https://link.springer.com/article/10.1007/s00236-009-0107-6
一般来说,将动作直接嵌入到语法中并不是一个好主意。 如果动作与语法分离,那就更好了。
Generally speaking, it is not a good idea to embed the actions directly into the grammar. It is better if the actions are decoupled from the grammar.
不确定这是否是一个答案,但我经常使用的扫描仪生成器之一是 JFlex,它输出 Java 代码。
它与 CUP 密切合作,在操作方面更接近一些。
Not sure if this is an answer, but one of the scanner generators I regularly use is JFlex, which outputs Java code.
It works closely with CUP, which is a bit closer regarding actions.