C++ 为什么变量名不能以数字开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/342152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:46:40  来源:igfitidea点击:

Why can't variable names start with numbers?

c++variablesprogramming-languageslanguage-designvariable-names

提问by Jeremiah

I was working with a new C++ developer a while back when he asked the question: "Why can't variable names start with numbers?"

不久前,当我与一位新的 C++ 开发人员一起工作时,他问了一个问题:“为什么变量名不能以数字开头?”

I couldn't come up with an answer except that some numbers can have text in them (123456L, 123456U) and that wouldn't be possible if the compilers were thinking everything with some amount of alpha characters was a variable name.

我想不出答案,只是有些数字中可以包含文本(123456L、123456U),如果编译器认为所有带有一定数量字母字符的东西都是变量名,那这是不可能的。

Was that the right answer? Are there any more reasons?

那是正确的答案吗?还有其他原因吗?

string 2BeOrNot2Be = "that is the question"; // Why won't this compile?

采纳答案by skiphoppy

Because then a string of digits would be a valid identifier as well as a valid number.

因为那样一串数字将是一个有效的标识符以及一个有效的数字。

int 17 = 497;
int 42 = 6 * 9;
String 1111 = "Totally text";

回答by Pyrolistical

Well think about this:

好好想想这个:

int 2d = 42;
double a = 2d;

What is a? 2.0? or 42?

什么是?2.0?还是42?

Hint, if you don't get it, d after a number means the number before it is a double literal

提示,如果你不明白,数字后面的 d 表示它前面的数字是一个双精度文字

回答by Roy Dictus

It's a convention now, but it started out as a technical requirement.

现在这是一个惯例,但它最初是作为一项技术要求。

In the old days, parsers of languages such as FORTRAN or BASIC did not require the uses of spaces. So, basically, the following are identical:

在过去,诸如 FORTRAN 或 BASIC 之类的语言的解析器不需要使用空格。因此,基本上,以下内容是相同的:

10 V1=100
20 PRINT V1

and

10V1=100
20PRINTV1

Now suppose that numeral prefixes were allowed. How would you interpret this?

现在假设允许使用数字前缀。你会如何解释这个?

101V=100

as

作为

10 1V = 100

or as

或作为

101 V = 100

or as

或作为

1 01V = 100

So, this was made illegal.

所以,这被定为非法。

回答by Jiayang

Because backtracking is avoided in lexical analysis while compiling. A variable like:

因为在编译时在词法分析中避免了回溯。一个变量,如:

Apple;

the compiler will know it's a identifier right away when it meets letter 'A'.

编译器在遇到字母“A”时会立即知道它是一个标识符。

However a variable like:

然而,一个变量,如:

123apple;

compiler won't be able to decide if it's a number or identifier until it hits 'a', and it needs backtracking as a result.

编译器在遇到“a”之前无法确定它是数字还是标识符,因此需要回溯。

回答by Ken Gentle

Compilers/parsers/lexical analyzers was a long, long time ago for me, but I think I remember there being difficulty in unambiguosly determining whether a numeric character in the compilation unit represented a literal or an identifier.

编译器/解析器/词法分析器对我来说是很久以前的事了,但我想我记得很难明确确定编译单元中的数字字符是代表文字还是标识符。

Languages where space is insignificant (like ALGOL and the original FORTRAN if I remember correctly) could not accept numbers to begin identifiers for that reason.

由于这个原因,空间无关紧要的语言(如 ALGOL 和原始 FORTRAN,如果我没记错的话)不能接受数字作为标识符的开头。

This goes way back - before special notations to denote storage or numeric base.

这可以追溯到 - 在表示存储或数字基础的特殊符号之前。

回答by allyourcode

I agree it would be handy to allow identifiers to begin with a digit. One or two people have mentioned that you can get around this restriction by prepending an underscore to your identifier, but that's really ugly.

我同意允许标识符以数字开头会很方便。一两个人提到您可以通过在标识符前加上下划线来绕过此限制,但这真的很难看。

I think part of the problem comes from number literals such as 0xdeadbeef, which make it hard to come up with easy to remember rules for identifiers that can start with a digit. One way to do it might be to allow anything matching [A-Za-z_]+ that is NOT a keyword or number literal. The problem is that it would lead to weird things like 0xdeadpork being allowed, but not 0xdeadbeef. Ultimately, I think we should be fair to all meats :P.

我认为部分问题来自于数字文字,例如 0xdeadbeef,这使得很难为可以以数字开头的标识符提出易于记忆的规则。一种方法可能是允许任何匹配的 [A-Za-z_]+ 不是关键字或数字文字。问题是它会导致一些奇怪的事情,比如允许 0xdeadpork,但不允许 0xdeadbeef。最终,我认为我们应该公平对待所有肉类:P。

