A Python parsing module providing an alternative approach to creating and executing simple grammars
npx @tessl/cli install tessl/pypi-pyparsing@3.2.0A comprehensive Python library that provides an alternative approach to creating and executing simple grammars versus traditional lex/yacc tools or regular expressions. PyParsing enables developers to construct grammar directly in Python code using a library of classes, offering readable grammar representations through self-explanatory class names and operator definitions.
pip install pyparsingimport pyparsingCommon usage pattern:
from pyparsing import Word, Literal, alphas, nums, Suppress, OptionalFor type annotations:
from typing import Union, Optional, Iterable, NamedTuple
from pyparsing import ParserElement, ParseResultsFor complete access to all elements:
from pyparsing import *from pyparsing import Word, alphas, nums, Literal
# Define a simple grammar for "Hello, World!" pattern
greet = Word(alphas) + "," + Word(alphas) + "!"
# Parse a string
hello = "Hello, World!"
result = greet.parse_string(hello)
print(result) # ['Hello', ',', 'World', '!']
# More complex example: parsing simple arithmetic
from pyparsing import Word, nums, oneOf, infixNotation, opAssoc
# Define number and operators
number = Word(nums)
arithmetic_expr = infixNotation(number,
[
(oneOf('* /'), 2, opAssoc.LEFT),
(oneOf('+ -'), 2, opAssoc.LEFT),
])
# Parse arithmetic expressions
expr = "3 + 4 * 2"
result = arithmetic_expr.parseString(expr)
print(result)PyParsing uses a compositional approach where complex grammars are built from simple parsing elements:
This design enables building parsers through composition, making grammars self-documenting and easily maintainable while handling common parsing challenges like whitespace, quoted strings, and comments.
Fundamental building blocks for creating parsing expressions including terminal elements, expression combinators, and structural components that form the foundation of all pyparsing grammars.
class ParserElement: ...
class Literal: ...
class Word: ...
class Regex: ...
class And: ...
class Or: ...
class MatchFirst: ...Modifiers and enhancers that control repetition, optionality, lookahead/lookbehind, and token transformation to create sophisticated parsing behavior from basic elements.
class Optional: ...
class ZeroOrMore: ...
class OneOrMore: ...
class FollowedBy: ...
class NotAny: ...
class Group: ...
class Suppress: ...High-level helper functions for common parsing patterns including delimited lists, nested expressions, HTML/XML parsing, infix notation, and specialized constructs.
def one_of(strs: str) -> MatchFirst: ...
def delimited_list(expr: ParserElement) -> ParserElement: ...
def nested_expr() -> ParserElement: ...
def infix_notation(baseExpr: ParserElement, opList: list) -> ParserElement: ...
def counted_array(expr: ParserElement) -> ParserElement: ...Pre-built parser expressions for frequently used patterns including numeric types, identifiers, network addresses, dates, and parse actions for data conversion.
class pyparsing_common:
integer: ParserElement
real: ParserElement
identifier: ParserElement
ipv4_address: ParserElement
uuid: ParserElement
@staticmethod
def convert_to_integer(): ...
@staticmethod
def convert_to_float(): ...Exception classes for parsing errors with detailed location information and error recovery mechanisms for robust parser development.
class ParseBaseException(Exception): ...
class ParseException(ParseBaseException): ...
class ParseFatalException(ParseException): ...
class RecursiveGrammarException(Exception): ...Testing utilities and debugging tools for parser development including test runners, trace functions, and diagnostic configuration options.
class pyparsing_test:
@staticmethod
def with_line_numbers(s: str) -> str: ...
def trace_parse_action(f: callable) -> callable: ...
def null_debug_action(*args) -> None: ...Unicode character set definitions organized by language/script families.
class pyparsing_unicode:
"""Unicode character sets organized by language/script."""
# Language-specific character sets
Arabic: UnicodeRangeList
Chinese: UnicodeRangeList
Greek: UnicodeRangeList
Hebrew: UnicodeRangeList
Latin1: UnicodeRangeList
# ... many more language/script sets
def unicode_set(s: str) -> str:
"""Create character set from Unicode categories/ranges."""
class UnicodeRangeList:
"""Container for Unicode character ranges."""Global configuration and diagnostic settings for pyparsing behavior.
class __diag__:
"""Diagnostic configuration flags."""
warn_multiple_tokens_in_named_alternation: bool
warn_ungrouped_named_tokens_in_collection: bool
warn_name_set_on_empty_Forward: bool
warn_on_parse_using_empty_Forward: bool
warn_on_assignment_to_Forward: bool
warn_on_multiple_string_args_to_oneof: bool
enable_debug_on_named_expressions: bool
class __compat__:
"""Compatibility configuration flags."""
collect_all_And_tokens: bool
def enable_diag(diag_enum) -> None:
"""Enable specific diagnostic option."""
def disable_diag(diag_enum) -> None:
"""Disable specific diagnostic option."""# Character sets for Word() construction
alphas: str # 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
nums: str # '0123456789'
alphanums: str # alphas + nums
hexnums: str # nums + 'ABCDEFabcdef'
printables: str # All printable ASCII characters
alphas8bit: str # Extended ASCII letters
punc8bit: str # Extended ASCII punctuation
identchars: str # Valid identifier start characters
identbodychars: str # Valid identifier body characters
# Pre-built parser expressions
empty: ParserElement # Always matches, consumes nothing
line_start: ParserElement # Matches at start of line
line_end: ParserElement # Matches at end of line
string_start: ParserElement # Matches at start of string
string_end: ParserElement # Matches at end of string
quoted_string: ParserElement # Any quoted string (single or double)
sgl_quoted_string: ParserElement # Single quoted strings
dbl_quoted_string: ParserElement # Double quoted strings
unicode_string: ParserElement # Unicode string literals__version__: str = "3.2.3"
__version_time__: str = "25 Mar 2025 01:38 UTC"
__author__: str = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>"
class version_info(NamedTuple):
major: int
minor: int
micro: int
releaselevel: str
serial: int
@property
def __version__(self) -> str:
"""Return version string."""
def __str__(self) -> str:
"""Return formatted version info."""
def __repr__(self) -> str:
"""Return detailed version representation."""