Pylayers is an elegant, unified and comprehensive educational system that teaches the fundamentals of software compilation, interpretation and execution using Python. It is designed for students, teachers and ambitious computer programmers. Compilation converts source code to intermediate code, then to assembly code, and finally, to machine code. These compilation stages can be pictorially represented by layers:
Computers only execute machine code. Interpreters can execute ("interpret") intermediate code. Pylayers teaches about these and numerous other related topics, all with working Python code, including: intermediate code generators, assembly code generators, machine code generators (assemblers), grammars, tokenizers, tokens, parsers, productions, abstract syntax trees, registers, instruction sets and more. An extensively documented compiler, interpreter and computer, all implemented in Python, are provided. See all Pylayers software here.
I hope you find Pylayers instructive. I the author, Dr. Christian Seberino, welcome all questions, comments and suggestions. You can reach me by email here.
Here are all the layers in the compilation or transformation of source code into machine code. Source code is typically the only layer humans see. Machine code is the only layer computers execute:
Layer | Example | Description |
---|---|---|
source code |
x = 1 + 2 print(x) |
The source code layer is typically the only layer the human sees. Here the source code is in a high level Pythonic language. |
abstract syntax tree |
('program', ('statement', ('stat_semicol', ('semicol_base', ('expression', ('exp_log_and', ('exp_log_not', ('exp_comp', ('exp_bit_or', ('exp_bit_xor', ('exp_bit_and', ('exp_shift', ('exp_sum', ('exp_prod', ('exp_pdbn', ('exp_pow', ('exp_inv_elems', ('exp_base', ('VARIABLE', 'x'))))))))))))))), ('assign_op', ('EQUALS', '=')), ('expression', ('exp_log_and', ('exp_log_not', ('exp_comp', ('exp_bit_or', ('exp_bit_xor', ('exp_bit_and', ('exp_shift', ('exp_sum', ('exp_prod', ('exp_pdbn', ('exp_pow', ('exp_inv_elems', ('exp_base', ('NATURAL', '1')))))), ('PLUS', '+'), ('exp_prod', ('exp_pdbn', ('exp_pow', ('exp_inv_elems', ('exp_base', ('NATURAL', '2')))))))))))))))), ('NEWLINE', '\n'))), ('statement', ('stat_semicol', ('semicol_base', ('expression', ('exp_log_and', ('exp_log_not', ('exp_comp', ('exp_bit_or', ('exp_bit_xor', ('exp_bit_and', ('exp_shift', ('exp_sum', ('exp_prod', ('exp_pdbn', ('exp_pow', ('exp_inv_elems', ('exp_base', ('VARIABLE', 'print')), ('L_PAREN', '('), ('expression', ('exp_log_and', ('exp_log_not', ('exp_comp', ('exp_bit_or', ('exp_bit_xor', ('exp_bit_and', ('exp_shift', ('exp_sum', ('exp_prod', ('exp_pdbn', ('exp_pow', ('exp_inv_elems', ('exp_base', ('VARIABLE', 'x'))))))))))))))), ('R_PAREN', ')'))))))))))))))), ('NEWLINE', '\n')))) |
The abstract syntax tree layer is created by parsing the source code with a parser. For more info click here. |
intermediate code |
(def print (func (e) (list "__PRINT__" e))) (def |
The intermediate code layer is formed from the abstract syntax tree layer. It is a program in a different programming language with less syntax. For more info click here. |
assembly code | The screen goes here. | The assembly code layer is created by transforming the intermediate code. |
machine code | The screen goes here. | The machine code layer is created by transforming the assembly code. |