Arpeggio Parser

2.0.3 · active · verified Thu Apr 09

Arpeggio is a Python library that provides a Packrat parser interpreter. It allows you to define grammars using Python functions or EBNF-like strings and then parse input text according to those grammars. The current version is 2.0.3, with releases focusing on bug fixes, performance, and modern Python compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a simple arithmetic grammar, parsing an expression, and then using a `PTNodeVisitor` to traverse and evaluate the resulting parse tree.

from arpeggio import ParserPython, visit_parse_tree
from arpeggio import PTNodeVisitor

# 1. Define your grammar using Python functions or a string
def calculator_grammar():
    return r"""
        calc = number (("+"|"-") number)* ;
        number = /\d+/ ;
    """

# 2. Create a parser instance
# For grammars defined as strings, use Parser(grammar_string)
# For grammars defined as Python functions, use ParserPython(grammar_function)
parser = ParserPython(calculator_grammar)

# 3. Parse input text
input_expr = "10 + 20 - 5"
parse_tree = parser.parse(input_expr)

# 4. (Optional) Process the parse tree using a visitor
class CalculatorVisitor(PTNodeVisitor):
    def visit_number(self, node):
        return int(node.value)

    def visit_calc(self, node):
        # The parse tree node will contain parsed elements as children
        res = node[0] # first number
        for i in range(1, len(node), 2):
            op = node[i].value
            num = node[i+1]
            if op == '+':
                res += num
            elif op == '-':
                res -= num
        return res

result = visit_parse_tree(parse_tree, CalculatorVisitor())

assert result == 25
# print(f"Input: '{input_expr}', Result: {result}")

view raw JSON →