pyparsing

Build recursive-descent parsers in Python without a separate grammar file

Library
PyPI
v3.3.3
2,497 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
83 /100 Excellent
Development Activity 88
Maintenance 76
Community 68
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
79 /100 Good
Architecture 80
Code Quality 82
Innovation 78
Learning Curve 75

pyparsing lets you build parsers for structured or semi-structured text entirely in Python, by combining small parser-element objects (literals, words, numbers, quoted strings) with operators like + (sequence), | (first match), and ^ (longest match) into a grammar object that parses directly. There’s no separate grammar DSL, code-generation step, or external parser-generator tool to run — the grammar is ordinary Python code, which makes it easy to debug, extend, and combine with the rest of an application.

It predates most modern PEG-parser libraries in the Python ecosystem and remains widely used for config-file formats, small DSLs, log-line extraction, and search-query grammars where a full parser-generator toolchain would be overkill.

What You Get

  • A library of composable parser-element primitives (Word, Literal, Regex, QuotedString, Combine, and more) usable directly as Python objects
  • Operator-based grammar composition (+ for sequence, | for first-match-wins, ^ for longest-match, ~ for negation)
  • Parse actions - attach Python callables to grammar elements to transform or validate matched tokens during parsing
  • ParseResults objects supporting both positional and named-field access to parsed tokens, plus nested/grouped results
  • A pyparsing.diagram module for generating railroad-diagram visualizations of a grammar, plus a common module of ready-made tokens (numbers, identifiers, comments)

Common Use Cases

  • Parsing small domain-specific languages or config-file formats without introducing a separate grammar-generator build step
  • Extracting structured fields from semi-structured log lines or command output
  • Building custom search-query or filter-expression grammars for an application’s search bar
  • Validating and transforming input during parsing itself via attached parse actions, rather than in a separate post-processing pass

Under The Hood

Architecture - core.py implements the parser-combinator engine: every grammar element is a ParserElement subclass with a parseImpl method, and composite elements (And, Or, MatchFirst, Each) recursively call into their children’s parseImpl during a scan over the input string, with results.py’s ParseResults accumulating matched tokens (including named results set via .set_results_name()) as parsing proceeds; actions.py implements the parse-action callback mechanism that lets grammar authors hook into successful matches.

Tech Stack - Pure Python with no runtime dependencies, distributed with inline type stubs (py.typed) for static type checkers; the pyparsing/tools and pyparsing/diagram subpackages add optional railroad-diagram grammar visualization (built on railroad-diagrams) as an extra rather than a hard dependency, keeping the core install lightweight.

Code Quality - The library ships an explicit testing.py module with TestParseResultsAsserts-style helpers specifically to make grammars written with pyparsing easier for consumers to unit-test, on top of its own large internal test suite; exceptions.py and warnings.py provide structured parse-error reporting with line/column context rather than generic Python exceptions, which matters a lot for a parsing library’s usability.

API Design - Composing grammars via Python operator overloading (Word(alphas) + '=' + Word(nums)) is the library’s signature ergonomic choice — grammars read close to the structure they describe and stay debuggable with ordinary Python tooling (breakpoints, print statements), at some cost of operator-precedence surprises that the docs and cookbook examples work to head off.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers