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 AST
#Your EBNF file goes here
ebnf = open('grammar.ebnf', 'r')
ast = AST()
try:
#Will raise SyntaxError on error with an error message describing what went wrong
ast.parse(ebnf.read) #You need to pass in a function that returns n characters where n is given as the first parameter.
finally:
#Even after an error a partial tree will be generated.
#str gives a text version of the parse tree(meant for debugging), while repr gives the text that it was produced from.
print(str(ast))
print(f'Parsed the file creating a tree with {ast.count} nodes, height of {ast.height}. Each node has at MOST {ast.maxDegree} children.')
def DepthFirst(node, func):
func(node)
for child in node.children:
DepthFirst(child, func)
#This will visit each node in the parse tree and print the line where its text begins
DepthFirst(ast.root, lambda node: print(node.startLine))
from parse_ebnf import ASTCommentNode
#Finds each comment in the file and prints its text content
for child in ast.root.children:
if isinstance(child, ASTCommentNode):
print(child.data)
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-1.0.2.tar.gz
(20.1 kB
view details)
Built Distribution
File details
Details for the file parse_ebnf-1.0.2.tar.gz
.
File metadata
- Download URL: parse_ebnf-1.0.2.tar.gz
- Upload date:
- Size: 20.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 038874950091d603b3debb9c807b92efb98b18e7c132e0e7024fa78ba15aa66d |
|
MD5 | a07d630c560bd6e111f9300661f4b111 |
|
BLAKE2b-256 | 8d742e5c6d92e94188dfa7bd4b0faedf1a0eae90634d9c235a406731cc68991b |
File details
Details for the file parse_ebnf-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: parse_ebnf-1.0.2-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f95d18b57bd4dbe7125bdfa31a7de6951da58da49a4527dc0c6da3b1c9bf998 |
|
MD5 | 9bd4c903c1f49ae49b728a1b1c37d982 |
|
BLAKE2b-256 | d4b1b970af8ed70c1f105f9a43ae30036137e61b3dd3a5edf0bd8d03b230d8d7 |