SlideShare a Scribd company logo
Python Compiler Internals Thomas Lee Shine Technologies, Melbourne
My Contributions to Python try/except/finally syntax for 2.5 AST to bytecode compilation from Python “ optimization” at the AST level
What will you get out of this? Compiler development != rocket science. No magic: it's just code.
Compiler? Isn't Python interpreted? Well, yes and no.
WTF It ... It's Java?
Aaanyway Let's screw with the compiler.
Erm ... Before we start ... A patch to fix a quirk in the build process. This has been reported on the bug tracker!
An overview of the compiler Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
New construct: the “unless” statement unless  may_give_you_up: print   “never gonna give you up...”
Semantics of “unless” Works just like “if not” ... if not  would_consider_letting_you_down: print   “never gonna let you down...”
Implementing “unless” Modify the Grammar Change the AST definition Generate bytecode for “unless”
So what's first? Modify the Grammar Change the AST definition Generate bytecode for “unless”
What does this change affect? Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
WTF is a “grammar”? Defines the syntactic structure of the language.
Why a tokenizer? Makes parsing easier.
So what does a tokenizer do, then? Generates a stream of events for the parser.
What does the parser do? Organises tokens into the structure defined by the grammar. This is the “parse tree”.
What's next? Modify the Grammar Change the AST definition Generate bytecode for “unless”
What's affected by this change? Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
What's an AST? Similar to a parse tree, but not bound to the syntax “ Abstract Syntax Tree”
Why does Python use an AST? Much easier to operate upon an AST than a parse tree.
AST reuse New constructs can sometimes be expressed using existing AST nodes ...
AST reuse ... so we  could  implement “unless” using “if” and “not” AST nodes. But then I wouldn't be able to show you the code generator ... Unless (test, body) =  If ( Not (test), body)
What's next? Modify the Grammar Change the AST definition Generate bytecode for “unless”
What's affected by this change? Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
Generating bytecode for “unless” Add a hook for our new AST node Generate bytecode implementing “unless” logic Use basicblocks as labels for jumps
What about the rest of the compiler? Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
“unless” in action Let's see if this stuff works ...
Where to go from here? Use the Source. Don't try to grok it all at once. Don't be afraid to be wrong. Annoy the python-dev mailing list, like I do!
Thanks!
Questions?

More Related Content

PPTX
Fundamentals of Python Programming
PDF
Python strings
PPT
Inner classes ,annoumous and outer classes in java
PDF
Intro to Jupyter Notebooks
PDF
Python Style Guide
PDF
LockFree Algorithm
PPTX
Introduction to numpy Session 1
PDF
Python Basics | Python Tutorial | Edureka
Fundamentals of Python Programming
Python strings
Inner classes ,annoumous and outer classes in java
Intro to Jupyter Notebooks
Python Style Guide
LockFree Algorithm
Introduction to numpy Session 1
Python Basics | Python Tutorial | Edureka

What's hot (20)

PPTX
Python Collections
PDF
프로그래머가 되고 싶으세요
PPTX
Python Flow Control
PPTX
Constructor in java
PDF
STL in C++
PDF
이승재, 사례로 배우는 디스어셈블리 디버깅, NDC2014
PDF
Python programming : Classes objects
PPT
6 data types
PPTX
TBB 소개
PPTX
Data Types, Variables, and Operators
PDF
코틀린 멀티플랫폼, 미지와의 조우
PPTX
Intro to Python Programming Language
PDF
카카오톡의 서버사이드 코틀린
PPTX
Python Tutorial Part 1
PPTX
Dictionaries and Sets in Python
PPTX
Call by value and call by reference in java
PPTX
Python
PDF
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
PDF
임태현, 게임 서버 디자인 가이드, NDC2013
Python Collections
프로그래머가 되고 싶으세요
Python Flow Control
Constructor in java
STL in C++
이승재, 사례로 배우는 디스어셈블리 디버깅, NDC2014
Python programming : Classes objects
6 data types
TBB 소개
Data Types, Variables, and Operators
코틀린 멀티플랫폼, 미지와의 조우
Intro to Python Programming Language
카카오톡의 서버사이드 코틀린
Python Tutorial Part 1
Dictionaries and Sets in Python
Call by value and call by reference in java
Python
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
임태현, 게임 서버 디자인 가이드, NDC2013
Ad

Similar to Python Compiler Internals Presentation Slides (19)

