Skip to main content

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:

  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

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

bnf-1.0.5.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bnf-1.0.5-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file bnf-1.0.5.tar.gz.

File metadata

  • Download URL: bnf-1.0.5.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for bnf-1.0.5.tar.gz
Algorithm Hash digest
SHA256 65fd97d258b4467716d658ad7e9af90657b578cdd12828013f750b8442183308
MD5 b173a0d9d1a0494e9e9f3c3316d4eb20
BLAKE2b-256 31bfec6d051111f2fd90600a449ad09bae3da737db35e30c198ed5fb996e896b

See more details on using hashes here.

File details

Details for the file bnf-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: bnf-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for bnf-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cd4c75dd7dbb27ed753dec53dbcfd17544487ec4566ccb10606e15fd919def1d
MD5 c20619665891c4b7af782e66273f96a4
BLAKE2b-256 5f45a724a6a31be333fef8c60ac3a188cd2f255985db65eeab3bbcffbdbc5d4d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page