Skip to main content

PyPI license: MIT

MiniGrammar

A parser-generation library that makes use of python metaprogramming to inject the parsing logic into user-defined AST-classes. All the user has to do is to decorate the classes in the codebase with the provided decorators. Such decorators will inject into the classes a constructor (__init__) and a list of parsed elements (elems), which will be accessible for every instance of the classes.

pip install minigrammar

Consider the following grammar:

expression
    : sum
    | mul
    | wrapped_expression
    | num
    | var
    ;

wrapped_expression: '(' expression ')';
sum: expression PLUS expression;
mul: expression STAR expression;
var: ID;
num: INT;

This grammar is left-recursive, so it will cause the parser to loop forever. But we can change it in such a way as to avoid this problem by refactoring it as follows:

expression
    : addend ((PLUS addend)*) 
    ;

addend
    : factor ((STAR factor)*) 
    ;

factor
    : num
    | var
    | wrapped_expression
    ;

wrapped_expression: '(' expression ')';
var: ID;
num: INT;

Then, for every grammar rule we define a Python class that will end up being used to build the AST itself. For instance, let's consider the class WrappedExpression

@chain(["OpenParen", "Expression", "ClosedParen"])
class WrappedExpression(MathSettings):
    def __repr__(self):
        return " ( " + self.elems[1].__repr__() + " ) "

@exact_match("(")
class OpenParen(MathSettings):
    def __repr__(self):
        return self.elems[0].__repr__()


@exact_match(")")
class ClosedParen(MathSettings):
    def __repr__(self):
        return self.elems[0].__repr__()

This class also has a way to be printed in the console. The user can add methods for evaluating the expression or to serialize it in multiple ways and so on. Possibilities are limitless, feel free to explore with your creativity as long as the grammar is not left-recursive and as long as regexes are non-prefix. The whole example has been uploaded in MiniGrammar/examples/math_demo.py.

Metadata

Release files for minigrammar 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for minigrammar 0.2.0
File Size Uploaded Provenance Actions
minigrammar-0.2.0.tar.gz 8.4 kB Without provenance Details for minigrammar-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for minigrammar 0.2.0
File Interpreter ABI Platform Actions
minigrammar-0.2.0-py3-none-any.whl Python 3 none any Details for minigrammar-0.2.0-py3-none-any.whl

Total release size: 16.8 kB

Release files / minigrammar-0.2.0.tar.gz

Download URL minigrammar-0.2.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f523268f4fdcccc42b83d9b23b29a50299b1363f18d53bd6a401adc070e1c30c
BLAKE2b-256 checksum
How to use checksums
4f9fb0f43d18135ff7652828c8127e15ebac59329efd1eafb7fa4f4d17c192c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.5

Release files / minigrammar-0.2.0-py3-none-any.whl

Download URL minigrammar-0.2.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b5a3845625930a18b49496675a9dad55d9e83c1db0fec20ad22b635e886fdea
BLAKE2b-256 checksum
How to use checksums
3ce122f571622ff5593b70fe95836b6a95596f0d0b056e4ccd0124fd9180f342
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.5

Security for minigrammar 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page