# Parse EBNF
[![PyPI - Version](https://img.shields.io/pypi/v/parse-ebnf.svg)](https://pypi.org/project/parse-ebnf)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/parse-ebnf.svg)](https://pypi.org/project/parse-ebnf)
-----
**Table of Contents**
- [Introduction](#introduction)
- [Installation](#installation)
- [Quick start](#quickstart)
- [Documentation](#documentation)
- [License](#license)
## Introduction
A simple and hacky parser for EBNF as defined by ISO. Give it an EBNF string and
it'll generate a **parse tree**. Note that this package does not parse the
described grammar.
## Installation
```console
pip install parse-ebnf
```
## Quick start
```python
from parse_ebnf.parsing import ParsingError, parse_file
try:
#Your EBNF file goes here.
pt = parse_file(ebnf_path)
partial = False
except ParsingError as e:
#If an exception occurs, a partial tree is generated. See the docs for
#details.
pt = e.parser.pt
partial = True
#Prints the text that the tree was parsed from.
print(str(pt))
#Prints a debug view of the tree.
print(repr(pt))
print(f'Parsing the file created a tree with {pt.count} nodes.')
print(f'The tree has a height of {pt.height}.')
print(f'Each node in the tree has at MOST {pt.maxDegree} children.')
def DepthFirst(node, partial, func):
#Partial nodes are in a mostly undefined state.
if not partial: func(node)
for child in node.children:
#If a node is partial, then its last child is partial. All other
#children are not partial.
if partial and child is node.children[-1]:
DepthFirst(child, True, func)
else:
DepthFirst(child, False, func)
#This will visit each node in the parse tree and print the line where its
#text begins.
DepthFirst(pt.root, partial, lambda node: print(node.startLine))
from parse_ebnf.nodes import Comment
#Finds each comment in the file and prints its text content.
for child in pt.root.children:
if isinstance(child, Comment):
#A tree being partial means that its root is partial.
if partial and child is pt.root.children[-1]: continue
print(str(child))
```
## Documentation
Check the [github page](https://chaosinventor.github.io/parse-ebnf/) that
holds the documentation.
## License
`parse-ebnf` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
Raw data
{
"_id": null,
"home_page": null,
"name": "parse-ebnf",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "EBNF, Extended Backus-Naur Form, Parse tree, Parser",
"author": null,
"author_email": "ChaosInventor <chaosinventor@yandex.com>",
"download_url": "https://files.pythonhosted.org/packages/9f/09/4976f84b30dd4df1a4b8d21f85bb5e1de902c0a058dc76d35700f7392d6f/parse_ebnf-2.0.2.tar.gz",
"platform": null,
"description": "# Parse EBNF\n\n[![PyPI - Version](https://img.shields.io/pypi/v/parse-ebnf.svg)](https://pypi.org/project/parse-ebnf)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/parse-ebnf.svg)](https://pypi.org/project/parse-ebnf)\n\n-----\n\n**Table of Contents**\n\n- [Introduction](#introduction)\n- [Installation](#installation)\n- [Quick start](#quickstart)\n- [Documentation](#documentation)\n- [License](#license)\n\n## Introduction\n\nA simple and hacky parser for EBNF as defined by ISO. Give it an EBNF string and\nit'll generate a **parse tree**. Note that this package does not parse the\ndescribed grammar.\n\n## Installation\n\n```console\npip install parse-ebnf\n```\n## Quick start\n\n```python\nfrom parse_ebnf.parsing import ParsingError, parse_file\n\ntry:\n #Your EBNF file goes here.\n pt = parse_file(ebnf_path)\n partial = False\nexcept ParsingError as e:\n #If an exception occurs, a partial tree is generated. See the docs for\n #details.\n pt = e.parser.pt\n partial = True\n\n#Prints the text that the tree was parsed from.\nprint(str(pt))\n#Prints a debug view of the tree.\nprint(repr(pt))\n\nprint(f'Parsing the file created a tree with {pt.count} nodes.')\nprint(f'The tree has a height of {pt.height}.')\nprint(f'Each node in the tree has at MOST {pt.maxDegree} children.')\n\ndef DepthFirst(node, partial, func):\n #Partial nodes are in a mostly undefined state.\n if not partial: func(node)\n for child in node.children:\n #If a node is partial, then its last child is partial. All other\n #children are not partial.\n if partial and child is node.children[-1]:\n DepthFirst(child, True, func)\n else:\n DepthFirst(child, False, func)\n\n#This will visit each node in the parse tree and print the line where its\n#text begins.\nDepthFirst(pt.root, partial, lambda node: print(node.startLine))\n\nfrom parse_ebnf.nodes import Comment\n\n#Finds each comment in the file and prints its text content.\nfor child in pt.root.children:\n if isinstance(child, Comment):\n #A tree being partial means that its root is partial.\n if partial and child is pt.root.children[-1]: continue\n print(str(child))\n```\n\n## Documentation\n\nCheck the [github page](https://chaosinventor.github.io/parse-ebnf/) that\nholds the documentation.\n\n## License\n\n`parse-ebnf` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.\n\n",
"bugtrack_url": null,
"license": null,
"summary": "A parse tree generator for extended Backus-Naur form.",
"version": "2.0.2",
"project_urls": {
"Documentation": "https://github.com/ChaosInventor/parse-ebnf#readme",
"Issues": "https://github.com/ChaosInventor/parse-ebnf/issues",
"Source": "https://github.com/ChaosInventor/parse-ebnf"
},
"split_keywords": [
"ebnf",
" extended backus-naur form",
" parse tree",
" parser"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ba5fecf329a41e58dfdb2a1378a239db81d2099afb537813392445d8a9c58b01",
"md5": "7d76d62a3442f80d29e954fef5281c37",
"sha256": "f5357af78ab06cd9a743706092dc24e8edeedcc25517c2994fabca5cfa416dc0"
},
"downloads": -1,
"filename": "parse_ebnf-2.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7d76d62a3442f80d29e954fef5281c37",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 13381,
"upload_time": "2024-08-31T11:31:53",
"upload_time_iso_8601": "2024-08-31T11:31:53.388306Z",
"url": "https://files.pythonhosted.org/packages/ba/5f/ecf329a41e58dfdb2a1378a239db81d2099afb537813392445d8a9c58b01/parse_ebnf-2.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9f094976f84b30dd4df1a4b8d21f85bb5e1de902c0a058dc76d35700f7392d6f",
"md5": "33ed639a20b7aa0fd82f4a4e571d6b29",
"sha256": "f87858ac2e0e35c671fd2b3f9e6fb6875020e6c944950450b2523096720411d9"
},
"downloads": -1,
"filename": "parse_ebnf-2.0.2.tar.gz",
"has_sig": false,
"md5_digest": "33ed639a20b7aa0fd82f4a4e571d6b29",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 26873,
"upload_time": "2024-08-31T11:31:54",
"upload_time_iso_8601": "2024-08-31T11:31:54.990160Z",
"url": "https://files.pythonhosted.org/packages/9f/09/4976f84b30dd4df1a4b8d21f85bb5e1de902c0a058dc76d35700f7392d6f/parse_ebnf-2.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-31 11:31:54",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ChaosInventor",
"github_project": "parse-ebnf#readme",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "parse-ebnf"
}