解析需要大量前瞻的案例

发布于 2024-12-05 00:52:52 字数 133 浏览 4 评论 0原文

大多数解析可以通过仅查看下一个符号(用于词法分析的字符,用于正确解析的标记)来完成,并且大多数剩余情况可以通过仅查看此后的一个符号来处理。

对于实际使用的编程语言或数据格式,是否有任何实际情况需要多个或无限多个前向符号(或等效的回溯)?

Most parsing can be done by looking only at the next symbol (character for lexical analysis, token for parsing proper), and most of the remaining cases can be handled by looking at only one symbol after that.

Are there any practical cases - for programming languages or data formats in actual use - that need several or indefinitely many symbols of lookahead (or equivalently backtracking)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

得不到的就毁灭 2024-12-12 00:52:52

我记得,Fortran 是一种需要大的前瞻缓冲区的语言。解析 Fortran 需要(理论上)无界的先行,尽管大多数实现都限制语句行的长度,这对先行缓冲区的大小施加了限制。

另请参阅 为什么无法解析 C++ 的所选答案使用 LR(1) 解析器?。特别是引用:

“C++ 语法是不明确的,依赖于上下文,并且可能需要无限的前瞻来解决一些歧义”。

As I recall, Fortran is one language in which you need a big lookahead buffer. Parsing Fortran requires (in theory) unbounded lookahead, although most implementations limit the length of a statement line, which puts a limit on the size of the lookahead buffer.

Also, see the selected answer for Why can't C++ be parsed with a LR(1) parser?. In particular, the quote:

"C++ grammar is ambiguous, context-dependent and potentially requires infinite lookahead to resolve some ambiguities".

锦欢 2024-12-12 00:52:52

Knuth 证明了任何 LR(k) 文法都可以机械地转换为 LR(1) 文法。另外,据我所知,任何 LR(k) 语法都可以在与解析字符串的长度成正比的时间内进行解析。由于它包含 LR(1),所以我不知道实现 k > 的 LR(k) 解析有什么用处。 1.

我从未研究过 LR(k)->LR(1) 变换,因此在某些情况下它可能不太实用。

Knuth proved that any LR(k) grammar can be mechanically transformed to a LR(1) one. Also, AFAIK, any LR(k) grammar can be parsed in time proportional to the length of the parsed string. As it includes LR(1), I don't see what use there would be for implementing LR(k) parsing with k > 1.

I never studied the LR(k)->LR(1) transformation, so it may be possible that it is not that practical for some cases, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文