teaching machines

Identifiers Starting with Numbers

I was skimming through an introduction to a new programming language and ran across the familiar rule that identifiers in this language must start with a letter or underscore. Because I’ve been feeling a bit subversive lately, I wondered what would happen if this rule went away? What if identifiers could start with a number?

Certainly we’d encounter ambiguity issues with tokens like this:

0xFF -- hexademical 255 as an int? Or a variable name?
500L -- 500 as a long? Or a variable name?
1e3 -- 1000 as an int? Or a variable name?

Then I headed to the Internet to see if there were deeper technical issues. A bunch of folks on Stack Overflow said that initial numbers are outlawed because ambiguities like the ones above would make lexing and parsing more difficult. Because I’ve been feeling a bit subversive lately, I couldn’t agree with them. Many of our lexer generators follow two rules that eliminate ambiguity:

  1. The longest match wins. If we try lexing the string 0xFFZZZ, and we have a rule [0-9]+ for integers and [A-Za-z0-9]+ for identifiers, the identifier rule will win because it can match more of the string.
  2. The first match wins. If we try lexing the string 0xFF, both our integer rule and our identifier rule match. Whichever one is specified first wins. So, as long as we list the pattern for integer literals before the pattern for identifiers, the lexer will classify things like 0xFF as integers. Only tokens that don’t match any previous integer patterns will get classified as identifiers.

As a proof of concept, I wrote up a little ANTLR grammar for Numid, a language that lets you add and subtract numbers and store values in variables whose identifiers may start with a number:

grammar Numid;

  : statement* EOF

  : expression NEWLINE # Print
  | NEWLINE # Blank

  : LEFT_PARENTHESIS expression RIGHT_PARENTHESIS # Parenthesized
  | DECIMAL_INTEGER # DecimalInteger
  | HEXADECIMAL_INTEGER # HexadecimalInteger
  | LONG_INTEGER # LongInteger
  | IDENTIFIER # Identifier
  | expression op=(ADD|SUBTRACT) expression # Additive
  | IDENTIFIER EQUALS expression # Assignment

DECIMAL_INTEGER: '-'? [0-9]+;
LONG_INTEGER: '-'? [0-9]+ 'L';
IDENTIFIER: [0-9A-Za-z_]+;
NEWLINE: '\r'? '\n';
ADD: '+';
EQUALS: '=';
WHITESPACE: [ \t]+ -> skip;

I went ahead and made a little in-browser calculator (with no error checking) so you can test out this language:

Try typing in these terrible but legal expressions:

5LL = 6
5LL - 5L
1l1l1l1 = 100
1l1l1l1 - 1l1l1l1
00xAB = -23

We can verify that 5LL and 0xABCDEFG are indeed identifiers by examining ANTLR’s token stream:


In conclusion, only humans get confused when identifiers start with digits. Lexers and parsers can deal.


  1. True enough. Two of your examples are even legal identifiers in Scheme/Racket. Only 1e3 is a number form.

    > (define 0xFF 1)
    > (define 500L 2)
    > (+ 0xFF 500L 1e3)

Leave a Reply

Your email address will not be published. Required fields are marked *