1

In many languages (C#, Javascript, CSS, and so on) I can't declare this variable:

123test

but I can declare this:

test123

What's the real cause of it?

markzzz
  • 44,504
  • 107
  • 265
  • 458
  • 1
    Duplicate of [http://stackoverflow.com/questions/342152/why-cant-variable-names-start-with-numbers](http://stackoverflow.com/questions/342152/why-cant-variable-names-start-with-numbers) – John Odom Feb 11 '14 at 15:28

2 Answers2

2

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";

@skiphoppy`s answear.

More information: Why can't variable names start with numbers?

Community
  • 1
  • 1
Rocketq
  • 4,388
  • 12
  • 59
  • 111
2

It simplifies the parser. It can tell from the first character in a token whether it's an identifier or a number.

Also, the syntax for floating point can look like this:

123e45

This means 123x1045. If identifiers could start with a number, this could be confused with a variable.

There are some languages that don't have this prohibition, Common Lisp for instance. It's rule is essentially that the token is a symbol unless it can be parsed as a number. Since it also allows the input radix to be customized, it has the property that whether a token is a number or symbol depends on the setting of a variable (it also has escaping mechanisms that allow you to force it one way or the other).

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • It basically simplifies lexical analysis if you don't have to account for a number (pun intended) of special cases. You _could_ see if digits (possibly a number) are followed by a letter with no intervening operator, but then you have to figure out from the context whether 1111 or 123e45 are (previously declared) variable names or numeric literals. It can get quite messy to push back token recognition from lexical to syntactic or even semantic analysis, so it's easier just to restrict the format of variable names. – Phil Perry Feb 11 '14 at 15:49