Parsing library with all required tools to parse context-free grammars.
Project description
grammpy
Package for representing formal grammars. Contains algorithms to work with a grammars and parse them.
Installation
If you are using pip, simple run following command.
pip install grammpy
You can install the package from the repository as well.
git clone https://github.com/PatrikValkovic/grammpy.git
cd grammpy
python setup.py install
Usage
Defining a grammar doesn't require special tools anymore. All what you need is just an IDE and you have the full support.
Let's define grammar using standard Python objects instead.
from grammpy import *
from grammpy.transforms import ContextFree, InverseContextFree
from grammpy.parsers import cyk
class Number:
def __init__(self, value):
self.value = value
def __hash__(self):
return hash(Number)
class PlusNonterminal(Nonterminal):
@property
def value(self):
return self.to_rule.get_value()
class PlusRule(Rule):
rule = ([PlusNonterminal], [PlusNonterminal, '+', PlusNonterminal])
def get_value(self):
child1 = self.to_symbols[0]
child2 = self.to_symbols[2]
return child1.value + child2.value
class RewriteRule(Rule):
fromSymbol = PlusNonterminal
toSymbol = Number
def get_value(self):
return self.to_symbols[0].s.value
g = Grammar(terminals=[Number, '+'],
nonterminals=[PlusNonterminal],
rules=[PlusRule, RewriteRule],
start_symbol=PlusNonterminal)
ContextFree.prepare_for_cyk(g, inplace=True)
root = cyk(g, [Number(5), '+', Number(3), '+' , Number(8)])
root = InverseContextFree.reverse_cyk_transforms(root)
assert root.value == 16
Documentation
You can read more about the library in the doc directory.
Examples
You can view some examples in the examples directory.
Author: Patrik Valkovič
Licence: MIT
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grammpy-2.1.1.tar.gz.
File metadata
- Download URL: grammpy-2.1.1.tar.gz
- Upload date:
- Size: 37.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfcadb6d0e6e74647483f87d41e5e42b9668f2b7762fa681f618355168bcba4c
|
|
| MD5 |
1c37ef6f500c37afa0e846c72cddd5a0
|
|
| BLAKE2b-256 |
0ca0101e3ec5d7af661ce6e502ac2155d04503672f0330ada695cd756bb69df1
|
File details
Details for the file grammpy-2.1.1-py3-none-any.whl.
File metadata
- Download URL: grammpy-2.1.1-py3-none-any.whl
- Upload date:
- Size: 67.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60d1f8bdfcbf6b36c25eda33b40560fb55c739290a8974d1749d906cdefcec01
|
|
| MD5 |
fead6ebb393911f431f9cce1361d77ae
|
|
| BLAKE2b-256 |
2c0c4048eff0888ce4a26feed8f846b005988b223059d192ca03119a1c35180f
|