Skip to main content

A BNF (Backus-Naur Form) parser and a LL input sequence scanner with backtracking

BNF syntax:

  1. non-terminals between <>

  2. rules end at newline n

  3. assign with ::=

  4. operators:
    • alternative derivations separated by |

    • group items between ()

    • optional items between {} or with postfix ? operator

    • zero or more repetitions with postfix * operator

    • one or more repetitions with postfix + operator

  5. set of terminal values between []: in set [aeiou], not in set [^aeiou] or ranges [a-z]

The BNF compiler uses a LL parser with backtracking:

  1. no left-recursion: <X> :== <X> …

  2. no a+ a alike sequences

  3. longest rule first: rule <X> ::= a | a b must be replaced by <X> ::= a b | a

  4. special chars \<>(){}[]|+*?:= each must be quoted with \

Grammar example for a python tuple of integer literals (tuple.bnf):

`bnf <tuple> ::= \( <body> \) | \( \) <body> ::= <elem> <num> | <elem> <elem> ::= <num> , <elem> | <num> , <num> ::= <dig> <num> | <dig> <dig> ::= [0-9] `

Test if an input sequence matches the above grammar with:

` echo -n "(12,34,)" | python3 -m bnf tuple.bnf `

The printed result should be True or False whether the input sequence is accepted by the grammar, or not, respectively.

Note: input sequence must not contain a newline (n) if grammar does not support it (use echo -n)

Use the environment DEBUG=1 for a verbose output (DEBUG=2 for a more verbose output):

` echo -n "(12,34,)" | DEBUG=1 python3 -m bnf tuple.bnf `

In interactive mode: ` >>> from bnf import grammar, parse >>> grammar("<tuple> ::= \( <body> \) | \( \)\n<body> ::= <elem> <num> | <elem>\n<elem> ::= <num> , <elem> | <num> ,\n<num> ::= <dig> <num> | <dig>\n<dig> ::= [0-9]\n") >>> parse("(12,34,)") `

EBNF syntax:

  1. terminal symbols must be quoted between “”: “if”

  2. rules end with ; not a newline

remaining rules are the same as for BNF syntax.

The tuple example in eBNF becomes (tuple.ebnf):

`bnf tuple ::= '(' body ')' | '(' ')' ; body ::= elem num | elem ; elem ::= num ',' elem | num ',' ; num ::= dig num | dig ; dig ::= [0-9] ; `

Test if an input sequence matches the above grammar with:

` echo -n "(12,34,)" | python3 -m ebnf tuple.ebnf `

In interactive mode: ` >>> from ebnf import grammar, parse >>> grammar("tuple ::= '(' body ')' | '(' ')' ;body ::= elem num | elem ;elem ::= num ',' elem | num ',' ;num ::= dig num | dig ;dig ::= [0-9] ;") >>> parse("(12,34,)") `

The bnf/ package includes:

  • bnf.py: BNF parser and input sequence scanner

  • ebnf.py: extended BNF parser and input sequence scanner

Documentation in the docs/ directory:

  • tutorial.html: an introduction guide

  • internals.html: bnf/ebnf routine description

Code examples:

  • exs/: some demonstration examples

  1. prs, IST 2022

Metadata

Release files for bnf 1.0.5

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

Source distribution (sdist)

Source distribution for bnf 1.0.5
File Size Uploaded Provenance
bnf-1.0.5.tar.gz 6.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for bnf 1.0.5
File Interpreter ABI Platform
bnf-1.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 15.8 kB

Release files / bnf-1.0.5.tar.gz

Download URL bnf-1.0.5.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
65fd97d258b4467716d658ad7e9af90657b578cdd12828013f750b8442183308
BLAKE2b-256 checksum
How to use checksums
31bfec6d051111f2fd90600a449ad09bae3da737db35e30c198ed5fb996e896b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.0

Release files / bnf-1.0.5-py3-none-any.whl

Download URL bnf-1.0.5-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd4c75dd7dbb27ed753dec53dbcfd17544487ec4566ccb10606e15fd919def1d
BLAKE2b-256 checksum
How to use checksums
5f45a724a6a31be333fef8c60ac3a188cd2f255985db65eeab3bbcffbdbc5d4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.0

Security for bnf 1.0.5

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