挂其他问题吗?
什么是“挂其他”问题? (这是正确的名字吗?)
遵循 C++ 编码标准(忘记了哪个)我总是 使用带有控制结构的括号(块)。所以我不 通常有这个问题(最后一个(?) 其他属于),但为了理解可能出现的问题 外国代码如果能牢固地理解就好了 这个问题。我记得在一本关于 帕斯卡很多年前写过,但我找不到那本书。
What is the "hanging else" problem? (Is that the right name?)
Following a C++ coding standard (forgot which one) I always
use brackets (block) with control structures. So I don't
normally have this problem (to which "if" does the last(?)
else belong), but for understanding possible problems in
foreign code it would be nice with a firm understanding of
this problem. I remember reading about it in a book about
Pascal many years ago, but I can't find that book.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我没看出帕斯卡有什么问题吗?
这个缩进不正确。
删除 x = 1 之后的分号将使其正确缩进。
这个缩进正确
I don't see the problem for Pascal?
This one is incorrectly indented.
Removing the semi-colon from after x = 1 would make it correctly indented.
This one correctly indented
别的就暧昧了。
这里有一些信息: http://theory.stanford.edu/~amitp /yapps/yapps-doc/node3.html
但经典的例子是:
vs
Ambiguous else.
Some info here: http://theory.stanford.edu/~amitp/yapps/yapps-doc/node3.html
But the classic example is:
vs.
else
属于哪个if
?(显然你应该忽略缩进。)
这就是“悬挂其他问题”。
C/C++ 通过制定一条规则来消除歧义,该规则规定不能将 -
if
-without-an-else
作为if--
if
-with-an-else
的主体。Which
if
does theelse
belong to?(Obviously you should ignore the indentation.)
That's the "hanging else problem".
C/C++ gets rid of the ambiguity by having a rule that says you can't have an-
if
-without-an-else
as theif
-body of an-if
-with-an-else
.从语言设计的角度来看这个问题。
if
BNF 语法>-else
:现在从解析器的角度来看:
当您到达 ELSE_TOKEN 时,解析器有两个选项:SHIFT 或 REDUCE。问题是选择哪一个需要解析器必须遵循的另一条规则。当给出此选项时,大多数解析器生成器默认为 SHIFT。
Looking at this from a langauge design point of view.
The standard BNF-like grammar for
if
-else
:Now from a parsers point of view:
When you get to the ELSE_TOKEN the parser has two options, SHIFT or REDUCE. The problem is that which to choose requires another rule that the parser must follow. Most parsers generators default to SHIFT when given this option.