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:
Install just.
$ git clone git@github.com:igordejanovic/parglare.git $ cd parglare $ just dev
Citing parglare
If you use parglare in your research please cite this paper:
Igor Dejanović, Parglare: A LR/GLR parser for Python,
Science of Computer Programming, issn:0167-6423, p.102734,
DOI:10.1016/j.scico.2021.102734, 2021.
@article{dejanovic2021b,
author = {Igor Dejanović},
title = {Parglare: A LR/GLR parser for Python},
doi = {10.1016/j.scico.2021.102734},
issn = {0167-6423},
journal = {Science of Computer Programming},
keywords = {parsing, LR, GLR, Python, visualization},
pages = {102734},
url = {https://www.sciencedirect.com/science/article/pii/S0167642321001271},
year = {2021}
}
License
MIT
Python versions
Tested with 3.8-3.14
Credits
Initial layout/content of this package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Release files for parglare 0.22.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| parglare-0.22.0.tar.gz | 1.7 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| parglare-0.22.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.8 MB
Release files / parglare-0.22.0.tar.gz
| Download URL | parglare-0.22.0.tar.gz |
|---|---|
| Size | 1.7 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b0b12c35525b93743e526dc265bb58812e30fcf546975a490cc95352ba5cb22e
|
|
BLAKE2b-256 checksum How to use checksums |
71ef135463a6cef784f2d4e7f9e06fa2d808741b147d47b2ac3e869faf9dc6f3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.34.2
|
Release files / parglare-0.22.0-py3-none-any.whl
| Download URL | parglare-0.22.0-py3-none-any.whl |
|---|---|
| Size | 63.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
535cccca425c252fd3191103235e483d3f7ea984ea9081a38825578b6de7cbb2
|
|
BLAKE2b-256 checksum How to use checksums |
87769a1f7c66051f92db411b2e759a0895c0b77937abf0cc3a6ef8a7e39eb00c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.34.2
|