{"id":20349942,"url":"https://github.com/fdero/minigrammar","last_synced_at":"2026-01-02T13:23:35.805Z","repository":{"id":260488046,"uuid":"881060461","full_name":"fDero/MiniGrammar","owner":"fDero","description":"A parser-generation library that makes use of python metaprogramming to inject the parsing-logic into user defined AST-classes","archived":false,"fork":false,"pushed_at":"2025-02-28T19:50:50.000Z","size":63,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-28T22:38:16.353Z","etag":null,"topics":["grammar","grammar-parser","metaprogramming","parsing","parsing-library","python3","reflection"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fDero.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-30T21:05:46.000Z","updated_at":"2025-02-28T19:50:53.000Z","dependencies_parsed_at":"2024-12-04T23:33:38.231Z","dependency_job_id":null,"html_url":"https://github.com/fDero/MiniGrammar","commit_stats":null,"previous_names":["fdero/minigrammar"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fDero%2FMiniGrammar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fDero%2FMiniGrammar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fDero%2FMiniGrammar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fDero%2FMiniGrammar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fDero","download_url":"https://codeload.github.com/fDero/MiniGrammar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241879163,"owners_count":20035743,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grammar","grammar-parser","metaprogramming","parsing","parsing-library","python3","reflection"],"created_at":"2024-11-14T22:28:15.679Z","updated_at":"2026-01-02T13:23:35.800Z","avatar_url":"https://github.com/fDero.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n[![PyPI](https://img.shields.io/badge/-PyPI-blue.svg?logo=pypi\u0026labelColor=555555)](https://pypi.org/project/minigrammar \"PyPI\")\n![license: MIT](https://img.shields.io/badge/license-MIT-blue)\n# MiniGrammar\n\nA parser-generation library that makes use of python metaprogramming to inject the parsing logic \ninto user-defined AST-classes. All the user has to do is to decorate the classes in the codebase\nwith the provided decorators. Such decorators will inject into the classes a constructor (`__init__`) and\na list of parsed elements (`elems`), which will be accessible for every instance of the classes.\n\n```bash\npip install minigrammar\n```\n\n---\n\nConsider the following grammar:\n```g4\nexpression\n    : sum\n    | mul\n    | wrapped_expression\n    | num\n    | var\n    ;\n\nwrapped_expression: '(' expression ')';\nsum: expression PLUS expression;\nmul: expression STAR expression;\nvar: ID;\nnum: INT;\n```\n\nThis grammar is left-recursive, so it will cause the parser to loop forever. But we can \nchange it in such a way as to avoid this problem by refactoring it as follows:\n\n```g4\nexpression\n    : addend ((PLUS addend)*) \n    ;\n\naddend\n    : factor ((STAR factor)*) \n    ;\n\nfactor\n    : num\n    | var\n    | wrapped_expression\n    ;\n\nwrapped_expression: '(' expression ')';\nvar: ID;\nnum: INT;\n```\n\nThen, for every grammar rule we define a Python class that will end up being used to build the **AST** itself. For instance,\nlet's consider the class `WrappedExpression`\n\n```python\n@chain([\"OpenParen\", \"Expression\", \"ClosedParen\"])\nclass WrappedExpression(MathSettings):\n    def __repr__(self):\n        return \" ( \" + self.elems[1].__repr__() + \" ) \"\n\n@exact_match(\"(\")\nclass OpenParen(MathSettings):\n    def __repr__(self):\n        return self.elems[0].__repr__()\n\n\n@exact_match(\")\")\nclass ClosedParen(MathSettings):\n    def __repr__(self):\n        return self.elems[0].__repr__()\n```\n\nThis 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\nand 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 \nregexes are non-prefix. The whole example has been uploaded in `MiniGrammar/examples/math_demo.py`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffdero%2Fminigrammar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffdero%2Fminigrammar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffdero%2Fminigrammar/lists"}