{"id":1909,"library":"arpeggio","title":"Arpeggio Parser","description":"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.","status":"active","version":"2.0.3","language":"en","source_language":"en","source_url":"https://github.com/textX/Arpeggio","tags":["parser","parsing","grammar","packrat","text-processing"],"install":[{"cmd":"pip install arpeggio","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"ParserPython","correct":"from arpeggio import ParserPython"},{"symbol":"Parser","correct":"from arpeggio import Parser"},{"symbol":"PTNodeVisitor","correct":"from arpeggio import PTNodeVisitor"},{"symbol":"visit_parse_tree","correct":"from arpeggio import visit_parse_tree"}],"quickstart":{"code":"from arpeggio import ParserPython, visit_parse_tree\nfrom arpeggio import PTNodeVisitor\n\n# 1. Define your grammar using Python functions or a string\ndef calculator_grammar():\n    return r\"\"\"\n        calc = number ((\"+\"|\"-\") number)* ;\n        number = /\\d+/ ;\n    \"\"\"\n\n# 2. Create a parser instance\n# For grammars defined as strings, use Parser(grammar_string)\n# For grammars defined as Python functions, use ParserPython(grammar_function)\nparser = ParserPython(calculator_grammar)\n\n# 3. Parse input text\ninput_expr = \"10 + 20 - 5\"\nparse_tree = parser.parse(input_expr)\n\n# 4. (Optional) Process the parse tree using a visitor\nclass CalculatorVisitor(PTNodeVisitor):\n    def visit_number(self, node):\n        return int(node.value)\n\n    def visit_calc(self, node):\n        # The parse tree node will contain parsed elements as children\n        res = node[0] # first number\n        for i in range(1, len(node), 2):\n            op = node[i].value\n            num = node[i+1]\n            if op == '+':\n                res += num\n            elif op == '-':\n                res -= num\n        return res\n\nresult = visit_parse_tree(parse_tree, CalculatorVisitor())\n\nassert result == 25\n# print(f\"Input: '{input_expr}', Result: {result}\")","lang":"python","description":"This quickstart demonstrates defining a simple arithmetic grammar, parsing an expression, and then using a `PTNodeVisitor` to traverse and evaluate the resulting parse tree."},"warnings":[{"fix":"Upgrade your Python environment to 3.6 or newer. If you need older Python support, use Arpeggio < 2.0.0 (e.g., 1.x).","message":"Arpeggio 2.0.0 and later dropped support for Python 2.x and Python 3.x up to 3.5. The lowest supported Python version is now 3.6.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always check for the presence of a rule or use iteration/indexing (`node[idx]`) if uncertain about rule names. Refer to the documentation on parse tree structure for correct navigation.","message":"Accessing a non-existent rule name as an attribute on a parse tree node (e.g., `parse_tree.non_existent_rule`) will raise an `AttributeError`.","severity":"gotcha","affected_versions":">=1.10.0"},{"fix":"When debugging parsing issues, ensure you are on Arpeggio >= 2.0.0 and consult the documentation on error handling to fully leverage the detailed `NoMatch` exception information.","message":"Error reporting for `NoMatch` exceptions was enhanced in 2.0.0 with the `eval_attrs` call, providing more detailed information on parse failures. Older versions might have less informative error messages.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Be consistent with your grammar definition method. Use `Parser(grammar_string)` for string-based grammars and `ParserPython(grammar_function)` for Python-function-based grammars.","message":"Arpeggio offers two primary ways to define grammars: using EBNF-like strings with `Parser` or Python functions with `ParserPython`. Mixing these or choosing the wrong parser can lead to unexpected behavior.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}