What Makes Pyparsing So Special?

Pyparsing was designed with some specific goals in mind. These goals are based on the premise that the grammar must be easy to write, to understand, and to adapt as the parsing demands on a given parser change and expand over time. The intent behind these goals is to simplify the parser design task as much as possible and to allow the pyparsing user to focus his attention on the parser, and not to be distracted by the mechanics of the parsing library or grammar syntax. The rest of this section lists the high points of the Zen of Pyparsing.

The grammar specification should be a natural-looking part of the Python program, easy-to-read, and familiar in style and format to Python programmers

Pyparsing accomplishes this in a couple of ways:

  • Using operators to join parser elements together. Python's support for defining operator functions allows us to go beyond standard object construction syntax, and we can compose parsing expressions that read naturally. Instead of this:

    streetAddress = And( [streetNumber, name,
                               Or( [Literal("Rd."), Literal("St.")] ) ] )

    we can write this:

    streetAddress = streetNumber + name + ( Literal("Rd.") | Literal("St.") )
  • Many attribute setting methods in pyparsing return self so that several of these methods can be chained together. This permits parser elements within a grammar to be more self-contained. For example, a common parser expression is the definition of an integer, including the specification of its name, and the attachment ...

Get Getting Started with Pyparsing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.