A BNF (Backus-Naur Form) parser and a LL input sequence scanner with backtracking
Project description
A BNF (Backus-Naur Form) parser and a LL input sequence scanner with backtracking
BNF syntax:
non-terminals between <>
rules end at newline n
assign with ::=
- 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
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:
no left-recursion: <X> :== <X> …
no a+ a alike sequences
longest rule first: rule <X> ::= a | a b must be replaced by <X> ::= a b | a
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:
terminal symbols must be quoted between “”: “if”
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 introdution guide
internals.html: bnf/ebnf routine description
Code examples:
exs/: some demonstration examples
prs, IST 2022
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
Built Distribution
File details
Details for the file bnf-1.0.4.tar.gz
.
File metadata
- Download URL: bnf-1.0.4.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 115d924e4effe6355d8af75eba9f8b83ff730a2d144a451477c79d734c5a4fb4 |
|
MD5 | c7f8348134281dce053e5df2ceaa4005 |
|
BLAKE2b-256 | 5425f1feb026774b1c190816b53ccfcb215c40f343b8deae26afd61ae9c4f03f |
File details
Details for the file bnf-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: bnf-1.0.4-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b48f0756d97b80b735db2f3d35e8956d68887f95ea60b0ae1c450b332bf5f90b |
|
MD5 | dcf09e560ea84f94d3e8f556aa41ec9a |
|
BLAKE2b-256 | ee1a19b94f0728db6829f4e4d0b21c4d276c557eb88504fbd1c9932fcc329deb |