Dynamically modifiable context-free grammar written in python
Project description
Neologism
A dynamically modifiable context-free grammar library for Python.
Neologism loads bison-style yacc files directly, exposes the grammar as a
DCFG object you can mutate (add or remove rules, promote nonterminals to
terminals, substitute symbols, and so on), and enumerates the sentences the
resulting grammar produces.
When to reach for it
The need that motivated neologism: you have a real bison grammar, one that
is partly machine-generated, or whose canonical form is a .y file, and
you want to programmatically transform it and then enumerate every sentence
the transformed grammar accepts.
Existing libraries cover one or two of those three pieces:
- ply / lark / parsimonious / ANTLR parse input using a grammar; the grammar itself is not a mutable object you can manipulate after loading.
- NLTK models CFGs as data, including sentence generation, but does not ingest bison.
- bison itself generates a parser, not a manipulable model.
Neologism is the cross-section.
Installing
pip install neologism
Bison must be available on PATH. It is invoked at runtime to convert
.y files to the XML form neologism consumes.
Example
from neologism import DCFG, Rule
dcfg = DCFG.from_yacc_file("my-grammar.y")
# Substitute the opaque terminal LL_NUMBER with a pretty placeholder
dcfg.make_symbol_terminal("LL_NUMBER")
dcfg.add_rule(Rule("LL_NUMBER", ("<number>",)))
# Stream every sentence the transformed grammar produces
for sentence in dcfg.iter_sentences():
print(" ".join(sentence))
The mutation-after-load workflow is the distinguishing feature. axosyslog-cfg-helper is a substantial real-world consumer: it walks the AxoSyslog bison grammar, applies module-specific transformations, and enumerates every driver invocation to build a configuration-option database.
Documentation
Full API reference: https://neologism.readthedocs.io/
License
GPL-3.0-or-later
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 neologism-1.1.0.tar.gz.
File metadata
- Download URL: neologism-1.1.0.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.12.3 Linux/6.17.0-1013-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd8c633636d1568bce6ce88025153087bf59c0550b2ed8a21f190e2f927ea850
|
|
| MD5 |
36e0e9bea8a845d57719c13694a684e4
|
|
| BLAKE2b-256 |
14979c1b563a308f99cdcfd68656056b13b5fde3c1932072a2ec1e8b5777ecea
|
File details
Details for the file neologism-1.1.0-py3-none-any.whl.
File metadata
- Download URL: neologism-1.1.0-py3-none-any.whl
- Upload date:
- Size: 35.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.12.3 Linux/6.17.0-1013-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6807774fab69d01abd09bd40e11ae014bfac38e81ad87fb0fbe71883eb3dab3
|
|
| MD5 |
cdec911124e04b03fc491e61d0c186b2
|
|
| BLAKE2b-256 |
931936fc953bba3207ad6ed1b50066a92e5d1011fc573b9efb6be98edc1a8b7c
|