Parsimonious is a pure-Python library for creating parsers based on Parsing Expression Grammars (PEGs). It aims for speed and usability, allowing users to define grammars using a simplified EBNF notation. It is designed for applications requiring efficient parsing of structured text, such as configuration files or domain-specific languages. The current version is 0.11.0.
pip install parsimoniousVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a simple arithmetic grammar, parse an input string, and then process the resulting parse tree using a `NodeVisitor` to evaluate the expression. This pattern of grammar definition followed by a visitor for tree manipulation is central to using Parsimonious effectively.
Always pin your `parsimonious` dependency to a specific version (e.g., `parsimonious==0.11.0`) to ensure stability. Consult the GitHub changelog for specific migration details between versions.
Adhere strictly to Parsimonious's regex syntax (`~"pattern"`). Be aware that future versions might change how regexes are defined or preferred. For complex character classes, explicit regexes are currently efficient.
For very large files or data streams, consider pre-processing to break them into smaller, manageable chunks, or evaluate if a stream-based parsing library is more suitable for your specific use case. Parsimonious is best suited for inputs that fit comfortably in memory.
For any non-trivial processing of the parse tree, implement a `NodeVisitor` subclass. This provides an organized, maintainable, and less error-prone way to traverse and transform the tree.
Ensure your top-level grammar rule accounts for all expected input, including optional whitespace or other characters at the end of the text. Often, adding an implicit or explicit whitespace rule (e.g., `_ = ~'\s*'` and applying it where necessary) or ensuring the main rule covers the full pattern can resolve this. For example, if parsing a full file, make sure your top rule matches all lines/statements.
Examine the grammar rule mentioned in the error (`'some_rule'`) and the input string section indicated by `didn't match at '...'`. Adjust the grammar definition to correctly reflect the expected input format, or modify the input to conform to the grammar. Pay close attention to literals, regular expressions (`~"..."`), and the order of alternatives.
Rewrite the left-recursive rule to an iterative (usually right-recursive) form. For example, a left-recursive rule like `expr = expr '+' term / term` can be rewritten as `expr = term (('+' term)*)`. This converts the recursive definition into one that uses repetition operators.Review your grammar definition and ensure that all referenced rule names are correctly spelled and have a corresponding definition within the `Grammar` string. If a rule is intentionally left undefined, it might indicate a logical error in the grammar design.
No dependency data recorded yet.