Lecture 001

Programming language:

Histroy: we used to write byte code. But we developed "speedcode" as first compiler and then FORTRAN I by John Backus. Before 1958, 50% of code was written in FORTRAN.

Five Phasis:

  1. Lexical Analysis: divide program text into tokens.
  2. Parsing: diagramming sentence by generating parse tree
  3. Semantic Analysis: understand "meaning" (ie. catch language inconsistencies and type check)
  4. Optimization: make program run faster, reduce power, network, memory by doing
  5. Code Generation: language translation

For FORTAN, the semantic analysis part is small and the rest is evenly distributed accross different phasis.

For today's compiler, we have big semantic alalysis phase and very large optimization phase. Other phasis are small.

Lexical Analysis

Lexical Analysis: divide program text into tokens.

Parsing

Pase Tree Example

Pase Tree Example

Semantic Analysis

Inconsistency example:

Jack said Jerry left his assignment at home

// Do you know what "his" is refering to?
// Answer: No

This is a problem in semantic analysis

To avoid this issue we use scope:

{
  int Jack = 3;
  {
    int Jack = 4;
    cout << Jack; // It will print 4
  }
  cout << Jack; // It will print 3
}

Optimization

Consider the following code

X = Y * 0

While this could be optimized to

X = 0

However, this optimization does not always hold. It only holds for integers, but invalid for floating point since NaN * 0 = NaN != 0

Code Generation

Generate lower level code.

Table of Content