Lexical analysis

Lexical tokenization is conversion of a text into (semantically or syntactically) meaningful lexical tokens belonging to categories defined by a "lexer" program.

Uses your system voice; voice and rate in settings.

≈ 4 min read

Cite, export and learn8 citation formats · 8 exports · lesson and quiz

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

Lexical tokenization is conversion of a text into (semantically or syntactically) meaningful lexical tokens belonging to categories defined by a "lexer" program. In case of a natural language, those categories include nouns, verbs, adjectives, punctuations etc. In case of a programming language, the categories include identifiers, operators, grouping symbols, data types and language keywords.

Lexical tokenization is related to the type of tokenization used in large language models (LLMs) but with two differences. First, lexical tokenization is usually based on a lexical grammar, whereas LLM tokenizers are usually probability-based. Second, LLM tokenizers perform a second step that converts the tokens into numerical values.

Lexical token and lexical tokenization

A lexical token is a string with an assigned and thus identified meaning, in contrast to the probabilistic token used in large language models. A lexical token consists of a token name and an optional token value. The token name is a category of a rule-based lexical unit.

Consider this expression in the C programming language: The lexical analysis of this expression yields the following sequence of tokens: A token name is what might be termed a part of speech in linguistics. Lexical tokenization is the conversion of a raw text into (semantically or syntactically) meaningful lexical tokens, belonging to categories defined by a "lexer" program, such as identifiers, operators, grouping symbols, and data types.

The resulting tokens are then passed on to some other form of processing. The process can be considered a sub-task of parsing input. For example, in the text string: The quick brown fox jumps over the lazy dog the string is not implicitly segmented on spaces, as a natural language speaker would do.

The raw input, the 43 characters, must be explicitly split into the 9 tokens with a given space delimiter (i.e., matching the string " " or regular expression ). When a token class represents more than one possible lexeme, the lexer often saves enough information to reproduce the original lexeme, so that it can be used in semantic analysis.

The parser typically retrieves this information from the lexer and stores it in the abstract syntax tree. This is necessary in order to avoid information loss in the case where numbers may also be valid identifiers. Tokens are identified based on the specific rules of the lexer.

Some methods used to identify tokens include regular expressions, specific sequences of characters termed a flag, specific separating characters called delimiters, and explicit definition by a dictionary. Special characters, including punctuation characters, are commonly used by lexers to identify tokens because of their natural use in written and programming languages.

A lexical analyzer generally does nothing with combinations of tokens, a task left for a parser. For example, a typical lexical analyzer recognizes parentheses as tokens but does nothing to ensure that each "(" is matched with a ")". When a lexer feeds tokens to the parser, the representation used is typically an enumerated type, which is a list of number representations.

For example, "Identifier" can be represented with 0, "Assignment operator" with 1, "Addition operator" with 2, etc. Tokens are often defined by regular expressions, which are understood by a lexical analyzer generator such as lex, or handcoded equivalent finite-state automata. The lexical analyzer (generated automatically by a tool like lex or hand-crafted) reads in a stream of characters, identifies the lexemes in the stream, and categorizes them into tokens.

This is termed tokenizing. If the lexer finds an invalid token, it will report an error. Following tokenizing is parsing.

From there, the interpreted data may be loaded into data structures for general use, interpretation, or compiling. Algorithms + Data Structures = Programs, Niklaus Wirth, 1975, Compiler Construction, Niklaus Wirth, 1996, Sebesta, R. W. (2006). Concepts of programming languages (Seventh edition) pp. 177.

Boston: Pearson/Addison-Wesley.

Text derived from https://en.wikipedia.org/w/index.php?oldid=1370769639 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001370769639 · captured 2026-08-22 · ledger · version history

Cite this page as: encyclopegia.org, “Lexical analysis”, snapshot enwiki-001370769639. https://encyclopegia.org/a/lexical-analysis

Sources cited in this revision

9 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.