Lexer hack

In computer programming, the lexer hack is a solution to parsing context-sensitive grammars 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 parser analyzes sequences of tokens attempting to match them to syntax rules representing language structures, such as loops and variable declarations.

[2] The solution generally consists of feeding information from the semantic symbol table back into the lexer.

[1] This problem does not arise (and hence needs no "hack" in order to solve) when using lexerless parsing techniques, as these are intrinsically contextual.

[4] This is also the approach used in most other modern languages, which do not distinguish different classes of identifiers in the lexical grammar, but instead defer them to the parsing or semantic analysis phase, when sufficient information is available.