我在哪里可以获得学习 EBNF 的材料?
扩展巴科斯-诺尔范式:EBNF
我对解析概念非常陌生。 我在哪里可以获得足够容易阅读和遵循的材料来为 boost::spirit 库编写语法,该库使用类似于 EBNF 的语法?
目前我正在研究来自维基百科的EBNF。
Extended Backus–Naur Form: EBNF
I'm very new to parsing concepts. Where can I get sufficiently easy to read and follow material for writing a grammar for the boost::spirit library, which uses a grammar similar to EBNF?
Currently I am looking into EBNF from Wikipedia.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
维基百科的文章是准确的。 如果您有访问权限,请务必阅读 Wirth 关于 EBNF 的原始文章。
另一件需要知道的事情是,EBNF 的设计目的是为了让每个语法结构在开头都有标识关键字的语言能够轻松地手写递归下降解析器。 大括号转换为
while
循环; 方括号(可选内容)转换为if
,替代项转换为if-then-else
或case
语句。 如果您有能力以这种方式设计您的语言,您可以快速敲除解析器并给出良好的错误消息。唯一有点乏味的地方是当您使用的语言中存在具有许多不同优先级的中缀运算符时。 为此,您需要 Dave Hanson 的论文 Compact Recursive-Descent Parsing of表达式。 也许普林斯顿科技报告系列有免费版本,您可以随时查看 Hanson 的 C 前端中的代码结束。
The Wikipedia article is accurate. If you have access, definitely read Wirth's original article on EBNF.
The other thing to know is that EBNF was designed to make it easy to hand-write recursive-descent parsers for languages in which each syntactic construct has identifying keywords at the beginning. Curly braces translate to
while
loops; square brackets (optional stuff) translates toif
, and alternatives translate toif-then-else
orcase
statements. If you have the luxury of designing your language this way you can knock out a parser quickly and give good error messages.The only place this gets a bit tedious is when you have a language in which there are infix operators with many different levels of precedence. For that you want Dave Hanson's paper Compact Recursive-Descent Parsing of Expressions. Maybe the Princeton tech report series has a free version, and you can always look at the code in Hanson's C front end.
BNF 本身很简单,但你需要习惯编译器编写者的思维方式。 它们不一定容易阅读,但以下是加州大学伯克利分校和斯坦福大学的讲义。
BNF itself is simple, but you need to get used to the way compiler writers think. They are not necessarily easy read, but following are lecture notes from UC Berkeley and Stanford.
这里是 php 中的 ebnf 解析器。
此外,了解一些有关正则表达式引擎如何实现的知识可能会有所帮助。 尝试:re2。
Here is an ebnf parser in php.
Also, learning a little about how regular expression engines are implemented might help. Try: re2.
好吧,我认为维基百科是最简单的方法,原因有两个:
此外我建议阅读 标准 BNF 只是为了熟悉其背后的想法。
至少我也总是从维基百科开始,而且它几乎总是有帮助的。
Well, I think that Wikipedia is the simplest way for two reasons:
Also I would suggest reading of standart BNF just to get familiar with the idea behind it.
At least I always start with Wikipedia too, and it almost always helps.