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:
- Grammar + @method: Methods contain their implementation directly
- 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
selfattributes - 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 supports functional programming paradigms through the FunctionalMixin:
from grammar_school import Grammar, FunctionalMixin, method
class MyGrammar(Grammar, FunctionalMixin):
@method
def square(self, x):
return x * x
@method
def is_even(self, x):
return x % 2 == 0
grammar = MyGrammar()
# Use functional operations with function references
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 datafilter(@predicate, data)- Filter data using a predicatereduce(@function, data, initial)- Reduce data using a functioncompose(@f, @g, @h)- Compose multiple functionspipe(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 argumentCall: Function call with argumentsCallChain: 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 interpretationInterpreter: Interprets CallChain AST into ActionsLarkBackend: 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
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 grammar_school-0.5.0.tar.gz.
File metadata
- Download URL: grammar_school-0.5.0.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb0605b124e3a4b16dfc9b500d163c607e06d6e9c6b85c208ba1fba25716c1ae
|
|
| MD5 |
86ec2f1e0c224898e485aff642fc9d16
|
|
| BLAKE2b-256 |
f724b53b938a0c3de25a552f4fa021678e43dfdad2e6e053ec2d31ecb25c3357
|
File details
Details for the file grammar_school-0.5.0-py3-none-any.whl.
File metadata
- Download URL: grammar_school-0.5.0-py3-none-any.whl
- Upload date:
- Size: 23.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b31e961d2f72eba36cc78fc1bd7c858b4254428c1b0efa17545143a6b46fc4c
|
|
| MD5 |
3ba463b5d626c9f8bb32de42fdae8d3d
|
|
| BLAKE2b-256 |
9aa3f04519bad82037ca0660b5591577efc9e8c9909606938bf31a218cdd6248
|