Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Monadic Parsing in Python
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Oleksii Kachaiev
June 06, 2014
Programming
13
7.5k
Monadic Parsing in Python
Oleksii Kachaiev
June 06, 2014
Tweet
Share
More Decks by Oleksii Kachaiev
See All by Oleksii Kachaiev
Counting HTTP with QUIC & HTTP/3
kachayev
2
300
Talking SQL to Strangers
kachayev
3
610
Counting HTTP: 0.9...3
kachayev
1
120
Managing Data Chaos in The World of Microservices
kachayev
3
680
Deep HTTP Dive Through Aleph & Netty
kachayev
6
4k
Keep Your Data Safe With Refined Types
kachayev
4
1.5k
Clojure at Attendify (2nd ed)
kachayev
5
1.7k
Clojure at Attendify
kachayev
4
380
Finagle & Clojure
kachayev
6
1.4k
Other Decks in Programming
See All in Programming
go directiveを最新にしすぎないで欲しい話──あるいは、Go 1.26からgo mod initで作られるgo directiveの値が変わる話 / Go 1.26 リリースパーティ
arthur1
2
560
AWS×クラウドネイティブソフトウェア設計 / AWS x Cloud-Native Software Design
nrslib
16
3.2k
Docコメントで始める簡単ガードレール
keisukeikeda
1
120
AI駆動開発の本音 〜Claude Code並列開発で見えたエンジニアの新しい役割〜
hisuzuya
4
510
クライアントワークでSREをするということ。あるいは事業会社におけるSREと同じこと・違うこと
nnaka2992
1
340
Everything Claude Code OSS詳細 — 5層構造の中身と導入方法
targe
0
120
encoding/json/v2のUnmarshalはこう変わった:内部実装で見る設計改善
kurakura0916
0
420
AIに任せる範囲を安全に広げるためにやっていること
fukucheee
0
130
Cyrius ーLinux非依存にコンテナをネイティブ実行する専用OSー
n4mlz
0
150
Agentic AI: Evolution oder Revolution
mobilelarson
PRO
0
180
エンジニアの「手元の自動化」を加速するn8n 2026.02.27
symy2co
0
160
Go 1.26でのsliceのメモリアロケーション最適化 / Go 1.26 リリースパーティ #go126party
mazrean
1
410
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
96
14k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The Curse of the Amulet
leimatthew05
1
10k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
120
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
210
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
220
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
Building an army of robots
kneath
306
46k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
140
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Transcript
Monadic Parsing in Python Alexey Kachayev, 2014
About me • CTO at Attendify.com • Erlang, Clojure, Go,
Haskell • Fn.py library author • CPython & Storm contributor
Find me •@kachayev •github.com/kachayev •kachayev <$> gmail.com
Topic
Will talk •What is "parsing(ers)" •Approaches •Monadic parsing from scratch
•More…
Will talk •Less about theory •Much more about practice
Won’t talk •What "monad" is •Why FP is cool (*)
* you’ll understand it by yourself
Parsing
Definition •Takes grammar •Takes input string (?) •Returns tree (??)
or an error
None
For PL creators only?
Tasks • Processing information from logs • Source code analysing
• DSLs • Protocols & data formats • … and more
Approaches
Production rule S → SS|(S)|()
Grammar block = ["const" ident "=" number {"," ident "="
number} ";"] ["var" ident {"," ident} ";"] {"procedure" ident ";" block ";"} statement ! expression = ["+"|"-"] term {("+"|"-") term} ! term = factor {("*"|"/") factor} ! factor = ident | number | "(" expression ")" ! . . . .
•Top-down / bottom-up •Predictive / Backtracking •LL(k), LALR, LR, CYK
and others In theory
Manually!
@ wikipedia
Manually •Simple to understand •Hard to maintain •Really boring
Can we do better?
What we have •Context-free grammars •Formal theory •Well-defined algorithms •Standard
grammar notation(s)
So…
Parser generator •1. Parse DSL notation •2. Generate parser code
•("any" language)
Parser generator •*PEG* •*Yacc* •ANTLR •… and tens more
Parser generator •Pros •many targeted languages •formalism •performance & optimisations
Parser generator •Cons •another language •bounded in features •"compiled-time" mostly
Can we do better?
Monadic parsers & combinators
Functional Pearls Monadic Parsing in Haskell @Graham Hutton, @Erik Meijer
Parsec MPC library for Haskell
Parsec •Monadic parser combinator(s) •Works even with context- sensitive, infinite
LA grammars •Tens of ports to other langs
None
The Big Idea
Simple type Parser = String → Tree
Compose? type Parser = String → (Tree, String)
Generalize? type Parser a = String → (a, String)
Errors? type Parser a = String → Maybe (a, String)
Or better… type Parser a = String → [(a, String)]
Let’s try…
Snippets: http://goo.gl/leQIEE
None
None
None
None
… and so?
Expressiveness •[] for error •[s1] for single (predictive) •[s1..sN] for
backtracking
First-class citizen
None
Skip anything…
Recognise digit
Combinators
RegExp •and: "abc" •or: "a | b | c" •Kleene
star: "a*"
Derives •a? = "" | a •a+ = aa* •a{2,3}
= aa | aaa
None
None
laziness is cool for this do you need backtracking?
How to use it?
None
None
Cool! but..
ugly ugly not readable
Enhancements •use generators for "laziness" •"combine" function •Scala-style methods •"delay"
method
fn.py Stream
None
[1,2,3,4,5] expr →"[" digit (","digit)* "]"
None
Interesting! but..
Is it enough?
In Haskell
Can I do this in Python?
… hm
Challenge accepted!
In Python
How?
Desugaring…
What?
WAT??? even more like
unit a → Parser a
bind Parser a → (a → Parser b) → Parser
b
lift (a → b) → (a → Parser b)
lifted Parser a → (a → b) → Parser b
WAT??? ok, looks cool, but
None
None
How to use
And even more..
Haskell-style
Do-notation
None
None
(define R 2) (define diameter (lambda (r) (* 2 r)))
None
None
Looks nice!
Mutability kills backtracking :(
And more •errors handling •backtracking control •performance
Links • "funcparselib" http://goo.gl/daidQY • "Monadic parsing in Haskell" http://goo.gl/gygNlM
• "Higher-Order functions for Parsing" http://goo.gl/c8VOIZ • "Parsec" http://goo.gl/bdnDZQ • "Parcon" http://goo.gl/CT06S5 • "Pyparsing" http://goo.gl/gmr2lQ • "You Could Have Invented Monadic Parsing" http://goo.gl/h0rnOQ
Learn Haskell For Great Good
Q/A thanks for your attention,