Skip to main content

A parser-generation library based on decorators

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minigrammar-0.2.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

minigrammar-0.2.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file minigrammar-0.2.0.tar.gz.

File metadata

  • Download URL: minigrammar-0.2.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for minigrammar-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f523268f4fdcccc42b83d9b23b29a50299b1363f18d53bd6a401adc070e1c30c
MD5 8cf6e73751f36b9535a4623537799c45
BLAKE2b-256 4f9fb0f43d18135ff7652828c8127e15ebac59329efd1eafb7fa4f4d17c192c8

See more details on using hashes here.

File details

Details for the file minigrammar-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: minigrammar-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for minigrammar-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b5a3845625930a18b49496675a9dad55d9e83c1db0fec20ad22b635e886fdea
MD5 66c24b324b117a66df31b20b102c1643
BLAKE2b-256 3ce122f571622ff5593b70fe95836b6a95596f0d0b056e4ccd0124fd9180f342

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page