如何阅读和阅读了解C& C++其中使用的标准和语言语法?

发布于 2024-10-05 19:53:37 字数 1016 浏览 3 评论 0原文

我经常发现 C 和 C++ 标准很难阅读和理解,即使是简单的英语句子及其措辞也会给我带来糟糕的体验。最重要的是,语言语法完全是地狱。相信很多人都有同样的感受,至少我的朋友是这样。

我想通过一些例子来理解它。让我们从这个开始(它试图解释为什么C++中的条件表达式C中的条件表达式不同:(引自维基百科

C 和 C++ 中运算符的绑定 被指定(在相应的 标准)由分解语言 语法,而不是优先级 表。这会产生一些微妙的 冲突。例如,在 C 语言中, 条件表达式的语法 是:

逻辑或表达式 ?表达 : 条件表达式

在 C++ 中是:

逻辑或表达式 ?表达 : 赋值表达式

因此,表达式:

e = a < d? a++:a=d

两者的解析方式不同 语言。在 C 语言中,这个表达式是 语法错误,但很多编译器都会解析 其为:

e = ((a < d ? a++ : a) = d)

这是一个语义错误,因为 条件表达式的结果 (可能是 a++)不是左值。 在C++中,它被解析为:

e = (a < d ? a++ : (a = d))

这是一个有效的表达式。

请有人解释一下上面引文中的粗体文本!请用更多的例子来解释语法(特别是那些 C 和 C++ 不同的地方)。

编辑:我只是想知道如何阅读和理解它们。我的意思是,如果我用英语口语解释这一点,那么我会怎么做呢?

I often find C and C++ Standards difficult to read and understand, even the simple English sentences and their wordings give terrible experience. On the top of it, the language grammar is totally hell. I'm sure many share the same feeling, at least my friends do.

I would like to understand it by some examples. Lets begin with this (which tries to explain why the conditional expression in C++ is different from the conditional expression in C : (quoted from wikipedia)

The binding of operators in C and C++
is specified (in the corresponding
Standards) by a factored language
grammar, rather than a precedence
table.
This creates some subtle
conflicts. For example, in C, the
syntax for a conditional expression
is:

logical-OR-expression ? expression :
conditional-expression

while in C++ it is:

logical-OR-expression ? expression :
assignment-expression

Hence, the expression:

e = a < d ? a++ : a = d

is parsed differently in the two
languages. In C, this expression is a
syntax error, but many compilers parse
it as:

e = ((a < d ? a++ : a) = d)

which is a semantic error, since the
result of the conditional-expression
(which might be a++) is not an lvalue.
In C++, it is parsed as:

e = (a < d ? a++ : (a = d))

which is a valid expression.

Please someone explain the bold text in the above quotation! Please explain the grammar with few more examples (especially those where C and C++ differ).

EDIT : I just want to know how to read and understand them. I mean, if I were to explain that in spoken English, then how would I do that?

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

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

发布评论

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

评论(3

欢烬 2024-10-12 19:53:37

以下是表达式的 C++ 语法的说明,它将赋值表达式定义为:

assignment-expression:
    conditional-expression 
    unary-expression assignment-operator assignment-expression

用简单的英语来说,赋值表达式可以是条件表达式或一元表达式,后跟赋值运算符,后跟赋值表达式。所以,你的下一个问题是“什么是条件表达式”,你查阅语法的这一部分,然后继续,直到你到达底部!

因此,在 C++ 中,您可以看到您提到的运算符可以采用 C 中的“条件表达式”,但也可以采用赋值

因此,戴上“C”帽子,您会看到最终的a = d 运算符的一部分作为赋值,这是 C 语法不允许的。相反,有些编译器似乎将运算符的最后部分简单地解析为 a 来给出

 e = (a < d ? a++ : a) = d

但是在 C++ 中,在那里找到赋值是有效的,因此 a = d 被完全接受作为最终表达式,所以你得到

 e = (a < d ? a++ : (a = d))

Here's a description of the C++ grammar for expressions, which defines assignment-expression as

assignment-expression:
    conditional-expression 
    unary-expression assignment-operator assignment-expression

In plain English, an assignment-expression can be either a conditional-expression OR a unary-expression, followed by an assignment-operator, followed by an assignment-expression. So, your next question is 'what's a conditional expression' and you consult that part of the grammar, and keep going until you reach the bottom!

So in C++ you can see the operator you referred to can take a 'conditional-expression' as in C, but also an assignment

So with your 'C' hat on, you look at the final a = d part of the operator as an assignment, which the C syntax shouldn't allow. Instead, it would seem some compilers parse the final part of the operator simple as a to to give

 e = (a < d ? a++ : a) = d

But in C++, it's valid to find an assignment there, so the a = d is accepted in its entirety as the final expression, so you get

 e = (a < d ? a++ : (a = d))
夏の忆 2024-10-12 19:53:37

您必须参考赋值表达式是什么。它是在 C++03 标准 5.17/1 [expr.ass] 中定义的:

assignment-expression:
        conditional-expression
        logical-or-expression assignment-operator assignment-expression
        throw-expression

assignment-operator: one of
        = *= /= %= += -= >>= <<= &= ˆ= |=

它表示赋值表达式可以是任一

  • A 条件表达式
  • 一个逻辑或表达式,后跟一个赋值运算符,后跟一个赋值表达式
  • A 抛出表达式

我不会引用所有内容的语法定义,因为那会非常庞大​​(最明显的是因为条件表达式涵盖了很多东西)。

因此,我们首先看到的是赋值表达式可以是条件表达式,因此我们涵盖了 C 语法。 C++ 标准添加的内容是,: 的右侧也可以包含赋值运算符抛出

提供的示例很好:e = a < d? a++:a=d

这里,: 的右侧是一个逻辑或表达式a,因为一元表达式包含在逻辑或表达式中),后跟一个赋值运算符 (=),然后是一个赋值表达式(d,因为一元表达式包含在赋值表达式中)。

You have to refer to what an assignment-expression is. It is defined in the C++03 standard in 5.17/1 [expr.ass] :

assignment-expression:
        conditional-expression
        logical-or-expression assignment-operator assignment-expression
        throw-expression

assignment-operator: one of
        = *= /= %= += -= >>= <<= &= ˆ= |=

What it says is that an assignment-expression can be either :

  • A conditional-expression
  • A logical-or-expression followed by an assignment-operator followed by an assignment-expression
  • A throw-expression.

I'm not quoting the grammar definition of everything because that would be pretty huge (most notably because condition-expression covers a lot of things).

So the first thing we see is that an assignment-expression can be a conditional-expression, so we have the C syntax covered. What the C++ standard adds is that the right side of the : can also be something containing an assignment-operator or a throw.

The provided example is good : e = a < d ? a++ : a = d.

Here, the right side of the : is a logical-or-expression (a, because unary-expression is included in logical-or-expression), followed by an assignment-operator (=), followed by an assignment-expression (d, because unary-expression is included in assignment-expression).

宛菡 2024-10-12 19:53:37

基本上,类似以下内容:(

assignment-expression:
    conditional-expression 
    unary-expression assignment-operator assignment-expression

如其他一些答案中所述)是用于描述有效 C(或 C++)“语法”的规则。这些写下来的规则也遵循一定的语法——所以我建议你学习该语法,这样你就能够阅读和理解这些规则。

首先,您可以研究Backus-Naur Form,如果您还不知道的话。 (我的链接指向有关此主题的 Wikipedia 文章。)虽然 C++ 标准不使用 Backus-Naur 形式 (IIRC),但它足够相似,足以帮助您入门。

Basically, things like:

assignment-expression:
    conditional-expression 
    unary-expression assignment-operator assignment-expression

(as mentioned in some other answers here) are rules used for describing the "grammar" of valid C (or C++). These written-down rules adhere to a certain grammar, too — so I would suggest that you learn that grammar, so that you'll be able to read and understand the rules.

For a start, you could study e.g. the Backus-Naur Form, if you don't already know it. (My link goes to the Wikipedia article on this topic.) While the C++ standard doesn't use Backus-Naur form (IIRC), it's similar enough to get you started.

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