A pure Python Scannerless LR/GLR parser
Project description
A pure Python scannerless LR/GLR parser.
For more information see the docs.
Quick intro
This is just a small example to get the general idea. This example shows how to parse and evaluate expressions with 5 operations with different priority and associativity. Evaluation is done using semantic/reduction actions.
The whole expression evaluator is done in under 30 lines of code!
from parglare import Parser, Grammar
grammar = r"""
E: E '+' E {left, 1}
| E '-' E {left, 1}
| E '*' E {left, 2}
| E '/' E {left, 2}
| E '^' E {right, 3}
| '(' E ')'
| number;
terminals
number: /\d+(\.\d+)?/;
"""
actions = {
"E": [lambda _, n: n[0] + n[2],
lambda _, n: n[0] - n[2],
lambda _, n: n[0] * n[2],
lambda _, n: n[0] / n[2],
lambda _, n: n[0] ** n[2],
lambda _, n: n[1],
lambda _, n: n[0]],
"number": lambda _, value: float(value),
}
g = Grammar.from_string(grammar)
parser = Parser(g, debug=True, actions=actions)
result = parser.parse("34 + 4.6 / 2 * 4^2^2 + 78")
print("Result = ", result)
# Output
# -- Debugging/tracing output with detailed info about grammar, productions,
# -- terminals and nonterminals, DFA states, parsing progress,
# -- and at the end of the output:
# Result = 700.8
Installation
Stable version:
$ pip install parglare
Development version:
$ git clone git@github.com:igordejanovic/parglare.git
$ pip install -e parglare
License
MIT
Python versions
Tested with 3.6-3.9
Credits
Initial layout/content of this package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
parglare-0.15.0.tar.gz
(5.0 MB
view hashes)
Built Distribution
Close
Hashes for parglare-0.15.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 908d58525127b499cafa25579c4eeab536d0224054a47afe52db788d79685e4f |
|
MD5 | 632a03a6496bb9e3d7f088a6911a8f1d |
|
BLAKE2b-256 | 0eef62f58d8ad203a11ef7c97751151890d65578a2bce150083b25e901a1dba6 |