Lecture 1

52 downloads 93084 Views 154KB Size Report
Aiken CS 143 Lecture 1. 4. Text. • The Purple Dragon Book. • Aho, Lam, Sethi &. Ullman. • Not required. – But a useful reference. Prof. Aiken CS 143 Lecture 1. 5.
Administrivia • Everything is on the class Web site

http://www.stanford.edu/class/cs143/

• Syllabus is on-line, of course

Compilers

– Assignment dates will not change – Midterm • Thursday, 10/21 • in class

– Final

CS143 11:00-12:15TT B03 Gates

• Wednesday, 12/8 • 7-10pm

• Communication

– Use newsgroup, email, phone, office hours – But definitely prefer the newsgroup!

Prof. Aiken CS 143 Lecture 1

Prof. Aiken CS 143 Lecture 1

1

Staff

Text

• Instructor

• The Purple Dragon Book

– Alex Aiken

• Aho, Lam, Sethi & Ullman

• TAs – – – –

2

Peter Boonstoppel Isil Dillig Tom Dillig Steven Elia

• Not required – But a useful reference

• Office hours, contact info on 143 web site Prof. Aiken CS 143 Lecture 1

3

Prof. Aiken CS 143 Lecture 1

Course Structure

Academic Honesty

• Course has theoretical and practical aspects

• Don’t use work from uncited sources

• Need both in programming languages!

• We use plagiarism detection software

4

– Including old code – many cases in past offerings

• Written assignments = theory – Class hand-in

PLAGIARISM

• Programming assignments = practice – Electronic hand-in

Prof. Aiken CS 143 Lecture 1

5

Prof. Aiken CS 143 Lecture 1

6

1

The Course Project

How are Languages Implemented?

• A big project

• Two major strategies: – Interpreters (older) – Compilers (newer)

• … in 4 easy parts

• Interpreters run programs “as is”

• Start early!

– Little or no preprocessing

• Compilers do extensive preprocessing

Prof. Aiken CS 143 Lecture 1

7

Prof. Aiken CS 143 Lecture 1

Language Implementations

History of High-Level Languages

• Batch compilation systems dominate

• 1954 IBM develops the 704

– gcc

8

– Successor to the 701

• Some languages are primarily interpreted

• Problem

– Java bytecode

– Software costs exceeded hardware costs!

• Some environments (Lisp) provide both

• All programming done in assembly

– Interpreter for development – Compiler for production Prof. Aiken CS 143 Lecture 1

9

Prof. Aiken CS 143 Lecture 1

The Solution

FORTRAN I

• Enter “Speedcoding”

• Enter John Backus

• An interpreter

• Idea

10

– Translate high-level code to assembly

• Ran 10-20 times slower than hand-written assembly

– Many thought this impossible – Had already failed in other projects

Prof. Aiken CS 143 Lecture 1

11

Prof. Aiken CS 143 Lecture 1

12

2

FORTRAN I (Cont.)

FORTRAN I

• 1954-7

• The first compiler

– FORTRAN I project

– Huge impact on computer science

• 1958

• Led to an enormous body of theoretical work

– >50% of all software is in FORTRAN

• Modern compilers preserve the outlines of FORTRAN I

• Development time halved

Prof. Aiken CS 143 Lecture 1

13

Prof. Aiken CS 143 Lecture 1

The Structure of a Compiler

Lexical Analysis

1. 2. 3. 4. 5.

• First step: recognize words.

Lexical Analysis Parsing Semantic Analysis Optimization Code Generation

14

– Smallest unit above letters

This is a sentence.

The first 3, at least, can be understood by analogy to how humans comprehend English. Prof. Aiken CS 143 Lecture 1

15

Prof. Aiken CS 143 Lecture 1

More Lexical Analysis

And More Lexical Analysis

• Lexical analysis is not trivial. Consider: ist his ase nte nce

• Lexical analyzer divides program text into “words” or “tokens” If x == y then z = 1; else z = 2;

16

• Units:

Prof. Aiken CS 143 Lecture 1

17

Prof. Aiken CS 143 Lecture 1

18

3

Parsing

Diagramming a Sentence

• Once words are understood, the next step is to understand sentence structure

This article

• Parsing = Diagramming Sentences

line

is

a

longer

noun verb article adjective

sentence noun

– The diagram is a tree

subject

object sentence

Prof. Aiken CS 143 Lecture 1

19

Prof. Aiken CS 143 Lecture 1

20

Parsing Programs

Semantic Analysis

• Parsing program expressions is the same • Consider: If x == y then z = 1; else z = 2; • Diagrammed: x == y z 1 z 2

• Once sentence structure is understood, we can try to understand “meaning”

relation

assign

assign

predicate

then-stmt

else-stmt

– But meaning is too hard for compilers

• Compilers perform limited analysis to catch inconsistencies

if-then-else Prof. Aiken CS 143 Lecture 1

21

Prof. Aiken CS 143 Lecture 1

Semantic Analysis in English

Semantic Analysis in Programming

• Example: Jack said Jerry left his assignment at home. What does “his” refer to? Jack or Jerry?

• Programming languages define strict rules to avoid such ambiguities

• Even worse: Jack said Jack left his assignment at home? How many Jacks are there? Which one left the assignment?

• This C++ code prints “4”; the inner definition is used

Prof. Aiken CS 143 Lecture 1

23

22

{ int Jack = 3; { int Jack = 4; cout