Skip to main content

Parse EBNF

PyPI - Version PyPI - Python Version


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.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 that holds the documentation.

License

parse-ebnf is distributed under the terms of the MIT license.

Metadata

Release files for parse-ebnf 2.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for parse-ebnf 2.0.2
File Size Uploaded Provenance Actions
parse_ebnf-2.0.2.tar.gz 26.9 kB Without provenance Details for parse_ebnf-2.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for parse-ebnf 2.0.2
File Interpreter ABI Platform Actions
parse_ebnf-2.0.2-py3-none-any.whl Python 3 none any Details for parse_ebnf-2.0.2-py3-none-any.whl

Total release size: 40.3 kB

Release files / parse_ebnf-2.0.2.tar.gz

Download URL parse_ebnf-2.0.2.tar.gz
Size 26.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f87858ac2e0e35c671fd2b3f9e6fb6875020e6c944950450b2523096720411d9
BLAKE2b-256 checksum
How to use checksums
9f094976f84b30dd4df1a4b8d21f85bb5e1de902c0a058dc76d35700f7392d6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release files / parse_ebnf-2.0.2-py3-none-any.whl

Download URL parse_ebnf-2.0.2-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f5357af78ab06cd9a743706092dc24e8edeedcc25517c2994fabca5cfa416dc0
BLAKE2b-256 checksum
How to use checksums
ba5fecf329a41e58dfdb2a1378a239db81d2099afb537813392445d8a9c58b01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Security for parse-ebnf 2.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page