Re:Pratt parsing is a black box
by nostrademons on Wednesday November 04, 2026 @11:55AM
(#50368344)
At least in imperative languages, a parser generator can execute arbitrary code, including side effects. There's nothing stopping that code from simply returning a data structure; indeed, most productions will have semantic actions that just say "return new Mul($1, $2)". However, the ability to add imperative side effects is what allows things like keeping a comment counter for nested comments; keeping a symbol table in case the same token might be a keyword or variable; handling indent levels in newline-sensitive languages; and so on.
Even in declarative languages like Haskell, your parser combinators will usually operate in a state monad so you can do things like this. True context-free grammars are rare; many real programmer convenience features have some level of context sensitivity, which is usually handled more easily by side effects in the semantic actions than by generalizing to a context-sensitive parser.
Even in declarative languages like Haskell, your parser combinators will usually operate in a state monad so you can do things like this. True context-free grammars are rare; many real programmer convenience features have some level of context sensitivity, which is usually handled more easily by side effects in the semantic actions than by generalizing to a context-sensitive parser.