SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Hugging Abstract Syntax Trees:
A Pythonic Love Story
<3
Tom Lee
Shine Technologies
About Me <3
● Consultant for Shine Technologies
● Software consultancy with offices in Melbourne and Brisbane
● http://www.shinetech.com
● Python contributor
● try/except/finally
● compile() ASTs
● … and other bits and bobs.
● Would-be compiler geek.
Overview <3
● What are we discussing?
● Python Compiler Architecture
● Abstract Syntax Trees (ASTs)
● Abstract Syntax Trees in CPython
● Writing Compilers for the Python VM
● Benefits
● Demo?
What are we discussing? <3
● A bird's-eye view of bytecode compilers.
● Features introduced in Python 2.5 & 2.6.
● Abstract Syntax Trees and their use in Python.
● What they are.
● Why they're cool!
● Implement your own language in Python for the Python VM.
Python Compiler Architecture <3
Abstract Syntax Trees <3
● Data structure.
● Constructed by a parser.
● Consumed by a code generator.
● In-memory, logical representation of a program.
● Syntax and bytecode independent.
● Contrast to parse trees.
Abstract Syntax Trees in CPython <3
● Python 2.5: Used internally by the Python compiler.
● API access from Python, but limited utility.
● Python 2.6: compile() ASTs to bytecode.
● Parse Python source to an AST.
● Analyze & manipulate ASTs in-memory.
– Optimisation
– Static analysis
● Generate and execute bytecode from an AST.
● What if you construct your own ASTs?
Writing Compilers for the Python VM <3
● Write a scanner (in Python!), generate tokens.
● Write a parser (in Python!), construct an AST.
● Use Python's ast module.
● Use compile() to construct a code object from the AST.
● Use exec/eval() to run the code object on the Python VM.
Benefits <3
● Write compilers in Python!
● ASTs more easily manipulated than generated source.
● Optimise code at the AST level.
● Use Python modules from within your language.
● OR Compile your language to .pyc files and call 'em from
Python.
Proof of Concept: Viking <3
http://github.com/thomaslee/viking-poc
About Viking <3
● Think of it as Python with braces...
● A front end for the Python bytecode compiler.
● Compiles source to an equivalent using Python's AST.
● Written over the course of a few hours.
● <400 LOPC (including spaces and comments).
● Quick, dirty, horrible, incomplete...
● … but surprisingly powerful as a demonstration.
Demo? <3
http://www.funnyhub.com/pictures/pages/snake-bites-face.html
Summary <3
● ASTs are cool!
● Python + ASTs + compile() = powerful language prototyping.
● Practical? Eh.
Questions? <3
Tom Lee
http://tomlee.co
Shine Technologies
http://www.shinetech.com
thomas.lee@shinetech.com

Weitere ähnliche Inhalte

Was ist angesagt?

Python debuggers slides
Python debuggers slidesPython debuggers slides
Python debuggers slides
mattboehm
 
Proxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::StaticperlProxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::Staticperl
nohuhu
 
a quick Introduction to PyPy
a quick Introduction to PyPya quick Introduction to PyPy
a quick Introduction to PyPy
Kai Aras
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
Piyush Katariya
 
Python for pentesters
Python for pentestersPython for pentesters
Python for pentesters
Rashid feroz
 

Was ist angesagt? (18)

Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
Python debuggers slides
Python debuggers slidesPython debuggers slides
Python debuggers slides
 
PHP Internals
PHP InternalsPHP Internals
PHP Internals
 
Proxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::StaticperlProxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::Staticperl
 
Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...
 
Introduction to protocol buffer
Introduction to protocol bufferIntroduction to protocol buffer
Introduction to protocol buffer
 
An Introduction to PyPy
An Introduction to PyPyAn Introduction to PyPy
An Introduction to PyPy
 
a quick Introduction to PyPy
a quick Introduction to PyPya quick Introduction to PyPy
a quick Introduction to PyPy
 
