Lecture 26 Special Topics 1 (Compilers and CPython Internals)
- Questions
- Review
- Levels of Languages
- High-level Language
- Assembly Language
- Machine Language
- Programming Languages
- Machine languages
- fixed set of instructions invoke operations implemented by the circuitry of the CPU
- Operations refer to specific hardware memory addresses, no abstraction
- High level languages
- Abstraction
- Machine languages
- Compilers
- translate source code into machine code so that the machine code can be distributed and run repeatedly
- Interpreters
- run source code directly producing an output/value, without first compiling it into machine code
- Levels of Languages
- Understanding Source Code
- parser must be written to understand that source code
- parser creates an Abstract Syntax Tree (AST)
- AST is passed through an evaluator
- Parsing
- Takes in text and returns an expression that represents the text in a tree-like structure
- Lexical analysis
- Tells us if it accept valid characters
- such as python new lines
- converts input text into a list of tokens
- Each token represents the smallest unit of information
- Syntactic analysis
- With the tokens, creating a tree for evaluating
- identifies the hierarchical structure of an expression
- Symbols can be nested
- BNF
- Backaus-Naur Form is a scheme designed specifically for describing the syntax of programming languages using context-free-grammars
- CFG can be parsed statement by statement without needing prior context
- In python, to evaluate a line, we don’t really need to know the lines before it
- BNF can tell us how to make the AST
- Backaus-Naur Form is a scheme designed specifically for describing the syntax of programming languages using context-free-grammars
- AST
- represents the hierarchical structure of formal languages
- They
- are unambiguous
- can be annotated
- can hold additional information about code
- are typically built by the parser
- Parsing Python
- How does the program know that we only used one line of code?
- Inspect - module that has functions to get information about live objects such as classes, functions, frames, etc.
- Ast - module to help process trees of the Python abstract syntax grammar
- Dangling Else (Variation)
- What does this evaluate to?
-
1 if 2 else 3 if False else 5
- 1
-
- What does this evaluate to?
- How does the program know that we only used one line of code?
- Interpreted vs Compiled
- Who’s interpreting who?
- Big snek
- Our Python is interpreted by C
- Who’s interpreting who?
- CPython Internals
- runs byte code directly producing an output/value but first compiles source code into byte code
- Compiled AST into byte code, then interpret Python
- Generating Byte Code
- Dis module
- disassembling python code in Python bytecode
- Byte code compiler can automatically multiply before assigning value to variable
- Dis module
- Stacks
- Stack - data strcuture for storing and retrieving values. Can only retrieve the most recently added item
- Push - adds an item to the top
- Pop - removes an item at the top and returns it
- Peek - looks at the item at the top without removing it
- Stack Machine
- a processor or virtual machine that computes by modifying values in a stack
- Operator - combines the top two values in the stack and then pushes the result 3.
- Stack - data strcuture for storing and retrieving values. Can only retrieve the most recently added item