A utility package for editing grammars (CFG, PDA, etc)
Project description
gramutil
GramUtil is a utility for grammars in computer science. Currently, context-free grammars (CFG) and pushdown automatas (PDA) are supported.
Installation
pip install gramutil
Overview
ContextFreeGrammar
This class provides initialization and methods for a context free grammar. Common operations are demonstrated below:
from gramutil import cfg
grammarString = """
S -> 0 S 0 | T
T -> 1 T 1
-> $
"""
# Create the grammar from the string
grammar = cfg.ContextFreeGrammar(grammarString)
# Simplify the grammar (simplifies using basic rules)
grammar.simplify()
# Removes any duplicate productions from any state
grammar.removeDuplicates()
# Converts the grammar to a pushdown automata object
pda = grammar.toPDA()
# Prints a textual representation of the grammar
print(grammar)
The rules for a grammar string are as follows:
- Start state must be
S - Multiple productions for one state may be separated by the
|character - Multiple productions for one state may also be on multiple lines, as shown above
- The epsilon (empty string) character is signified with a
$ - Terminals may be an underscore or lowercase alphanumeric
- Nonterminals have the same rules, but must start with an uppercase character and allow uppercase throughout
- All terms (terminals and nonterminals) must be space-separated
PushdownAutomata
This class provides initialization and method for a pushdown automata. Currently, creation is only supported from a ContextFreeGrammar.
from gramutil import cfg
grammar = cfg.ContextFreeGrammar('S -> 0 S 0 | $')
# Two options for creating the PDA
pda = cfg.PushdownAutomata(grammar)
pda = grammar.toPDA()
# Prints a textual representation of the PDA
print(pda)
# Converts the PDA back to a CFG
grammar = pda.toCFG()
grammar.simplify()
print(grammar)
Examples
Check out examples/convert.py for an example use
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 gramutil-0.0.2.tar.gz.
File metadata
- Download URL: gramutil-0.0.2.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d3fb30882993be6eca67928d74bba3ab94c0f8a71d29a6245a0dc46855295a6
|
|
| MD5 |
da0b2e03f062c3fd5b05cf2390fdd075
|
|
| BLAKE2b-256 |
54d90dbbde30183f22066ae6bae5d63d39b7e9ca4f44ce307b54e32b4ce998c5
|
File details
Details for the file gramutil-0.0.2-py3-none-any.whl.
File metadata
- Download URL: gramutil-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9106f359cb2c06ddc9e7d325f232b47295e5d22486f410913b701c192f4c317
|
|
| MD5 |
cd033d80463f518ac3edca5f8b9b17be
|
|
| BLAKE2b-256 |
28e8f4b452104ea54023f913e3a0bd7007936c6f13a92164464ba44a6927d924
|