Académique Documents
Professionnel Documents
Culture Documents
Introduction to Compilers
Vijay Ganesh
Dates/Time: Mon/Fri, 10 11:20 AM
Classroom: EIT 1015
Staff and Website
Instructor: Vijay Ganesh
Course website
https://ece.uwaterloo.ca/~vganesh/TEACHING/S2017/ECE351/index.html
2
Textbook and course material
3
Course Structure: lectures
The course has 5 modules (sets of 4-5 lectures) + labs + assignments + exams
There are extra lecture slots on Fridays from 12:30 1:20 PM. Also Tutorials
from 4:30 5:20 PM on Thursdays
I will use some of these extra slots and tutorials. Announcements will be made
one week in advance.
The Labs
8 graded labs + lab0 (not graded)
One lab per week, due every Thursday11:59 PM, and released every
Thursday (Except Lab0, which is due May 7th 11:59 PM)
Lab1 is due May 11th 11:59 PM,
Writing a compiler for a hardware description language
All labs must be done individually, Collaboration is okay
Cheating is not okay. Please name your collaborators in your submissions
5
Final exam (closed book, notes, computer, phone,)
Grade Distribution
7
What is a programming language and how are they
implemented?
Programming languages are powerful mathematical constructs that
enable one to program a computer
Semantics
What do these constructs mean
Assumes or enables a model of computation, e.g., Turing
machines or mathematical functions
Denotational, operational, and axiomatic
8
Types of programming languages
Declarative: You specify the what of the computation. The system
will figure out the how. E.g. SQL, Matlab,
Imperative: You specify both the what and the how of the
computation. C/C++/Java
You can add features like object orientation to any of the above
category of languages
9
Programming language design
Not easy
Languages must
Make it easier to program
Increase programmer productivity
Automate routine tasks
Enable programmers to produce efficient (time, space,
power,) code
Enable secure and correct construction
10
How are Languages Implemented?
Major strategies:
Interpreters (older)
Compilers (newer)
Mixed: JIT compilation
In the process of translation, the compiler checks the input program for
correctness (up to a point)
Optimizes the code (up to a point)
Links to code libraries
Generates what is called a binary or executable
Input to the compiler must be logically equivalent to the output of the compiler
13
History of High-Level Languages
Problem
Software costs exceeded
hardware costs!
14
FORTRAN I
Idea
Translate high-level code to
assembly
15
FORTRAN I (Cont.)
1954-7
FORTRAN I project
1958
>50% of all software is in
FORTRAN
16
FORTRAN I
17
The Structure (Phases) of a Compiler
1. Lexical Analysis
2. Parsing
3. Semantic Analysis
4. Optimization
5. Code Generation
18
The Structure (Phases) of a Compiler
Semantic
Input Lexical Tokens
Parser
AST Analysis (Type
Program Analyzer Checking,)
Three Intermediate
Address Representation (IR)
Output Code Format Code
Program Generation Optimizations
19
Lexical Analysis
This is a sentence.
20
And More Lexical Analysis
Units:
Keywords
Variables
Operators
Constants
Punctuations
White spaces
21
Parsing
22
Diagramming a Sentence
subject object
sentence
23
Parsing Programs
Semantic
Input Lexical Tokens
Parser
AST Analysis (Type
Program Analyzer Checking,)
Three Intermediate
Address Representation (IR)
Output Code Format Code
Program Generation Optimizations
25
Semantic Analysis
26
Semantic Analysis in English
Example:
Jack said Jerry left his assignment at home.
What does his refer to? Jack or Jerry?
Even worse:
Jack said Jack left his assignment at home?
How many Jacks are there?
Which one left the assignment?
27
Semantic Analysis in Programming
Programming languages {
define strict rules to int Jack = 3;
avoid such ambiguities, {
e.g., variable binding or
scoping rules int Jack = 4;
cout << Jack;
This C++ code prints }
4; the inner definition }
is used
28
More Semantic Analysis
Example:
Jack left her homework at home.
Semantic
Input Lexical Tokens
Parser
AST Analysis (Type
Program Analyzer Checking,)
Three Intermediate
Address Representation (IR)
Output Code Format Code
Program Generation Optimizations
30
Optimization
31
Optimization Example
X = Y * 0 is the same as X = 0
32
The Structure (Phases) of a Compiler
Semantic
Input Lexical Tokens
Parser
AST Analysis (Type
Program Analyzer Checking,)
Three Intermediate
Address Representation (IR)
Output Code Format Code
Program Generation Optimizations
33
Code Generation
Hardware retargetability
34
Intermediate Languages
35
Intermediate Languages (Cont.)
36
Compilers Today
37
Research Topics in Compilers Today
Module 2: Parsing
Abstract syntax trees
Ambiguity in grammars
Top-down parsers: recursive-descent, LL(k)
Bottom-up parsers
41
Next Lecture: Formal Language Theory, Basics of
Computability and Complexity
42