Skip to main content

A lightweight framework for building tiny LLM-friendly DSLs

Project description

Grammar School - Python Implementation

A lightweight framework for building tiny LLM-friendly DSLs in Python.

Installation

pip install grammar-school

For development:

pip install -e ".[dev]"

Quick Start

from grammar_school import Grammar, method

class MyGrammar(Grammar):
    @method
    def greet(self, name):
        # @method contains the actual implementation
        # You can do anything here - side effects, state changes, etc.
        print(f"Hello, {name}!")

# No runtime needed - methods execute directly!
grammar = MyGrammar()
grammar.execute('greet(name="World")')

# Methods can maintain state using self
class MyGrammarWithState(Grammar):
    def __init__(self):
        super().__init__()
        self.greetings = []  # State managed in the grammar instance

    @method
    def greet(self, name):
        self.greetings.append(name)
        print(f"Hello, {name}!")

grammar = MyGrammarWithState()
grammar.execute('greet(name="World")')
print(grammar.greetings)  # ['World']

Understanding the Architecture

Grammar School provides a unified interface:

  1. Grammar + @method: Methods contain their implementation directly
  2. Framework handles the rest: Parsing, interpretation, and execution happen automatically

Benefits:

  • Simple and intuitive - just write methods with your logic
  • No need to separate concerns - methods can do anything
  • State management via self attributes
  • The Grammar/Runtime split is handled internally but hidden from you

Streaming Execution

For large DSL programs or real-time processing, you can stream method executions:

grammar = MyGrammar()

# Stream method executions one at a time (memory efficient)
for _ in grammar.stream('greet(name="A").greet(name="B").greet(name="C")'):
    # Methods execute as they're called
    pass

This is useful for:

  • Large programs: Don't load all method calls into memory at once
  • Real-time processing: Start executing methods before parsing completes
  • Memory efficiency: Process methods incrementally

Functional Programming Support

Grammar School allows you to implement functional programming patterns by defining your own methods:

from grammar_school import Grammar, method

class MyGrammar(Grammar):
    @method
    def square(self, x):
        return x * x

    @method
    def is_even(self, x):
        return x % 2 == 0

    @method
    def map(self, func, data):
        # Implement your own map logic
        return [func(x) for x in data]

    @method
    def filter(self, predicate, data):
        # Implement your own filter logic
        return [x for x in data if predicate(x)]

grammar = MyGrammar()
# Use functional operations - you provide the implementation
grammar.execute('map(@square, data)')
grammar.execute('filter(@is_even, data)')
grammar.execute('map(@square, data).filter(@is_even, data)')

Available functional operations:

  • map(@function, data) - Map a function over data
  • filter(@predicate, data) - Filter data using a predicate
  • reduce(@function, data, initial) - Reduce data using a function
  • compose(@f, @g, @h) - Compose multiple functions
  • pipe(data, @f, @g, @h) - Pipe data through functions

Function references: Use @function_name syntax to pass functions as arguments.

Examples

See the examples/ directory for complete DSL implementations.

API Reference

Core Types

  • Value: AST value node (number, string, identifier, bool)
  • Arg: Named argument
  • Call: Function call with arguments
  • CallChain: Chain of calls (method chaining)

Decorators

  • @method: Mark a method as a DSL handler (contains implementation)
  • @rule: Define grammar rules (for custom grammars)

Classes

  • Grammar: Main grammar class that orchestrates parsing and interpretation
  • Interpreter: Interprets CallChain AST into Actions
  • LarkBackend: Lark-based parser backend

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

grammar_school-0.6.0.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

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

grammar_school-0.6.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file grammar_school-0.6.0.tar.gz.

File metadata

  • Download URL: grammar_school-0.6.0.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for grammar_school-0.6.0.tar.gz
Algorithm Hash digest
SHA256 6885cfb5eae7feaf344a3d9426b14fea222bff06ebe158e15b7b1217d7c70eb2
MD5 f67d1772bb4ebee623cd00760dd8cf2d
BLAKE2b-256 117614d1d70adcdcce606d7ade7197e63a85fb3d8c72e9921e07584d55a692b2

See more details on using hashes here.

File details

Details for the file grammar_school-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: grammar_school-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for grammar_school-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad7425faeca25d79bf00eb99ae300524e1d4b90a3779594c05a688ac3b36edaf
MD5 3bd5cc4e76274072a6341f30af0fd89b
BLAKE2b-256 eed381e4e5fcc4c0c5caac66aca310a4909913a2eae3ef5bafd969dcf2b582ae

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