A Python library for building Parsing Expression Grammer (PEG) parsers using a custom grammar notation. FLTK generates packrat PEG parsers that produce type-safe Concrete Syntax Trees (CST). The overall goal is to allow developers to specify the grammar intuitively without worrying about the details of the parsing algorithm. A major secondary goal is to make the resulting syntax trees easy to work with and type-safe.
- Custom Grammar Notation: Define grammars using
.fltkgformat (the grammar parser is self-hosting) - Extensions for recursive grammars: Supports left-recursive grammars automatically
- Packrat Parsing: Built-in memoization for efficient O(N) parsing
- Type-Safe CST: Generated node classes with typed child access methods
- Source Tracking: All nodes maintain spans to original source text
- Python Code Generation: Generates clean, readable Python parser code
- Define a grammar (
calc.fltkg):
grammar := expression;
expression := term , (("+" | "-") , term)*;
term := factor , (("*" | "/") , factor)*;
factor := number | "(" , expression , ")";
number := /[0-9]+/;
- Generate parser:
import fltk
# Generate parser from grammar
parser_code = fltk.generate_parser("calc.fltkg")
with open("calc_parser.py", "w") as f:
f.write(parser_code)- Use the parser:
from calc_parser import Parser
import fltk.fegen.pyrt.terminalsrc as ts
source = "3 + 4 * 2"
parser = Parser(ts.StringTerminalSource(source))
cst = parser.parse_grammar()
print(cst) # Parsed CST with full source trackingFLTK uses a powerful grammar notation with the following features:
rule_name := alternative1 | alternative2;
.- No whitespace allowed between items,- Whitespace allowed between items:- Whitespace required between items
?- Optional (zero or one)+- One or more*- Zero or more
%- Suppress (don't include in CST)$- Include (force include)!- Inline (flatten into parent)
rule := label:identifier , "literal" , /regex_pattern/;
rule := method_call() | variable_ref;
invocation := method:identifier . "(" , args:expression? , ")";
fltk.fegen: Grammar processing and parser generationfltk.iir: Intermediate representation and type systemfltk.fegen.pyrt: Runtime support (memoization, error tracking)
# Install dependencies
uv sync --group test --group lint# Run all tests
uv run pytest
# Run with coverage
uv run coverage run -m pytest && uv run coverage report# Check style and types
uv run ruff check . && uv run pyright
# Format code
uv run ruff format .
# Fix auto-fixable issues
uv run ruff check --fix .# Using setuptools
uv build
# Using Bazel
bazel build //...See the grammar files in fltk/fegen/ for real-world examples:
bootstrap.fltkg- Minimal grammar for bootstrappingfegen.fltkg- Full grammar definitionfltk.fltkg- Extended grammar with advanced features
- Python 3.10+
- Dependencies:
astor,typer
MIT License - see LICENSE for details.
- Fork the repository
- Create a feature branch
- Make your changes with tests
- Run
uv run ruff check . && uv run pyrightto check style and types - Submit a pull request
- Issues: GitHub Issues