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

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

问题描述

我正在阅读关于解析器和解析器生成器,并在wikipedia的LR解析-page中找到这个语句:


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


为什么会这样?使用google,我只发现C可以被完美地解析LR(1),但C + +(1)需要LR(∞)。

解决方案

有一个有趣的主题Lambda the Ultimate ,其中讨论了 C ++的LALR语法



它包含指向博士论文,其中包括C ++解析的讨论,其中规定:


C ++语法不明确,
上下文相关和潜在的
需要无限的前瞻以解决
一些歧义。


给出一些例子(见pdf的第147页)。


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

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

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

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

解决方案

There is an interesting thread on Lambda the Ultimate that discusses the LALR grammar for C++.

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

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

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

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

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