prof fateman cs 164 lecture 371 review: programming languages and compilers cs164 9-10am mwf 10...

24
Prof Fateman CS 164 Lecture 37 1 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Upload: adonis-amber

Post on 15-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 1

Review: Programming Languages and Compilers

CS1649-10AM MWF

10 Evans

Page 2: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 2

Aministrivia

• Sample final questions will be posted• Projects (rather than exams) will probably

be weighted more for grades.• Final Exam is Dec 18, morning• We agreed that it should be closed book,

but 3 pages, (6 sides) of your handwritten notes can be taken to the exam.

Page 3: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 3

Review: Why Should you Study Compilers/ Programming Languages?

• Increased capacity to express ideas• Improved background for choosing

appropriate tools / languages• Increased ability to learn “new” languages

or design new ones• Better understanding of the interchange

between implementation and design• Appreciation of the beauty of relevant

material from CS 61a/b/c CS170, Math 55

Page 4: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 4

What can you do with your new skills?

• Understand/ be a TA/ for CS61a/b/c• Or a reader for CS164• Appreciate CS188 (since you know Lisp

better!)• Take graduate courses CS 264, 263• Take graduate course CS282 (W,F, 9:30)

taught by Prof. Fateman: Algebraic Algorithms: need some math, CS164, Lisp helps

• Get a high-paying job

Page 5: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 5

Recall: The Course Project

• A big project. Written in Common Lisp.• … in several easy parts• Why Lisp?

– Typical JAVA/C++ project code size: 5,000 lines– Typical Lisp code size: 2000 lines.– You are given 1000 lines in “skeletons” either

way, so the ratio is about 4:1 in favor of Lisp– You all know Scheme; CL is a superset.

Page 6: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 6

Important: how did the pieces fit together?

• You must know the form of the input for each module

• You must know the form of the output for each module

• We didn’t ask you to write an interpreter, but we could have…

• What was the point of the interpreter? – Defined the language– Allowed us to TEST TIGER PROGRAMS– Could be used to resolve disputes, ambiguities

Page 7: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 7

Recall: How are Languages Implemented?

• Two major strategies:– Interpreters (simple, general, faster setup)– Compilers (complex, popular, faster runtime)

• Interpreters run programs with only modest processing; often easily portable to many hosts.

• Compilers do extensive preprocessing, some of it to target specific machine architectures.

Page 8: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 8

Interpreters and Compilers Both:

• Read in “source code”• Construct some model of the program• Provide error messages• Adhere to some “execution model” of the

programming language.

Page 9: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 9

Language Implementations

• Batch compilation systems dominate many production environments; Optimizes for fast runtime.

• Some languages are primarily interpreted– Java bytecode

• Lisp provides both– Interpreter for fast development/debugging– Typechecking compiler for faster running

Page 10: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 10

The Structure of a Typical Compiler

1. Lexical Analysis2. Parsing3. Semantic Analysis4. Optimization5. Code Generation

Page 11: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 11

Lexical Analysis (The “lexer” or “scanner”)

• First step: recognize words.– Smallest unit above letters– Some relation to finite state machines and regular

expressions. (We could still ask questions about these on the final)

let type tt={foo:int,bar:int} var xx:tt:=nil in 3 end

(let let (1 . 3)) (type type (1 . 8)) (id tt (1 . 11)) (= = (1 . 12)) ({ { (1 . 13)) (id foo (1 . 16)) (|:| |:| (1 . 17)) (id int (1 . 20)) (|,| |,| (1 . 21)) (id bar (1 . 24)) (|:| |:| (1 .

25)) (id int (1 . 28)) (} } (1 . 29)) …

Page 12: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 12

Parsing

• Once words are understood, the next step is to understand sentence structure as we saw in CS61a

• Parsing = producing AST.– 3 ways

• Recursive descent, seat of the pants programming• LL (also top down parsing), more systematic• LR (bottom up) especially LALR

Page 13: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 13

Typechecking = Static Semantic Analysis

• Scramble over the tree– Build up a symbol table

• Structured in lexical levels• Observe all the details• Maybe make some changes. E.g. X.foo x[1]

Page 14: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 14

Interpretation = Dynamic (Runtime) Analysis

• Scramble over the tree– Execute it

Page 15: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 15

OR…Compiling = code generation

• Scramble over the tree– Expand pieces of the AST into straight line

sequences of Assembly code

Page 16: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 16

then…

• Assemble the generated code• Run the “binary” code on a computer

– Simulated computer – Actual computer (think of it as a hardware

simulation of the “architecture” which is, after all, a formal system.)

Page 17: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 17

then…

• Celebrate if your code worked.

Page 18: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 18

What else? Other ways of thinking about Programming Languages

• (We did not do…) prolog• The Lambda calculus• Macro expansion• Object-orientation

Page 19: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 19

Is there more to this subject?

– Lots more. And its not just about programming, but about user interfaces, theorem proving, mathematics, (also politics, standards, economics).

– At least 3 graduate courses here (more like 5 or 6 if you count “closely related”)

– Many books (not counting “Java for dummies” etc.)

– Magazines and Journals– Just a few web pages

Page 20: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 20

Page 21: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 21

Time for questions?

Page 22: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 22

I hope you found this course challenging & fun.

– Good luck on all your finals– Have a good vacation

Page 23: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 23

Page 24: Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS164 9-10AM MWF 10 Evans

Prof Fateman CS 164 Lecture 37 24