or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

common-expressions.mdcore-elements.mdenhancement.mdexceptions.mdhelpers.mdindex.mdtesting-debugging.md
tile.json

tessl/pypi-pyparsing

A Python parsing module providing an alternative approach to creating and executing simple grammars

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pyparsing@3.2.x

To install, run

npx @tessl/cli install tessl/pypi-pyparsing@3.2.0

index.mddocs/

PyParsing

A 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.

Package Information

  • Package Name: pyparsing
  • Language: Python
  • Installation: pip install pyparsing

Core Imports

import pyparsing

Common usage pattern:

from pyparsing import Word, Literal, alphas, nums, Suppress, Optional

For type annotations:

from typing import Union, Optional, Iterable, NamedTuple
from pyparsing import ParserElement, ParseResults

For complete access to all elements:

from pyparsing import *

Basic Usage

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)

Architecture

PyParsing uses a compositional approach where complex grammars are built from simple parsing elements:

  • ParserElement: Base class for all parsing components
  • Terminal Elements: Match specific text patterns (Literal, Word, Regex)
  • Expression Classes: Combine other elements (And, Or, MatchFirst, Each)
  • Enhancement Classes: Modify parsing behavior (Optional, ZeroOrMore, OneOrMore)
  • Token Converters: Transform results (Group, Suppress, Combine)
  • ParseResults: Container for parsed data with list/dict/object access patterns

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.

Capabilities

Core Parser Elements

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: ...

Core Elements

Expression Enhancement

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: ...

Enhancement

Helper Functions and Utilities

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: ...

Helpers

Common Parser Expressions

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(): ...

Common Expressions

Exception Handling

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): ...

Exceptions

Testing and Debugging

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: ...

Testing & Debugging

Unicode Support

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."""

Configuration

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."""

Built-in Constants

# 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 Information

__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."""