Lexer hack
In computer programming, the lexer hack is a solution to parsing a context-sensitive grammar such as C, where classifying a sequence of characters as a variable name or a type name requires contextual information, by feeding contextual information backwards from the parser to the lexer. The lexer hack is frowned upon in modern compilers as it creates tight coupling between otherwise largely independent steps in the compilation process. Instead, identifier-like tokens are tokenized as identifiers and later disambiguated by the parser, allowing cleaner separation of concerns.
In computer programming, the lexer hack is a solution to parsing a context-sensitive grammar such as C, where classifying a sequence of characters as a variable name or a type name requires contextual information, by feeding contextual information backwards from the parser to the lexer. The lexer hack is frowned upon in modern compilers as it creates tight coupling between otherwise largely independent steps in the compilation process. Instead, identifier-like tokens are tokenized as identifiers and later disambiguated by the parser, allowing cleaner separation of concerns. A * B; This code could either be multiplication or a declaration, depending on context. In more detail, in a compiler, the lexer performs one of the earliest stages of converting the source code to a program. It scans the text to extract meaningful tokens, such as words, numbers, and strings. The parser analyzes sequences of tokens attempting to match them to syntax rules representing language structures, such as loops and variable declarations. A problem occurs here if a single sequence of tokens can ambiguously match more than one syntax rule. This ambiguity can happen in C if the lexer does not distinguish between variable and typedef identifiers. For example, in the C expression: A * B; the lexer may find these tokens: ?? 'A' operator '*' identifier 'B' punctuation ';' Depending on whether A is a typedef name or not it may be desirable to tokenize A as either an identifier or a type so the parser does not have to handle an ambiguous parse. This grammatical ambiguity is known as the "typedef-name: identifier" problem, due to the name of the problematic production rule. http://groups.google.com/group/comp.compilers/browse_frm/thread/db7f68e9d8b49002/fa20bf5de9c73472?lnk=st&q=%2B%22the+lexer+hack%22&rnum=1&hl=en#fa20bf5de9c73472
References
No reference rows published for this article.
Cite this article
MLA
"Lexer hack." encyclopegia.org, 2026, https://encyclopegia.org/wiki/lexer-hack. Accessed 24 November 2026.
Chicago
"Lexer hack." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/lexer-hack.
APA
encyclopegia.org. (2026). Lexer hack. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/lexer-hack
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON