PyLGEN
From prototype to production: a Python-native compiler framework that brings the "Dragon Book" to life in Python, with clarity throughout.
PyLGEN gives you complete control over every stage of language processing. Build interpreters and compilers from scratch without leaving the Python ecosystem. Prototype rapidly in pure Python, then compile to native speed with Cython for production workloads.
[!note] Cython compilation requires a C compiler installed on system to compile the code
Why PyLGEN?
-
Full pipeline control: You own every step: lexer, parser, AST, semantic analysis, and evaluation.
-
Dual‑nature design: Pure Python for development and debugging; Cython for near‑C performance.
-
Built for scale: Handles 2‑million‑line inputs with ~1.92x faster parsing than Lark +
lark_cython. -
Python ecosystem integration: Leverage NumPy, SciPy, or any library from within your language.
📊 Benchmark at a Glance
Lark + lark_cython |
PyLGEN | |
|---|---|---|
| Parsing (2M lines, 40 MB) | 116.73 s | 60.80 s |
| AST Construction | Separate pass | Integrated |
| Full Interpreter | — | ~66.03 s (incl. semantic checks + eval) |
| Peak Memory | ~4 GB | ~927 MB |
PyLGEN is not just a parser, it's a complete, production‑ready interpreter framework that outperforms industry‑standard tools.
🚀 Minimal Example
from pylgen.lexer import Lexer
from pylgen.grammar import AttributedGrammar
from pylgen.parser import ParserBuilder, ParserType
# 1. Define tokens & lexer
lexer = Lexer(mapping_function, r'\s+')
lexer[0, 'NUMBER'] = r'\d+'
lexer[1, 'PLUS'] = r'\+'
# 2. Define grammar with AST builders
G = AttributedGrammar(start=Symbol('E'))
G[E] += (E, plus, T), binary_reductor
G[E] += (T,), single_reductor
# 3. Build the parser
parser = ParserBuilder.build_parser_from_attributed(G, ParserType.LALR1)
# 4. Parse, analyse, execute
ast = parser.parse(lexer.tokens)
# ... your semantic visitors & evaluator ...
🧩 Architecture
flowchart LR
Source["Source Code"] --> Lexer["Lexer"]
Lexer --> Parser["Parser LALR(1)"]
Parser --> AST["AST"]
AST --> Visitors["Visitors"]
Visitors --> Result["Result / Errors"]
Module |
Purpose |
|---|---|
common |
Core types: Symbol, AST, Token, ASTListView |
automaton |
Finite automata(DFA/NFA), determinization, Hopcroft minimisation |
regex |
Full regex engine -> automata conversion |
lexer |
Regex‑based tokenisation with priority and validation |
grammar |
CFG and attributed grammar with reducers |
parser |
LALR(1) parser generation and runtime |
analysis |
Visitor pattern, traversal strategies, context management |
visual |
Interactive HTML visualisation of ASTs, parse trees, automata and parsing tables |
All modules are fully usable from Python and Cython, prototype in Python, ship in C.
⚡ Quick Start
pip install pylgen-core
Then build your interpreter step by step, following the complete tutorial in the documentation.
📖 Learn More
-
Step‑by‑step tutorial: Build a full arithmetic REPL from scratch.
-
VecLang case study: Production‑grade language with vectors, functions, and slicing.
-
Deep‑dive API tour: Understand every module inside out.
PyLGEN: Where compiler theory meets Python pragmatism.
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 pylgen_core-0.5.8.tar.gz.
File metadata
- Download URL: pylgen_core-0.5.8.tar.gz
- Upload date:
- Size: 2.4 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca8d9998eba376dfef277700b5199db51549e7fbccb12d1c4ff7726fe35b78fd
|
|
| MD5 |
43091dbcfe85f5c8f8328624fe493cc3
|
|
| BLAKE2b-256 |
137a0e78baf409ea82a198ca0ccce28e771ff64e6b7f957560357b70c226025b
|
File details
Details for the file pylgen_core-0.5.8-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: pylgen_core-0.5.8-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 3.4 MB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3cf0738220eb2b1df7dbc9ac7140e57ece089f54cf2102c442d01fc33ff70390
|
|
| MD5 |
5720068ba23d3d778f9f9f6adbed7798
|
|
| BLAKE2b-256 |
d7845fa0a4c030c682268c78f9091106cc6754e21c0b7ad02459e1cdb39b548c
|