Ruby formatters
Ruby formattersRuby formatters
Ruby formatters
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
Introduction to Go programming
Introduction to Go programmingIntroduction to Go programming
Introduction to Go programming
 
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
 
.Net Architecture and Performance Tuning
.Net Architecture and Performance Tuning.Net Architecture and Performance Tuning
.Net Architecture and Performance Tuning
 
Python for pentesters
Python for pentestersPython for pentesters
Python for pentesters
 
PyPy
PyPyPyPy
PyPy
 
Python and web driver
Python and web driverPython and web driver
Python and web driver
 
Introduction to Kotlin coroutines
Introduction to Kotlin coroutinesIntroduction to Kotlin coroutines
Introduction to Kotlin coroutines
 

Andere mochten auch

AST - the only true tool for building JavaScript
AST - the only true tool for building JavaScriptAST - the only true tool for building JavaScript
AST - the only true tool for building JavaScript
Ingvar Stepanyan
 

Andere mochten auch (13)

Ast transformation
Ast transformationAst transformation
Ast transformation
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1
 
Don't Be Afraid of Abstract Syntax Trees
Don't Be Afraid of Abstract Syntax TreesDon't Be Afraid of Abstract Syntax Trees
Don't Be Afraid of Abstract Syntax Trees
 
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
 
AST - the only true tool for building JavaScript
AST - the only true tool for building JavaScriptAST - the only true tool for building JavaScript
AST - the only true tool for building JavaScript
 
Syntax
SyntaxSyntax
Syntax
 
Compiler vs interpreter
Compiler vs interpreterCompiler vs interpreter
Compiler vs interpreter
 
Language translator
Language translatorLanguage translator
Language translator
 
Abstract syntax semantic analyze
Abstract syntax semantic analyzeAbstract syntax semantic analyze
Abstract syntax semantic analyze
 
Translators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreterTranslators(Compiler, Assembler) and interpreter
Translators(Compiler, Assembler) and interpreter
 
Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.Compiler vs Interpreter-Compiler design ppt.
Compiler vs Interpreter-Compiler design ppt.
 

Ähnlich wie Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)

Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
gabriellekuruvilla
 
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineTwo-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Yusuke Izawa
 

Ähnlich wie Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010) (20)

Pi Is For Python
Pi Is For PythonPi Is For Python
Pi Is For Python
 
Python_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfPython_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdf
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfPython_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdf
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python_vision_academy notes
Python_vision_academy notes Python_vision_academy notes
Python_vision_academy notes
 
Everything You Need to Know About the Intel® MPI Library
Everything You Need to Know About the Intel® MPI LibraryEverything You Need to Know About the Intel® MPI Library
Everything You Need to Know About the Intel® MPI Library
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
python-ppt.ppt
python-ppt.pptpython-ppt.ppt
python-ppt.ppt
 
Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.
 
Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler language
 
Elasticwulf Pycon Talk
Elasticwulf Pycon TalkElasticwulf Pycon Talk
Elasticwulf Pycon Talk
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OS
 
Introduction to python3.pdf
Introduction to python3.pdfIntroduction to python3.pdf
Introduction to python3.pdf
 
SOSCON 2016 JerryScript
SOSCON 2016 JerryScriptSOSCON 2016 JerryScript
SOSCON 2016 JerryScript
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
 
Python for the C# developer
Python for the C# developerPython for the C# developer
Python for the C# developer
 
Rusty Python
Rusty PythonRusty Python
Rusty Python
 
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineTwo-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One Engine
 
.NET compiler platform codename Roslyn
.NET compiler platform codename Roslyn.NET compiler platform codename Roslyn
.NET compiler platform codename Roslyn
 

Mehr von Tom Lee

Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVM
Tom Lee
 

Mehr von Tom Lee (8)

A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
A(n abridged) tour of the Rust compiler [PDX-Rust March 2014]
 
