Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
replacing
import with
accio in
CPython
>>> import sys!
>>> accio sys!
SyntaxError
>>> import sys!
>>> accio sys!
SyntaxError
>>> import sys!
>>> accio sys!
SyntaxError
>>> accio sys!
>>> import sys!
SyntaxError
>>> accio sys!
>>> import sys!
SyntaxError
>>> accio sys!
>>> import sys!
SyntaxError
why?
we’ll learn:!
what is a
statement
we’ll learn:!
what is a
SyntaxError
we’ll learn:!
how does
Python know
the difference?
we’ll learn:!
bootstrapping
!
replacing
import with
accio
import is a
statement
statements
are defined in
the Grammar!
file
import_stmt: !
import_name | !
import_from
import_name: !
‘import’ !
dotted_as_names
first attempt
import_name: !
‘accio’ !
dotted_as_names
$ make
import sys!
^!
SyntaxError:
invalid syntax
oops
second
attempt
$ sed ’s/
import/accio’
$ make
accio sys!
^!
SyntaxError:
invalid syntax
bootstrapping
!
third attempt
import_name: !
‘import’ !
dotted_as_names!
| ‘accio’ !
dotted_as_names
$ make
$PATH
import_name: !
‘accio’ !
dotted_as_names
$ sed ’s/
import/accio’
$ make
>>> accio sys!
>>> import sys!
SyntaxError
>>> accio sys!
>>> import sys!
SyntaxError
>>> accio sys!
>>> import sys!
SyntaxError!
links
• Grammar - https://docs.python.org/2/reference/
grammar.html
• EBNF - http://en.wikipedia.org/wiki/
Extended_Backus...
@amygdalama!
mathamy.com
Upcoming SlideShare
Loading in …5
×

Replacing `import` with `accio` in cpython

828 views

Published on

Replacing `import` with `accio`: Compiling Pythons with Custom Grammar for the sake of a joke!

These are the slides from my 03/25/14 talk at NYC Hack and Tell.

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Replacing `import` with `accio` in cpython

  1. 1. replacing import with accio in CPython
  2. 2. >>> import sys! >>> accio sys! SyntaxError
  3. 3. >>> import sys! >>> accio sys! SyntaxError
  4. 4. >>> import sys! >>> accio sys! SyntaxError
  5. 5. >>> accio sys! >>> import sys! SyntaxError
  6. 6. >>> accio sys! >>> import sys! SyntaxError
  7. 7. >>> accio sys! >>> import sys! SyntaxError
  8. 8. why?
  9. 9. we’ll learn:! what is a statement
  10. 10. we’ll learn:! what is a SyntaxError
  11. 11. we’ll learn:! how does Python know the difference?
  12. 12. we’ll learn:! bootstrapping !
  13. 13. replacing import with accio
  14. 14. import is a statement
  15. 15. statements are defined in the Grammar! file
  16. 16. import_stmt: ! import_name | ! import_from
  17. 17. import_name: ! ‘import’ ! dotted_as_names
  18. 18. first attempt
  19. 19. import_name: ! ‘accio’ ! dotted_as_names
  20. 20. $ make
  21. 21. import sys! ^! SyntaxError: invalid syntax
  22. 22. oops
  23. 23. second attempt
  24. 24. $ sed ’s/ import/accio’
  25. 25. $ make
  26. 26. accio sys! ^! SyntaxError: invalid syntax
  27. 27. bootstrapping !
  28. 28. third attempt
  29. 29. import_name: ! ‘import’ ! dotted_as_names! | ‘accio’ ! dotted_as_names
  30. 30. $ make
  31. 31. $PATH
  32. 32. import_name: ! ‘accio’ ! dotted_as_names
  33. 33. $ sed ’s/ import/accio’
  34. 34. $ make
  35. 35. >>> accio sys! >>> import sys! SyntaxError
  36. 36. >>> accio sys! >>> import sys! SyntaxError
  37. 37. >>> accio sys! >>> import sys! SyntaxError!
  38. 38. links • Grammar - https://docs.python.org/2/reference/ grammar.html • EBNF - http://en.wikipedia.org/wiki/ Extended_Backus%E2%80%93Naur_Form • Adding a statement - http:// eli.thegreenplace.net/2010/06/30/python- internals-adding-a-new-statement-to-python/
  39. 39. @amygdalama! mathamy.com

×