Skip to main content

pygramm : Grammar processing in Python

Experiments in processing BNF with Python. Work in progress.

Why?

While Ply provides a semi-yaccalike, it has some characteristics that bother me. First, it prioritizes lexical processing by the length of the pattern, not the length of the token ... in violation of the "maximum munch" rule. Second, it tries to do everything at run time.

Also I want to experiment with generation of sentences as well as parsing, and with LL as well as LR parsing.

Work in progress

Done

  • Parse BNF (llparse.py) and create an internal form. The BNF form is extended with Kleene *, but a grammar in pure BNF without Kleene is also fine.
  • Internal structure (grammar.py) represents the BNF structure directly. The Grammar object contains a list of symbols, each of which has a single expansion (which could be a sequence or a choice). The following two grammars will produce precisely the same internal form:
    S ::= "a";
    S ::= "b";
    
    and
    S ::= "a" | "b";
    
  • A phrase generator (generator.py), together with some grammar analysis in grammar.py, can produce sentences within a given length limit (the budget) with or without direction. See choicebot.py for an example of how grammar choices can be controlled.

To Do

  • Distinguish lexical from CFG productions even for sentence generation because we will want different tactics for tokens than for RHS. In CFG we budget for length of sentence. In lexical productions we should choose between new and previously used tokens. Currently the BNF goes all the way to string constants, always. The works for the kinds of grammars that Glade learns, but it is not really ideal for generating useful program inputs.
  • Related to the prior point: Infer a good boundary between CFG and lexical structure. In conventional grammar processing, a developer makes this distinction. For grammar learners like Glade, though, the distinction is not trivial to recognize.
  • Add classic grammar analyses, starting with analyses for LL(1) grammars (first, follow), then checking for conflicts, and likewise for LALR(1) and/or LR(1).
  • Add simple transformations, such as left-factoring for LL(1).

Release files for pygramm 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygramm 0.0.2
File Size Uploaded
pygramm-0.0.2.tar.gz 54.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pygramm 0.0.2
File Interpreter ABI Platform
pygramm-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 83.6 kB

Release files / pygramm-0.0.2.tar.gz

Download URL pygramm-0.0.2.tar.gz
Size 54.1 kB
Tags Source
SHA-256 checksum
How to use checksums
71c0319bfb193a7ee5c10366597200a34d6557a62c5fa6b90dd791900531c543
BLAKE2b-256 checksum
How to use checksums
84321080aa588f0a3b870e152a56aa7808d1f18531f2e88a0932c5578e399aac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / pygramm-0.0.2-py3-none-any.whl

Download URL pygramm-0.0.2-py3-none-any.whl
Size 29.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ecf713032441053a3ef3b5185b61a373cc2fa05b66a92cdaa3bb705d8d18722b
BLAKE2b-256 checksum
How to use checksums
65e9b95a79118f2472a54a092032b47e7083cd7e43af8a568d44b7590410a546
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page