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.
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.
-
Handles large inputs: 2‑million‑line files parsed in ~61.62 s, about 1.89x faster 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 | 61.62 s |
| AST Construction | Separate pass | Integrated |
| Full Interpreter | — | ~67.52 s (incl. semantic checks + eval) |
| Peak Memory | ~4 GB | ~968 MB |
more details in the benchmark section of documentation →
PyLGEN is a framework for building interpreters, covering the full compilation pipeline from lexical analysis to execution.
🚀 Minimal Example
from pylgen.lexer import Lexer
from pylgen.grammar import AttributedGrammar
from pylgen.parser import ParserBuilder, ParserType
from pylgen.common.enums import TokenType
from pylgen.common.types import Symbol
class TokenTypeEnum(TokenType):
NUMBER = 'NUMBER'
PLUS = 'PLUS'
# ...
def mapping_function(t:TokenTypeEnum,tx:str) -> Symbol:
# ....
# 1. Define tokens & lexer
lexer = Lexer(mapping_function, r'\s+')
lexer[0, TokenTypeEnum.NUMBER] = r'\d+'
lexer[1, TokenTypeEnum.PLUS] = r'\+'
# 2. Define grammar with AST builders
# Assumes the following are already defined:
# E, T, plus, binary_reductor, single_reductor
G = AttributedGrammar(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
text = '1 + 2' # your text to parse
lexer.load_text(text)
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, and error hierarchy |
automaton |
Finite automata(DFA/NFA), determinization, Hopcroft minimization |
regex |
Full regex engine -> automata conversion |
lexer |
Regex‑based tokenization 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 visualization 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.
-
Under the hood: Build a lexer and parser from scratch, using the same building blocks that the framework uses internally.
-
Examples: See practical examples of using PyLGEN to build interpreters and DSLs.
PyLGEN: Where compiler theory meets Python pragmatism.
Contributing
Contributions are welcome. Please open an issue or submit a pull request. See Contributing.md for details.
License
This project is licensed under the BSD 3-Clause License. See LICENSE for details.
Changelog
All notable changes to this project are documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
[Unreleased]
[0.7.1] - 2026-09-28
Fixed
BaseLexerwrong behavior with idented languages.
[0.7.0] - 2026-09-27
Changed
-
ASTnow exposesstart_positionandend_positionproperties instead of separatelineandcolumn. The constructor now receivesstart_line,start_column,end_line, andend_columninstead of justlineandcolumn. Breaking change for any subclass ofASTor any reductor that instantiates AST nodes. -
Moved
ErrorTypefrompylgen.analysis.error_typetopylgen.common.enums. Update your imports accordingly. Breaking change. -
Moved
Error,LexicalError,SyntaxError,SemanticErrorandRuntimeErrorfrompylgen.analysis.errortopylgen.common.types. Update your imports accordingly. Breaking change.
[0.6.2] - 2026-08-29
Fixed
- Fixed type mismatches in
BottomUpParserwith the enumBottomUpParserAction.
Metadata
Release files for pylgen-core 0.7.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pylgen_core-0.7.1.tar.gz | 2.2 MB | Details |
Built distributions (wheels)
Total release size: 89.9 MB
Release files / pylgen_core-0.7.1.tar.gz
| Download URL | pylgen_core-0.7.1.tar.gz |
|---|---|
| Size | 2.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d7d7e1b404f4503f9fdf4b19bcf9db300ec82c73284caa8ca97b0fbb176ea541
|
|
BLAKE2b-256 checksum How to use checksums |
7568c0de35f7c0651b69e19bda48d4ea50f2a95e24e29cb4462055d65b4ee786
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp314-cp314-win_amd64.whl
| Download URL | pylgen_core-0.7.1-cp314-cp314-win_amd64.whl |
|---|---|
| Size | 3.2 MB |
| Tags | CPython 3.14 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
e743e7d70f4694559cbae88f7bd489d34254a17dfa3be295ab30c64305b9426c
|
|
BLAKE2b-256 checksum How to use checksums |
5be2f91ef8e0307b8d39915f06341672a7ead6f2fac8227d4eb86db3a5b1156a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp314-cp314-musllinux_1_2_x86_64.whl
| Download URL | pylgen_core-0.7.1-cp314-cp314-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 9.7 MB |
| Tags | CPython 3.14 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
317feaef3708dc4d200f388137c86dab9a14606930484df31b897268b4895f86
|
|
BLAKE2b-256 checksum How to use checksums |
beb895f4728bdfd8b33689f5cbf549a234432ad6594095f1f145c397a3203706
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | pylgen_core-0.7.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 9.6 MB |
| Tags | CPython 3.14 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
22e1cc9d59da9ccfe0207fd84a5d4171b135894a223d708add0bb5ab0c6cb83a
|
|
BLAKE2b-256 checksum How to use checksums |
bcb2c0015c0aa36f35d7f64e805b6f4d30d76745ad69c6c7bdfcf431a4fd00d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp314-cp314-macosx_11_0_arm64.whl
| Download URL | pylgen_core-0.7.1-cp314-cp314-macosx_11_0_arm64.whl |
|---|---|
| Size | 3.4 MB |
| Tags | CPython 3.14 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
767ddbf0aca04ee9afd7f65846d62ad75eeb2dde98d7c67e5c2faf10e2acf691
|
|
BLAKE2b-256 checksum How to use checksums |
0ec99b23f2e7300c4fc2cfec841655ea3e092997a5323d04d84a7b751f02f456
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp314-cp314-macosx_10_15_x86_64.whl
| Download URL | pylgen_core-0.7.1-cp314-cp314-macosx_10_15_x86_64.whl |
|---|---|
| Size | 3.4 MB |
| Tags | CPython 3.14 macOS 10.15+ x86-64 |
|
SHA-256 checksum How to use checksums |
a533780bbdf7c34fb3ee1a38a91093f507e7f7fb54dda7f27bac2549895e1a26
|
|
BLAKE2b-256 checksum How to use checksums |
d8d0b11ce081ec468b59d2cca78ebbf2ad42dc5ed8df3fdfa4f836b0f6b7ea51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp313-cp313-win_amd64.whl
| Download URL | pylgen_core-0.7.1-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 3.1 MB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
5b992a11a4d9439476b36f4cc23dab59210b193b2876296f97392ef484bf5937
|
|
BLAKE2b-256 checksum How to use checksums |
e219b6817e6c33ee54734e2ef4776787a9ea74cfc3d2e2d5c2caa0d1a54afc62
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp313-cp313-musllinux_1_2_x86_64.whl
| Download URL | pylgen_core-0.7.1-cp313-cp313-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 9.7 MB |
| Tags | CPython 3.13 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
82048d0838aef254ec9f86ad532d8e1a776acc5fcb0e4ed50af88ee3188a9ae6
|
|
BLAKE2b-256 checksum How to use checksums |
4cdbc2f4cdcf249aea2fb47a16acdf276ffea969e31f7be5138d18d58e1fc64c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | pylgen_core-0.7.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 9.7 MB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
04b475ce1b0dbe75092802e286c5f4dbe7f0d22a1c76129d974ff430554e83cc
|
|
BLAKE2b-256 checksum How to use checksums |
97c39d40d219d23c3cb7dc5cefb358932172e248ddf3a6c652686e64dde1fb1b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | pylgen_core-0.7.1-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 3.3 MB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
11629f1f93e87f546e0fe971b7d4fbabfcde76a409cfcb69d7500561cbfb8fc6
|
|
BLAKE2b-256 checksum How to use checksums |
f846f165b0704133c019d90404d6a463fa755c0653a3bd734be70d0e0970af14
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp313-cp313-macosx_10_13_x86_64.whl
| Download URL | pylgen_core-0.7.1-cp313-cp313-macosx_10_13_x86_64.whl |
|---|---|
| Size | 3.3 MB |
| Tags | CPython 3.13 macOS 10.13+ x86-64 |
|
SHA-256 checksum How to use checksums |
6428a2f3c95f3f3d34f48d384fe1fb196d4474b6d30c22babe024e28de2bbc18
|
|
BLAKE2b-256 checksum How to use checksums |
91091f3aa220b33dc6190e295cae42402892e2758682a01a95b91c022afb35ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp312-cp312-win_amd64.whl
| Download URL | pylgen_core-0.7.1-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 3.1 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
ae7d906a06f9d48f45ab819fc0bc9bff58bde972550199f46a5ca26e8c6d2eff
|
|
BLAKE2b-256 checksum How to use checksums |
c8028f3db765576a25336d9937284c5e17de61e7acc85ed1d9f47f3dc0a7118f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp312-cp312-musllinux_1_2_x86_64.whl
| Download URL | pylgen_core-0.7.1-cp312-cp312-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 9.8 MB |
| Tags | CPython 3.12 Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
553c441e69b8a3478a2859a090617696c9716e364523e9fa502789d904e39472
|
|
BLAKE2b-256 checksum How to use checksums |
f19612d87f6e2e77e496b3ac614b02e1d43350398497a22eaf0e80dc03d10778
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | pylgen_core-0.7.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 9.8 MB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
4df912a000ea99f8aadcf76e3aea07a48325b2f841343d31e0d97489bc06ca57
|
|
BLAKE2b-256 checksum How to use checksums |
65effc05d4b8e4f698b310a029dfb7d9bf902f0ac3af97a28289180b2ed3093c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | pylgen_core-0.7.1-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 3.3 MB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
61b178ce455496bc03aea9ebf4cb26c0202ee57cd45fb00c850fdfc6a1ca714e
|
|
BLAKE2b-256 checksum How to use checksums |
d82ff20f59c527da8fe56d052dc02b23dd5b72e24abad2ea4559a2d1c264b12b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|
Release files / pylgen_core-0.7.1-cp312-cp312-macosx_10_13_x86_64.whl
| Download URL | pylgen_core-0.7.1-cp312-cp312-macosx_10_13_x86_64.whl |
|---|---|
| Size | 3.4 MB |
| Tags | CPython 3.12 macOS 10.13+ x86-64 |
|
SHA-256 checksum How to use checksums |
df454d268c195d2c73462ff64999a00b008d3ab62786c46e7e2783cf3714d41e
|
|
BLAKE2b-256 checksum How to use checksums |
4f7e716b24a86cf207813c48c2e2f067a4e068f44e346fa47e0f9d7e5bf5c6a1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.7
|