为什么不能用 LR(1) 解析器解析 C++? [英] Why can't C++ be parsed with a LR(1) parser?

查看:47
本文介绍了为什么不能用 LR(1) 解析器解析 C++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关解析器和解析器生成器的文章,并在维基百科的 LR parsing -page 中找到了此语句:

I was reading about parsers and parser generators and found this statement in wikipedia's LR parsing -page:

可以使用 LR 解析器的某些变体来解析许多编程语言.一个值得注意的例外是 C++.

Many programming languages can be parsed using some variation of an LR parser. One notable exception is C++.

为什么会这样?C++ 的什么特殊属性导致它无法使用 LR 解析器进行解析?

Why is it so? What particular property of C++ causes it to be impossible to parse with LR parsers?

使用google,我只发现C可以用LR(1)完美解析,但C++需要LR(∞).

Using google, I only found that C can be perfectly parsed with LR(1) but C++ requires LR(∞).

推荐答案

Lambda the Ultimate 上有一个有趣的话题 讨论了 C++ 的 LALR 语法.

它包含一个指向博士论文的链接其中包括对 C++ 解析的讨论,其中指出:

It includes a link to a PhD thesis that includes a discussion of C++ parsing, which states that:

"C++ 语法有歧义,上下文相关的和潜在的需要无限前瞻来解决一些歧义".

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

接着给出了一些例子(见 pdf 的第 147 页).

It goes on to give a number of examples (see page 147 of the pdf).

例子是:

int(x), y, *const z;

意义

int x;
int y;
int *const z;

比较:

int(x), y, new int;

意义

(int(x)), (y), (new int));

(逗号分隔的表达式).

(a comma-separated expression).

两个标记序列具有相同的初始子序列但不同的解析树,这取决于最后一个元素.在消除歧义的标记之前可以有任意多个标记.

The two token sequences have the same initial subsequence but different parse trees, which depend on the last element. There can be arbitrarily many tokens before the disambiguating one.

这篇关于为什么不能用 LR(1) 解析器解析 C++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