KEY
Inside Python
ZIP
Inside Python [OSCON 2012]
ODP
Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)
PDF
Building Interpreters with PyPy
PDF
Hacking the Python AST
PDF
Why "Hello World" is a Massive Operation - From Python code to Stack Virtual ...
ODP
Open Source Compiler Construction for the JVM
PDF
How to create a programming language
PPTX
2. introduction
PDF
DSLs for fun and profit by Jukka Välimaa
PPTX
Yacc (yet another compiler compiler)
PPT
Ch4c.ppt
PPTX
6 attributed grammars
DOCX
PPTX
2. introduction to compiler
PPTX
PDF
llvm-py: Writing Compilers In Python
PDF
Writing Parsers and Compilers with PLY
PDF
Learning to Generate Pseudo-code from Source Code using Statistical Machine T...
Inside Python
Inside Python [OSCON 2012]
Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)
Building Interpreters with PyPy
Hacking the Python AST
Why "Hello World" is a Massive Operation - From Python code to Stack Virtual ...
Open Source Compiler Construction for the JVM
How to create a programming language
2. introduction
DSLs for fun and profit by Jukka Välimaa
Yacc (yet another compiler compiler)
Ch4c.ppt
6 attributed grammars
2. introduction to compiler
llvm-py: Writing Compilers In Python
Writing Parsers and Compilers with PLY
Learning to Generate Pseudo-code from Source Code using Statistical Machine T...
Ad

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Big Data Technologies - Introduction.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
KodekX | Application Modernization Development
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation_ Review paper, used for researhc scholars
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Big Data Technologies - Introduction.pptx
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
cuic standard and advanced reporting.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
KodekX | Application Modernization Development
sap open course for s4hana steps from ECC to s4
Encapsulation_ Review paper, used for researhc scholars

Python Compiler Internals Presentation Slides

  • 1. Python Compiler Internals Thomas Lee Shine Technologies, Melbourne
  • 2. My Contributions to Python try/except/finally syntax for 2.5 AST to bytecode compilation from Python “ optimization” at the AST level
  • 3. What will you get out of this? Compiler development != rocket science. No magic: it's just code.
  • 4. Compiler? Isn't Python interpreted? Well, yes and no.
  • 5. WTF It ... It's Java?
  • 6. Aaanyway Let's screw with the compiler.
  • 7. Erm ... Before we start ... A patch to fix a quirk in the build process. This has been reported on the bug tracker!
  • 8. An overview of the compiler Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
  • 9. New construct: the “unless” statement unless may_give_you_up: print “never gonna give you up...”
  • 10. Semantics of “unless” Works just like “if not” ... if not would_consider_letting_you_down: print “never gonna let you down...”
  • 11. Implementing “unless” Modify the Grammar Change the AST definition Generate bytecode for “unless”
  • 12. So what's first? Modify the Grammar Change the AST definition Generate bytecode for “unless”
  • 13. What does this change affect? Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
  • 14. WTF is a “grammar”? Defines the syntactic structure of the language.
  • 15. Why a tokenizer? Makes parsing easier.
  • 16. So what does a tokenizer do, then? Generates a stream of events for the parser.
  • 17. What does the parser do? Organises tokens into the structure defined by the grammar. This is the “parse tree”.
  • 18. What's next? Modify the Grammar Change the AST definition Generate bytecode for “unless”
  • 19. What's affected by this change? Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
  • 20. What's an AST? Similar to a parse tree, but not bound to the syntax “ Abstract Syntax Tree”
  • 21. Why does Python use an AST? Much easier to operate upon an AST than a parse tree.
  • 22. AST reuse New constructs can sometimes be expressed using existing AST nodes ...
  • 23. AST reuse ... so we could implement “unless” using “if” and “not” AST nodes. But then I wouldn't be able to show you the code generator ... Unless (test, body) = If ( Not (test), body)
  • 24. What's next? Modify the Grammar Change the AST definition Generate bytecode for “unless”
  • 25. What's affected by this change? Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
  • 26. Generating bytecode for “unless” Add a hook for our new AST node Generate bytecode implementing “unless” logic Use basicblocks as labels for jumps
  • 27. What about the rest of the compiler? Tokens Parse Tree AST Bytecode + Data AST + Symtable Tokenizer Parser AST Translation Bytecode Generation Bytecode Optimization Execution Symtable Construction
  • 28. “unless” in action Let's see if this stuff works ...
  • 29. Where to go from here? Use the Source. Don't try to grok it all at once. Don't be afraid to be wrong. Annoy the python-dev mailing list, like I do!