

In languages that support reflection, such as interactive evaluation of source code (using an interpreter or an incremental compiler), identifiers are also runtime entities, sometimes even as first-class objects that can be freely manipulated and evaluated. That is, at runtime the compiled program contains references to memory addresses and offsets rather than the textual identifier tokens (these memory addresses, or offsets, having been assigned by the compiler to each identifier). įor implementations of programming languages that are using a compiler, identifiers are often only compile time entities. A local identifier is declared within a specific function and only available within that function. A global identifier is declared outside of functions and is available throughout the program. The scope, or accessibility within a program of an identifier can be either local or global. In a few languages, e.g., PL/1, the distinction is not clear. Non-keywords may also be reserved words (forbidden as identifiers), particularly for forward compatibility, in case a word may become a keyword in future. This overlap can be handled in various ways: these may be forbidden from being identifiers – which simplifies tokenization and parsing – in which case they are reserved words they may both be allowed but distinguished in other ways, such as via stropping or keyword sequences may be allowed as identifiers and which sense is determined from context, which requires a context-sensitive lexer. In most languages, some character sequences have the lexical form of an identifier but are known as keywords – for example, if is frequently a keyword for an if clause, but lexically is of the same form as ig or foo namely a sequence of letters. In ALGOL this was possible because keywords are syntactically differentiated, so there is no risk of collision or ambiguity, spaces are eliminated during the line reconstruction phase, and the source was processed via scannerless parsing, so lexing could be context-sensitive. half pi (keywords are represented in boldface, concretely via stropping). Some languages do allow spaces in identifiers, however, such as ALGOL 68 and some ALGOL variants – for example, the following is a valid statement: real half pi which could be entered as.


Whitespace in identifier is particularly problematic, as if spaces are allowed in identifiers, then a clause such as if rainy day then 1 is legal, with rainy day as an identifier, but tokenizing this requires the phrasal context of being in the condition of an if clause.

For example, forbidding + in identifiers due to its use as a binary operation means that a+b and a + b can be tokenized the same, while if it were allowed, a+b would be an identifier, not an addition. However, a common restriction is not to permit whitespace characters and language operators this simplifies tokenization by making it free-form and context-free. Later versions of these languages, along with many other modern languages, support many more Unicode characters in an identifier. A common rule is alphanumeric sequences, with underscore also allowed (in some languages, _ is not allowed), and with the condition that it can not begin with a numerical digit (to simplify lexing by avoiding confusing with integer literals) – so foo, foo1, foo_bar, _foo are allowed, but 1foo is not – this is the definition used in earlier versions of C and C++, Python, and many other languages. Which character sequences constitute identifiers depends on the lexical grammar of the language.