Inside PHP [OSCON 2012]
Inside PHP [OSCON 2012]Inside PHP [OSCON 2012]
Inside PHP [OSCON 2012]
 
Inside Python [OSCON 2012]
Inside Python [OSCON 2012]Inside Python [OSCON 2012]
Inside Python [OSCON 2012]
 
Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVM
 
Open Source in Big Business [LCA2011 Miniconf]
Open Source in Big Business [LCA2011 Miniconf]Open Source in Big Business [LCA2011 Miniconf]
Open Source in Big Business [LCA2011 Miniconf]
 
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
 
Open Source in Big Business (OSDC 2010)
Open Source in Big Business (OSDC 2010)Open Source in Big Business (OSDC 2010)
Open Source in Big Business (OSDC 2010)
 
Python Compiler Internals Presentation Slides
Python Compiler Internals Presentation SlidesPython Compiler Internals Presentation Slides
Python Compiler Internals Presentation Slides
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 

Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)

  • 1. Hugging Abstract Syntax Trees: A Pythonic Love Story <3 Tom Lee Shine Technologies
  • 2. About Me <3 ● Consultant for Shine Technologies ● Software consultancy with offices in Melbourne and Brisbane ● http://www.shinetech.com ● Python contributor ● try/except/finally ● compile() ASTs ● … and other bits and bobs. ● Would-be compiler geek.
  • 3. Overview <3 ● What are we discussing? ● Python Compiler Architecture ● Abstract Syntax Trees (ASTs) ● Abstract Syntax Trees in CPython ● Writing Compilers for the Python VM ● Benefits ● Demo?
  • 4. What are we discussing? <3 ● A bird's-eye view of bytecode compilers. ● Features introduced in Python 2.5 & 2.6. ● Abstract Syntax Trees and their use in Python. ● What they are. ● Why they're cool! ● Implement your own language in Python for the Python VM.
  • 6. Abstract Syntax Trees <3 ● Data structure. ● Constructed by a parser. ● Consumed by a code generator. ● In-memory, logical representation of a program. ● Syntax and bytecode independent. ● Contrast to parse trees.
  • 7. Abstract Syntax Trees in CPython <3 ● Python 2.5: Used internally by the Python compiler. ● API access from Python, but limited utility. ● Python 2.6: compile() ASTs to bytecode. ● Parse Python source to an AST. ● Analyze & manipulate ASTs in-memory. – Optimisation – Static analysis ● Generate and execute bytecode from an AST. ● What if you construct your own ASTs?
  • 8. Writing Compilers for the Python VM <3 ● Write a scanner (in Python!), generate tokens. ● Write a parser (in Python!), construct an AST. ● Use Python's ast module. ● Use compile() to construct a code object from the AST. ● Use exec/eval() to run the code object on the Python VM.
  • 9. Benefits <3 ● Write compilers in Python! ● ASTs more easily manipulated than generated source. ● Optimise code at the AST level. ● Use Python modules from within your language. ● OR Compile your language to .pyc files and call 'em from Python.
  • 10. Proof of Concept: Viking <3 http://github.com/thomaslee/viking-poc
  • 11. About Viking <3 ● Think of it as Python with braces... ● A front end for the Python bytecode compiler. ● Compiles source to an equivalent using Python's AST. ● Written over the course of a few hours. ● <400 LOPC (including spaces and comments). ● Quick, dirty, horrible, incomplete... ● … but surprisingly powerful as a demonstration.
  • 13. Summary <3 ● ASTs are cool! ● Python + ASTs + compile() = powerful language prototyping. ● Practical? Eh.
  • 14. Questions? <3 Tom Lee http://tomlee.co Shine Technologies http://www.shinetech.com thomas.lee@shinetech.com

Hinweis der Redaktion

  1. TODO:Make the diagram clearer: show the flow of data from scanner to parser to bytecode generator.