Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (testpypi.python.org).
Help us improve Python packaging - Donate today!

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;
number: /\d+(\.\d+)?/;
"""

actions = {
    "E": [lambda _, nodes: nodes[0] + nodes[2],
          lambda _, nodes: nodes[0] - nodes[2],
          lambda _, nodes: nodes[0] * nodes[2],
          lambda _, nodes: nodes[0] / nodes[2],
          lambda _, nodes: nodes[0] ** nodes[2],
          lambda _, nodes: nodes[1],
          lambda _, nodes: nodes[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
# -- Debuging/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 2.7, 3.3-3.6

Credits

Initial layout/content of this package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Release History

This version
History Node

0.4

History Node

0.1

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Hash SHA256 Hash Help Version File Type Upload Date
parglare-0.4-py2.py3-none-any.whl
(50.9 kB) Copy SHA256 Hash SHA256
py2.py3 Wheel Oct 18, 2017
parglare-0.4.tar.gz
(785.1 kB) Copy SHA256 Hash SHA256
Source Oct 18, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting