encyclopegia.org

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

Related articles

Shelley Hack

stub · references available

Wilhelm-Hack-Museum

stub · references available

Daniel Hack Tuke

stub · references available

Hack's law

stub · references available

Ernst Hack

stub · references available

Reinhold Lexer

stub · references available

Seo Jung-Hack

stub · references available

2012 LinkedIn hack

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1366066717 · 0.5 · snapshot enwiki-001366066717

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.