描述正则表达式的上下文无关语法?
我正在尝试编写一个正则表达式引擎。 我想手工编写一个递归下降解析器。 对于正则表达式语言(不是可以用正则表达式描述的语言)来说,没有左递归的上下文无关语法会是什么样子? 重构语法糖是否是最简单的,即将 a+
更改为 aa*
? 提前致谢!
I'm trying to write a regular expression engine. I'd like to write a recursive descent parser by hand. What would a context-free grammar without left recursion for the language of regular expressions (not the languages that can be described by regular expressions) look like? Would it be easiest to re-factor out the syntactic sugar, i.e. change a+
to aa*
? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
左递归:
右递归:
不明确形式:
Left recursion:
Right recursion:
Ambiguous form:
您可以查看Plan 9 grep 的源代码。 文件 grep.y 有一个用于正则表达式的 yacc (LALR(1),如果我没记错的话)语法。 您也许可以从 yacc 语法开始,并重写它以进行递归下降解析。
You could look at the source code for Plan 9 grep. The file grep.y has a yacc (LALR(1) if I recall correctly) grammar for regular expressions. You might be able to start from the yacc grammar, and rewrite it for recursive descent parsing.
关于左递归的维基百科文章提供了关于如何实现这一点的很好的信息。
The wikipedia article on Left Recursion gives pretty good info on how to pull this off.