Skip to main content

A parser-generation library based on decorators

Project description

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), wich will be accessible for every instance of the classes.


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 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([rid("OpenParen"), rid("Expression"), rid("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 has also a way to be printed in the console. The user can add method 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 regex are non-prefix. The whole example has being 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.1.0.tar.gz (8.0 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.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for minigrammar-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d6bc68c015fc067121a857f1bb2d1e8db7ddb2a2efd9707cd969bfd7fee9eaea
MD5 5d8d6a3cc07802fe344c0176f2fcedde
BLAKE2b-256 f3a97b1df86cc39b6ab46e8725932e399219ed6b792f584971b8eb7a3cd8636a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for minigrammar-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c93583b9b54e010ddfe18033c0831f0a5be2fb6bd82fafa1d7defc69f995595
MD5 1b4f24cc9f8bf5c2b9dcf1c90c4e2a8b
BLAKE2b-256 23c24f964d28429e009bcf9acb095c644337239fec70291a38d84dcf1ed7834c

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