A pure Python 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
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.6-3.11
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
Built Distribution
File details
Details for the file parglare-0.18.0.tar.gz
.
File metadata
- Download URL: parglare-0.18.0.tar.gz
- Upload date:
- Size: 1.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7db7366976644b0d8e106503cf2a3e001cd3adb22ed0252ef8fd7b31d9d2c1cc |
|
MD5 | 17506f7c89e3b9e3a9fbd57fa4f2716d |
|
BLAKE2b-256 | e1f742d764a514bf67e6a61643552a6cd181bc0858226e255c467475ef07ef55 |
File details
Details for the file parglare-0.18.0-py3-none-any.whl
.
File metadata
- Download URL: parglare-0.18.0-py3-none-any.whl
- Upload date:
- Size: 59.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fedb997ec41e50e7232ff6dc739664215b0a67422ad8e820d9f37566b7f219c3 |
|
MD5 | 792a33f41d4bc5199137a0b067a3be3e |
|
BLAKE2b-256 | e67d9807acd0638904398a2a31a76857d3a307600780c8f06795c09b421319f8 |