www.nand2tetris.org
Building a Modern Computer From First Principles
Compiler I: Syntax Analysis
Course map
Assembler Chapter 6
H.L. Language
&
Operating Sys.
abstract interface
Compiler
Chapters 10 - 11
VM Translator
Chapters 7 - 8
Computer Architecture
Chapters 4 - 5
Gate Logic
Chapters 1 - 3
Electrical
Engineering
Physics Virtual
Machine
abstract interface
Software hierarchy
Assembly Language
abstract interface
Hardware hierarchy
Machine Language
abstract interface
Hardware Platform
abstract interface
Chips &
Logic Gates
abstract interface
Human Thought
Abstract design
Chapters 9, 12
Motivation: Why study about compilers?
Because Compilers …
Are an essential part of applied computer science
Are very relevant to computational linguistics
Are implemented using classical programming techniques
Employ important software engineering principles
Train you in developing software for transforming one structure to another (programs, files, transactions, …)
Train you to think in terms of ”description languages”.
Parsing files of some complex syntax is very common in many
applications.
The big picture
. . .
RISC machine language
Hack machine language CISC
machine
language
. . .
a high-levelwritten inlanguage
. . .
HW lectures (Projects
1-6) Intermediate code
VM implementation
over CISC platforms
VM imp.
over RISC platforms
VM imp.
over the Hack platform
VM emulator
VM lectures (Projects
7-8) Some Other
language
Jack language
Some
compiler Some Other compiler
Jack compiler
. . .
Some
language
. . .
Compiler lectures (Projects
10,11)
Modern compilers are two-tiered:
Front-end:
from high-level language to some intermediate language
Back-end:
from the
intermediate
language to
binary code.
Compiler architecture (front end)
. . .
Intermediate code
RISC machine language
Hack machine language CISC
machine
language . . . a high-levelwritten in
language
. . .
VM implementation
over CISC platforms
VM imp.
over RISC platforms
VM imp.
over the Hack platform VM emulator Some Other
language Jack language
Some compiler Some Other
compiler Jack compiler
. . .
Some language . . .
Syntax analysis: understanding the structure of the source code
Tokenizing: creating a stream of “atoms”
Parsing: matching the atom stream with the language grammar
XML output = one way to demonstrate that the syntax analyzer works
(Chapter 11)
Jack Program
Toke-
nizer Parser
Code Gene -ration
Syntax Analyzer Jack Compiler
VM code XML code
(Chapter 10)
(source) (target)
scanner