pyparsing

pyparsing: Build Text Parsers in Python

pyparsing is a Python library for defining parsing-expression grammars directly in code. Use it to turn structured text into accessible parse results without writing a separate grammar file or relying only on regular expressions.

Stars
2.5k
Forks
350
Stars, last 8 days
+0

Our take

Health score

44/100

How it is scored
Activity
58
Community
53
Issues
35
Pull requests
31

+0 stars in the last 8 days

  • Slower to close issues than 99% of the projects we track

Overview

pyparsing is a Python library for building parsers by composing grammar elements in Python code. It provides an alternative to traditional lex/yacc workflows and regular expressions for recognizing structured text.

It is suited to developers who need a readable, customizable parser for a text format. Parsing results can be accessed as nested lists, dictionaries, or objects with named attributes, and the library accounts for common input variations such as whitespace and quoted strings.

Key Features

  • Define parsing-expression grammars using Python classes and operators.
  • Parse text with parse_string() and receive ParseResults.
  • Access results as nested lists, dictionaries, or named attributes.
  • Handle variations in whitespace without spelling out every spacing pattern.
  • Support quoted strings and embedded comments in parsing grammars.
  • Explore examples including parsers for SQL, configuration files, chemical formulas, and algebraic notation.
  • Find usage guidance in online documentation and the project wiki.

Use Cases

  • A Python developer can parse a configuration file when its structure needs more precise handling than simple string operations.
  • A maintainer can build a parser for a small domain-specific language, such as algebraic expressions, directly in application code.
  • A developer working with structured text can recognize quoted values, comments, and flexible whitespace in one grammar.
  • Someone learning parser construction can study the included examples and compose grammar elements incrementally.

Getting Started

Start with the project README and online documentation for usage details and examples.

Alternatives

  • sqlparse: sqlparse handles SQL specifically, splitting and inspecting token trees without validating syntax, while pyparsing lets you define grammars for many text formats.
ProjectLanguageLicenseStarsStatus
pyparsingPythonMIT2.5kNot checked yet
sqlparsePythonBSD-3-Clause4kActive

Considerations

  • Grammar definitions are written in Python, so using the library requires working in a Python project.
  • The repository describes pyparsing as an approach for simple grammars; assess whether it fits the complexity and performance needs of your particular input format.
  • The README points to a benchmark suite in tests/README.md, but provides no benchmark results to compare here.

Found this useful?

Share it with someone who would like pyparsing.

Comparisons