为什么X ++ - + - ++ x法律而表达式x +++ - +++ x不是? [英] Why is x++-+-++x legal but x+++-+++x isn't?

查看:218
本文介绍了为什么X ++ - + - ++ x法律而表达式x +++ - +++ x不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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

这篇关于为什么X ++ - + - ++ x法律而表达式x +++ - +++ x不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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