挂其他问题吗?

发布于 2024-08-05 16:04:24 字数 165 浏览 10 评论 0原文

什么是“挂其他”问题? (这是正确的名字吗?)

遵循 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 技术交流群。

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

发布评论

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

评论(4

盛装女皇 2024-08-12 16:04:25

我没看出帕斯卡有什么问题吗?

这个缩进不正确。

if a then
  if b then
     x = 1;
  else
     y = 1;

删除 x = 1 之后的分号将使其正确缩进。

这个缩进正确

if a then
  if b then
     x = 1;
else
  y = 1;

I don't see the problem for Pascal?

This one is incorrectly indented.

if a then
  if b then
     x = 1;
  else
     y = 1;

Removing the semi-colon from after x = 1 would make it correctly indented.

This one correctly indented

if a then
  if b then
     x = 1;
else
  y = 1;
淡紫姑娘! 2024-08-12 16:04:24

别的就暧昧了。

这里有一些信息: http://theory.stanford.edu/~amitp /yapps/yapps-doc/node3.html

但经典的例子是:

if a then
  if b then
     x = 1;
  else 
     y = 1;

vs

if a then
  if b then
     x = 1;
else 
  y = 1;

Ambiguous else.

Some info here: http://theory.stanford.edu/~amitp/yapps/yapps-doc/node3.html

But the classic example is:

if a then
  if b then
     x = 1;
  else 
     y = 1;

vs.

if a then
  if b then
     x = 1;
else 
  y = 1;
孤寂小茶 2024-08-12 16:04:24

else 属于哪个 if

if (a < b)
    if (c < d)
        a = b + d;
    else
        b = a + c;

(显然你应该忽略缩进。)

这就是“悬挂其他问题”。

C/C++ 通过制定一条规则来消除歧义,该规则规定不能将 -if-without-an-else 作为 if--if-with-an-else 的主体。

Which if does the else belong to?

if (a < b)
    if (c < d)
        a = b + d;
    else
        b = a + c;

(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 the if-body of an-if-with-an-else.

最后的乘客 2024-08-12 16:04:24

从语言设计的角度来看这个问题。

ifBNF 语法>-else

Statement :-   .. STUFF..
          |    IfStatement

IfStatement :- IF_TOKEN '(' BoolExpression ')' Statement IfElseOpt

IfElseOpt :-   /* Empty */
          |    ELSE_TOKEN Statement

现在从解析器的角度来看:

if (cond1) Statement1
   if (cond2) Statement2
else Statement3

当您到达 ELSE_TOKEN 时,解析器有两个选项:SHIFT 或 REDUCE。问题是选择哪一个需要解析器必须遵循的另一条规则。当给出此选项时,大多数解析器生成器默认为 SHIFT。

Looking at this from a langauge design point of view.

The standard BNF-like grammar for if-else:

Statement :-   .. STUFF..
          |    IfStatement

IfStatement :- IF_TOKEN '(' BoolExpression ')' Statement IfElseOpt

IfElseOpt :-   /* Empty */
          |    ELSE_TOKEN Statement

Now from a parsers point of view:

if (cond1) Statement1
   if (cond2) Statement2
else Statement3

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.

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