L1 Intro to Compilers
Intro to Compilers
Compilers
Interpreters
FORTRAN 1
- The first Compiler
- Huge impact o computer science.
- Led to an enormous body of theoretical work
- Modern compilers preserve the outline of FORTRAN 1
Structure of FORTRAN 1
- Lexical Analysis(词法分析)
- Parsing (语法分析)
- Semantic Analysis (语义分析)
- Optimization(优化)
- Code Generation
First step: recognize words.
smallest unit above letters
This is a sentence.
-
Lexical analysics divides program text into "words" or "tokens"
if x == y then z = 1; else z = 2;
-
Parsing = Diagrmming Sentences.
- The diagram is a tree.
- Once sentence structure is understood, we can try to understand "meaning"
- This is too hard
-
Compilers perform limited semantic analysis to catch inconsistencies.
-
Expmple: Jack said Jerry left his assignment at home.
-
Even worse: Jack said Jack left his assignment at home?
-
Question? Which one "his" is referring to whether it's jack or it's Jerry.
-
Programming languages define strict rules to avoid such ambiguities.
int main { int jack = 3; { int jack = 4; cout << jack; } }
Compilers perform many semantic checks besides variable bindings.
- Example: Jack left her homework at home.
- A "type mismatch" between her and jack; we know they are different people.
Optimization has no strong counterpart in English.
- But a little bit like editing.
Automatically modify programs so that they
- Run faster
- Use less memory
X = Y * 0 is the same as X = 0
- NO!
- valid for integers
- invalid for floating point (NaN)
- NaN, Standing for Not a Number, is a numeric data type value representing an undefined or unrepresentable value.
- NaN * 0 = NaN
Produces assembly code (usually)
A translation into another language
-
Analogous to human translation
-
The overall structure of almost every compiler adheres to our outline
-
The proportions has changed since FORTRAN