Parsimonious
A fast, pure-Python parsing expression grammar (PEG) parser library.
Repository Health
Technical Analysis
Parsimonious is a pure-Python parser built on parsing expression grammars (PEGs) — you define a grammar in a simplified EBNF-like syntax, and it parses text into a tree you can walk with a visitor class. It was originally built to undergird a MediaWiki parser that needed to avoid the multi-second, gigabyte-RAM behavior of alternatives, so speed and low memory use are explicit design goals.
The library deliberately separates recognition (parsing) from interpretation (turning the parse tree into something else, like HTML or plain text), which lets the same grammar drive multiple different output transformations via separate NodeVisitor subclasses.
What You Get
- A
Grammar()class that compiles a PEG rule set (in EBNF-like syntax) into a parser - A
NodeVisitorbase class for cleanly separating grammar definition from tree interpretation - Arbitrary-lookahead PEG parsing with reasonable speed and low memory overhead for pure Python
- Clear error reporting that points at the exact rule and position where parsing failed
- A small, readable core codebase (~1,500 lines) with complete test coverage
Common Use Cases
- Building a custom domain-specific language (DSL) parser for config files, query languages, or templating syntax
- Parsing structured but non-standard text formats (wiki markup, log formats, custom protocols) into a tree
- Writing linters or static analysis tools that need a lightweight, dependency-free grammar parser
- Prototyping a language or grammar quickly before committing to a heavier parser-generator toolchain
Under The Hood
Architecture: grammar.py compiles a textual PEG rule set into a tree of Expression objects (defined in expressions.py — literals, sequences, choices, lookaheads, repetitions), which nodes.py’s NodeVisitor then walks post-parse to transform the tree; exceptions.py provides precise, position-aware parse error reporting. Tech Stack: pure Python with zero runtime dependencies, packaged via setup.py, keeping the entire implementation to roughly 1,500 lines across six modules. Code Quality: the project states “complete test coverage” as an explicit goal, with a dedicated parsimonious/tests package; the codebase is small enough to read end-to-end, and the README documents design goals (speed, frugal RAM, minimalism, readability, extensibility, good error reporting) that the implementation is visibly built around. API Design: the core workflow — define a Grammar(), subclass NodeVisitor, call .parse() — is minimal and consistent, and the explicit separation of parsing from tree interpretation makes it straightforward to reuse one grammar for multiple output targets.