A parse tree generator for extended Backus-Naur form.
Project description
Parse EBNF
Table of Contents
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
pip install parse-ebnf
Quick start
from parse_ebnf import parse_file
from parse_ebnf.parsing import ParsingError
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 that holds the documentation.
License
parse-ebnf
is distributed under the terms of the MIT license.
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
parse_ebnf-2.0.0.tar.gz
(26.9 kB
view details)
Built Distribution
File details
Details for the file parse_ebnf-2.0.0.tar.gz
.
File metadata
- Download URL: parse_ebnf-2.0.0.tar.gz
- Upload date:
- Size: 26.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31db4d75c67e32e81a44c80d5dcefd3d4a73d7b47990afa245d7de4d8e9007dd |
|
MD5 | 8f5efdc0f75d21e4118064208856ae66 |
|
BLAKE2b-256 | f09c1db506bd66a53acdd4f8e129108a401c50f0fe96bc5b105a968a0cddf5e8 |
File details
Details for the file parse_ebnf-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: parse_ebnf-2.0.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c316babce8509e82292e93ec2d330c316beac72e386eed6b0325b92ba2fe2204 |
|
MD5 | 6af7f012013152e363e0a9d5dd40e047 |
|
BLAKE2b-256 | 6dfb443bde287346025419f0ddccfbc034b8dc301af50c3d7bfcc0088774b0cc |