https://github.com/theobori/bnfparser
A simple BNF parser, random expression generator and input sequence parser
https://github.com/theobori/bnfparser
Last synced: 2 months ago
JSON representation
A simple BNF parser, random expression generator and input sequence parser
- Host: GitHub
- URL: https://github.com/theobori/bnfparser
- Owner: theobori
- License: mit
- Created: 2024-06-28T14:53:54.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-09-27T13:06:14.000Z (7 months ago)
- Last Synced: 2025-01-10T23:47:19.608Z (4 months ago)
- Language: Python
- Homepage:
- Size: 103 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# BNF parser
[](https://github.com/theobori/bnfparser/actions/workflows/lint.yml) [](https://github.com/theobori/bnfparser/actions/workflows/build.yml)
[](https://builtwithnix.org)
This Python module parses BNF grammar rules and produces an AST representing the BNF syntax. It can also generate random expressions from given BNF grammar rules and parse input sequences matching the BNF rules. It handles infinite recursion grammar rules within the needed `Visitor` implementations.
## 🍬 Syntactic sugar
I've added a grouping feature that have the following syntax.
```text
::= ("A" | "T" | "C" | "G") | ("A" | "T" | "C" | "G")
```It is the same as below.
```text
::= |
::= "A" | "T" | "C" | "G"
```## 📖 Build and run
For the build, you only need the following requirements:
- [Python](https://www.python.org/downloads/) 3+ (tested with 3.12.4)
## 🤝 Contribute
If you want to help the project, you can follow the guidelines in [CONTRIBUTING.md](./CONTRIBUTING.md).
## 📎 Some examples
Here is an example of how you could use this module.
```py
import bnfparser
import sysRESTRICTED_LIST = '''
::= "[" "]"
::= ( | "," )
::= | |
::= |
::= "\"" "\""
::= |
::= | |
::= "a" | "b" | "c" | "z" | "A" | "B" | "C" | "Z"
::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
::= "!" | "@" | "#" | "$" | "%" | "^" | "&" | "*" | "(" | ")" | "-" | "_" | "=" | "+"
'''try:
bnf = bnfparser.parse(RESTRICTED_LIST)
# except bnfparser.LexerError as error:
# pass
# except bnfparser.ParserError as error:
# pass
# except bnfparser.VisitorError as error:
# pass
except bnfparser.BaseError as error:
print(error, file=sys.stderr)# Generate a random expression
print(bnf.generate())# Printing an AST representation
bnf.print()# BNF expressions list (AST)
expressions = bnf.expressions# InputTree inheriting the graphviz.Graph class
input_tree = bnf.parse_input('[")",[0],[882,["Z","6b"],5]]')# Change the entry point rule
try:
bnf.set_start("")
# except bnfparser.CoreError:
# pass
except bnfparser.BaseError:
pass# Generate a random expression
print(bnf.generate())input_tree = bnf.parse_input("\"123\"")
if not input_tree is None:
# Building graphviz graph then render it
input_tree.build_graph().render(
filename="bnf_string_graph",
format="png",
cleanup=True,
view=True,
directory="./images"
)# Saved as ./images/bnf_string_graph.png
```
## 🎉 Tasks
- [x] Iterative generator
- [ ] Iterative input parser
- [x] Handles infinite recursion grammar rules