是否有可以使用 Wirth 语法的解析器生成器?
即: http://en.wikipedia.org/wiki/Wirth_syntax_notation
似乎大多数人都使用 BNF / EBNF ...
ie: http://en.wikipedia.org/wiki/Wirth_syntax_notation
It seems like most use BNF / EBNF ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,维基百科文章所做的区分简直是吹毛求疵。 “BNF/EBNF”长期以来意味着以大致以下形式编写语法规则:
与其他愚蠢的语言差异(C 中的“{”,以 Pascal 开头,endif 与 fi)一样,您可以通过选择获得非常不同的外观但相同的含义呃,end_rule_marker 的语法和 right_hand_side 的语法不同。
通常,人们允许在(您选择的)引号、其他非终结符名称中使用文字标记,对于 EBNF,通常会使用各种“选择”运算符 |或 / 表示替代,* 或 + 表示“重复”,[ ... ] 或 ?因为
设计语言语法的人正在玩弄语法,所以他们似乎每次写下一些语法时都会发明自己的语法。 (检查语言标准中的各种语法形式;它们都不相同)。是的,如果有一种标准的方法来编写这些东西,我们都会过得更好。但我们不会对 C、C++ 或 C# 这样做(甚至 Microsoft 也无法遵循他们自己的标准);为什么 BNF 应该有所不同?
因为构建解析器生成器的人通常使用它来解析自己的语法,所以他们可以轻松地为每个解析器生成器定义自己的语法。我从未见过有人在维基百科上看到过精确的“WSN”版本,而且我怀疑我在实践中是否会这样做。
这很重要吗?嗯,不。真正重要的是符号背后的解析器生成器的强大功能。您必须改变大多数语法以匹配解析器生成器的功能(好吧,弱点);例如,对于 LL 风格的生成器,你的语法规则不能有左递归(根据我的阅读,WSN 允许这样做)。构建解析器生成器的人们也希望能够方便地表达非解析器问题(例如“如何构建树节点”),因此他们还为非解析问题添加了额外的符号。
因此,真正驱动解析器生成器语法的是解析器生成器处理任意语言的弱点、对解析器生成器有价值的额外好处以及实现它的人的心情。
The distinction made by the Wikipedia article looks to me like it is splitting hairs. "BNF/EBNF" has long meant writing grammar rules in roughly the following form:
As with other silly langauge differences ( "{" in C, begin in Pascal, endif vs. fi) you can get very different looking but identical meaning by choosing different, er, syntax for end_rule_marker and what you are allowed to say for the right_hand_side.
Normally people allow literal tokens in (your choice! of) quotes, other nonterminal names, and for EBNF, various "choice" operators typically | or / for alternatives, * or + for "repeat", [ ... ] or ? for optional, etc.
Because people designing language syntax are playing with syntax, they seem to invent their own every time they write some down. (Check the various syntax formalisms in the language standards; none of them are the same). Yes, we'd all be better off if there were one standard way to write this stuff. But we don't do that for C or C++ or C# (not even Microsoft could follow their own standard); why should BNF be any different?
Because people that build parser generators usually use it to parse their own syntax, they can and so easily define their own for each parser generator. I've never seen one that did precisely the "WSN" version at Wikipedia and I doubt I ever will in practice.
Does it matter much? Well, no. What really matters is the power of the parser generator behind the notation. You have to bend most grammars to match the power (well, weakness) of the parser generator; for instance, for LL-style generators, your grammar rules can't have left recursion (WSN allows that according to my reading). People building parser generators also want to make it convenient to express non-parser issues (such as, "how to build tree nodes") and so they also add extra notation for non-parsing issues.
So what really drives the parser generator syntax are weaknesses of the parser generator to handle arbitrary languages, extra goodies deemed valuable for the parser generator, and the mood of the guy implementing it.