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
Release files for grammpy 2.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| grammpy-2.1.1.tar.gz | 37.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| grammpy-2.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 104.8 kB
Release files / grammpy-2.1.1.tar.gz
| Download URL | grammpy-2.1.1.tar.gz |
|---|---|
| Size | 37.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cfcadb6d0e6e74647483f87d41e5e42b9668f2b7762fa681f618355168bcba4c
|
|
BLAKE2b-256 checksum How to use checksums |
0ca0101e3ec5d7af661ce6e502ac2155d04503672f0330ada695cd756bb69df1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.13.0
|
Release files / grammpy-2.1.1-py3-none-any.whl
| Download URL | grammpy-2.1.1-py3-none-any.whl |
|---|---|
| Size | 67.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
60d1f8bdfcbf6b36c25eda33b40560fb55c739290a8974d1749d906cdefcec01
|
|
BLAKE2b-256 checksum How to use checksums |
2c0c4048eff0888ce4a26feed8f846b005988b223059d192ca03119a1c35180f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.13.0
|