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.0.1.tar.gz (21.4 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.0.1-py3-none-any.whl (34.9 kB view details)

Uploaded Python 3

File details

Details for the file neologism-1.0.1.tar.gz.

File metadata

  • Download URL: neologism-1.0.1.tar.gz
  • Upload date:
  • Size: 21.4 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.0.1.tar.gz
Algorithm Hash digest
SHA256 3f05e9f0394407b7b853e172a9c401fe2b86a3a0cb1373db6993e8421df930b0
MD5 c35fd4ae117c5629b6953dca385c2081
BLAKE2b-256 732258eb6ecfb6b7d89d2f02a64cd1a0929d838ffd6a6a5a21a1881a85d2d902

See more details on using hashes here.

File details

Details for the file neologism-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: neologism-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 34.9 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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c602146d67f8519dcdd9b1d57841ccf98ac90f0814914c50b8b6385be8b13a7d
MD5 0a7269e5551e3659992115af01a9ab1b
BLAKE2b-256 50c27f5b78b32b16798d8e1043780837e0e68007996774a9d4743643ae1e74cc

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