mo-parsing

8.694.25301 · active · verified Thu Apr 16

mo-parsing is a Python PEG (Parsing Expression Grammar) parsing tool. It is a fork of `pyparsing`, designed for faster parsing, and allows users to define parsers using predefined patterns and Python operators. The library is currently at version 8.694.25301, with its latest release uploaded on October 27, 2025 (likely 2024, indicating active development with frequent updates).

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a simple grammar using `Word` and `Literal`, managing whitespace with `Whitespace` context, and parsing a string. It shows how to access the parsed results as both a list and a dictionary.

from mo_parsing import Word, Literal
from mo_parsing.utils import alphas
from mo_parsing.whitespaces import Whitespace

# Define a simple grammar for 'Hello, World!' with a custom whitespace rule
with Whitespace(' \t') as whitespace_context:
    greet = Word(alphas)('greeting') + Literal(',') + Word(alphas)('person') + Literal('!')

    # Parse a string
    result = greet.parse_string('Hello, World!')

    # Access results as a list
    print(f"List view: {list(result)}")
    # Access results as a dictionary (for named tokens)
    print(f"Dict view: {dict(result)}")

    # Example with different input
    result_named = greet.parse_string('Hi, There!')
    print(f"Named tokens: {result_named.greeting}, {result_named.person}")

view raw JSON →