为什么X ++ - + - ++ x法律而表达式x +++ - +++ x不是?表达式、不是、法律

2023-09-03 15:51:06 作者:づ男人要霸气

我在C#下面疑惑,为什么是好的:

I'm wondering why in C# the following is fine:

int y = x++-+-++x;

int y = x+++-+++x;

是不是?为什么会出现反对偏见+?

Isn't? Why is there a bias against the +?

推荐答案

另外两个答案都是正确的;我将增加他们,这说明了词法分析的一些基本原则:

The other two answers are correct; I will add to them that this illustrates some basic principles of lexical analysis:

词法分析器的短视的 - 它具有最小的前瞻 词法分析器的贪婪的 - 它试图使最长的令牌它可以的现在的。 在词法分析器不会回溯尝试当一个人无法找到替代解决方案。 The lexical analyzer is short-sighted -- it has minimal "look-ahead" The lexical analyzer is greedy -- it tries to make the longest token it can right now. The lexical analyzer does not backtrack trying to find alternate solutions when one fails.

这些原则意味着 +++ X 将lexed为 ++ + X ,而不是 + + + X

These principles imply that +++x will be lexed as ++ + x and not + ++ x.

解析器将然后解析 ++ + X ++(+ X)(+ x)​​的不是可变的,它是一种的值的,因此它不能被递增。

The parser will then parse ++ + x as ++(+x), and (+x) is not a variable, it is a value, so it cannot be incremented.

另请参阅: HTTP://blogs.msdn .COM / B / ericlippert /存档/ 2010/10/11 / 10070831.aspx

 
精彩推荐
图片推荐