Amazing post! A little mind bending but worth to try and learn something.
––––
Staged Parser Combinators in Scala: Have Your Cake and Eat It (Too)
https://moleike.github.io/blog/staged-parser-combinators/
#Programming #Scala #plt #FunctionalProgramming #FP #parserCombinators
Staged Parser Combinators in Scala: Have Your Cake and Eat It (Too)
Parser combinators have a reputation of poor performance—worst-case exponential time—and deemed good for prototyping but not for production. Or so the story goes. While techniques like Packrat parsing or static analysis offer linear-time guarantees by addressing naive backtracking, they come with their own trade-offs. In this post, I want to explore an optimization route that complements the others: avoiding the performance penalty of abstraction. By leaning into Scala 3’s metaprogramming capabilities, our combinators combine code fragments. By moving to a staged continuation-passing style (CPS) encoding, we make the control flow explicit and continuations are fully evaluated at compile time. Results show that a staged parser runs ~50% faster than a handwritten recursive descent parser and over 25x faster than a non-staged version. Interestingly, this approach is deeply rooted in partial evaluation history, closely mirroring the First Futamura Projection.
🎄