Skip to main content

A pure Python LR/GLR parser

Project description

parglare logo

build-status coverage docs status license python-versions

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.

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

parglare-0.21.0.tar.gz (1.7 MB view details)

Uploaded Source

Built Distribution

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

parglare-0.21.0-py3-none-any.whl (62.0 kB view details)

Uploaded Python 3

File details

Details for the file parglare-0.21.0.tar.gz.

File metadata

  • Download URL: parglare-0.21.0.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for parglare-0.21.0.tar.gz
Algorithm Hash digest
SHA256 fec405041f6e86ab4c0276792c3eebc0433cf8caf81570f1ae23afcfea9b230c
MD5 0c46384c84fb5521c51f7a4bcee0956a
BLAKE2b-256 836c82f11a4e21f5bb2971a17d44448a8bcc6f3749c9e3e3e5caf4ef0f60ff5f

See more details on using hashes here.

File details

Details for the file parglare-0.21.0-py3-none-any.whl.

File metadata

  • Download URL: parglare-0.21.0-py3-none-any.whl
  • Upload date:
  • Size: 62.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for parglare-0.21.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb2899b65c86b34fc5a69b33dc4706a54baa05fba7f8143fbd3f513234637cb3
MD5 0eba59161ece194824bdf688e7abad68
BLAKE2b-256 990593e048ff5e4807115f140c433b2a4f53bd84d018c460773c816b030b33e4

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