Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neologism-1.1.0.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neologism-1.1.0-py3-none-any.whl (35.4 kB view details)

Uploaded Python 3

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

Hashes for neologism-1.1.0.tar.gz
Algorithm Hash digest
SHA256 fd8c633636d1568bce6ce88025153087bf59c0550b2ed8a21f190e2f927ea850
MD5 36e0e9bea8a845d57719c13694a684e4
BLAKE2b-256 14979c1b563a308f99cdcfd68656056b13b5fde3c1932072a2ec1e8b5777ecea

See more details on using hashes here.

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

Hashes for neologism-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6807774fab69d01abd09bd40e11ae014bfac38e81ad87fb0fbe71883eb3dab3
MD5 cdec911124e04b03fc491e61d0c186b2
BLAKE2b-256 931936fc953bba3207ad6ed1b50066a92e5d1011fc573b9efb6be98edc1a8b7c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page