Python Compiler Internals Presentation Slides
Upcoming SlideShare
Loading in...5
×
 

Python Compiler Internals Presentation Slides

on

  • 7,188 views

My OSDC 2008 presentation on the internals of the Python programming language.

My OSDC 2008 presentation on the internals of the Python programming language.

Statistics

Views

Total Views
7,188
Views on SlideShare
7,150
Embed Views
38

Actions

Likes
9
Downloads
118
Comments
1

7 Embeds 38

http://www.slideshare.net 24
http://www.linkedin.com 5
https://www.linkedin.com 5
http://translate.googleusercontent.com 1
http://coderwall.com 1
http://www.slashdocs.com 1
http://www.docseek.net 1
More...

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • hey there,could you please mail this across to me,it will truly help me for my function.thank you very much.
    Sharika
    http://financeadded.com http://traveltreble.com
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Python Compiler Internals Presentation Slides Python Compiler Internals Presentation Slides Presentation Transcript

    • 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?