When I was first learning C, I remember feeling the rules for variable names were arbitrary and restrictive. Worst of all, they were hard to remember, so I gave up trying to learn them. I just did what felt right, and it worked pretty well. Now that I've learned alot more, it doesn't seem so bad, and I finally got around to learning it right.

当我第一次学习 C 时,我记得感觉变量名的规则是任意和限制性的。最糟糕的是,它们很难记住,所以我放弃了学习它们。我只是做了我认为正确的事情,而且效果很好。现在我学到了很多东西,看起来还不错,我终于开始正确地学习了。

回答by William

It's likely a decision that came for a few reasons, when you're parsing the token you only have to look at the first character to determine if it's an identifier or literal and then send it to the correct function for processing. So that's a performance optimization.

这可能是出于几个原因做出的决定,当您解析令牌时,您只需查看第一个字符以确定它是标识符还是文字,然后将其发送到正确的函数进行处理。所以这是一个性能优化。

The other option would be to check if it's not a literal and leave the domain of identifiers to be the universe minus the literals. But to do this you would have to examine every character of every token to know how to classify it.

另一种选择是检查它是否不是文字,并将标识符域保留为宇宙减去文字。但是要做到这一点,您必须检查每个标记的每个字符才能知道如何对其进行分类。

There is also the stylistic implications identifiers are supposed to be mnemonics so words are much easier to remember than numbers. When a lot of the original languages were being written setting the styles for the next few decades they weren't thinking about substituting "2" for "to".

还有文体含义标识符应该是助记符,因此单词比数字更容易记住。当编写许多原始语言时,他们并没有考虑将“2”替换为“to”,从而为接下来的几十年设置样式。

回答by mkClark

Use of a digit to begin a variable name makes error checking during compilation or interpertation a lot more complicated.

使用数字作为变量名的开头会使编译或解释期间的错误检查变得更加复杂。

Allowing use of variable names that began like a number would probably cause huge problems for the language designers. During source code parsing, whenever a compiler/interpreter encountered a token beginning with a digit where a variable name was expected, it would have to search through a huge, complicated set of rules to determine whether the token was really a variable, or an error. The added complexity added to the language parser may not justify this feature.

允许使用以数字开头的变量名可能会给语言设计者带来巨大的问题。在源代码解析过程中,每当编译器/解释器遇到一个以数字开头的标记,其中需要一个变量名,它必须搜索大量复杂的规则来确定标记是否真的是一个变量,还是一个错误. 添加到语言解析器的复杂性可能无法证明此功能的合理性。

As far back as I can remember (about 40 years), I don't think that I have ever used a language that allowed use of a digit to begin variable names. I'm sure that this was done at least once. Maybe, someone here has actually seen this somewhere.

早在我的记忆中(大约 40 年),我认为我从未使用过允许使用数字作为变量名开头的语言。我敢肯定,这至少做过一次。也许,这里有人真的在某处见过这个。

回答by staticsan

As several people have noticed, there is a lot of historical baggage about valid formats for variable names. And language designers are always influenced by what they know when they create new languages.

正如一些人所注意到的,关于变量名的有效格式有很多历史包袱。语言设计师在创造新语言时总是受到他们所知道的影响。

That said, pretty much all of the time a language doesn't allow variable names to begin with numbers is because those are the rules of the language design. Often it is because such a simple rule makes the parsing and lexing of the language vastly easier. Not all language designers know this is the real reason, though. Modern lexing tools help, because if you tried to define it as permissible, they will give you parsing conflicts.

也就是说,几乎所有时间语言都不允许变量名以数字开头,因为这是语言设计的规则。通常是因为这样一个简单的规则使语言的解析和词法分析变得更加容易。不过,并非所有语言设计师都知道这是真正的原因。现代词法分析工具会有所帮助,因为如果您试图将其定义为允许的,它们会给您解析冲突。

OTOH, if your language has a uniquely identifiable character to herald variable names, it is possible to set it up for them to begin with a number. Similar rule variations can also be used to allow spaces in variable names. But the resulting language is likely to not to resemble any popular conventional language very much, if at all.

OTOH,如果您的语言具有唯一可识别的字符来表示变量名称,则可以将其设置为以数字开头。类似的规则变体也可用于在变量名称中允许空格。但最终的语言很可能与任何流行的传统语言不太相似,如果有的话。

For an example of a fairly simple HTML templating language that does permit variables to begin with numbers and have embedded spaces, look at Qompose.

有关允许变量以数字开头并嵌入空格的相当简单的 HTML 模板语言的示例,请查看Qompose

回答by Kyle Jones

The restriction is arbitrary. Various Lisps permit symbol names to begin with numerals.

限制是任意的。各种 Lisps 允许符号名称以数字开头。