CYK library with all required tools to parse context-free grammars.
Project description
grammpy
Current version: 2.0.0
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 a 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.
Version: 2.0.0
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
File details
Details for the file grammpy-2.0.0.tar.gz
.
File metadata
- Download URL: grammpy-2.0.0.tar.gz
- Upload date:
- Size: 28.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.26.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 663fb2cbfd0ba6eeed42f9c465f720dec376712c165ae3ad0f060aa1aa9068e7 |
|
MD5 | 79cddf67949e100f8a4c83750ae3ed0d |
|
BLAKE2b-256 | 897bc2c79c3f200c3d226765e11350f1c9f3b02d0a9c7e025c8511e79c60ab49 |