Overview
Programming Languages
- The computer is unique among “machines” (e.g. lever, pulley, etc.) in that it magnifies our mental force rather than our physical force.
- Computers can assist with decision making, model and predict outcomes, etc.
- Programming languages are the mechanism for communicating with and commanding the only tool available that magnifies your mind.
Course Structure
- Course has theoretical and practical aspects
- Need both in programming languages
- Reading: both theoretical and practical
- Review sets and Exams: theory
- Programming assignments: practice
How are Languages Implemented?
- Two major strategies:
- Interpreters - take source code and run it
- Compilers - translate source code, run result
- Interpreters run programs “as is” with little or no preprocessing
- Compilers to extensive preprocessing
(Short) History of High-Level Languages
- 1953 IBM develops the 701 “Defense Calculator”
- All programming done in assembly
- Problem: software costs exceeded hardware costs
FORTRAN I
- 1954 IBM develops the 704
- John Backus
- Idea: translate high-level code to assembly
- Many thought this was impossible
- 1954 - 1957 Fortran I project
- By 1958, greater than 50% of all software is in FORTRAN
- Cut development time dramatically (2 weeks \(\rightarrow\) 2 hours)
FORTRAN I
- The first compiler
- Produced code almost as good as hand-written assembly
- Huge impact on computer science
- Led to an enormous body of theoretical work
- Modern compilers keep the outlines of FORTRAN I
Interpreter Phases
- Lexical Analysis
- Parsing
- Semantic Analysis
- Optimization (optional)
- Interpret the program
Compiler Phases
- Lexical Analysis
- Parsing
- Semantic Analysis
- Optimization (optional)
- Generate machine code
- Run the machine code
Lexical Analysis (English)
- First step: recognize words
- smallest unit above letters
- Example: “This is a sentence.”
- Captial “T” (start of sentence symbol)
- Space (word separator)
- Period (end of sentence symbol)
Lexical Analysis
- Lexical Analysis is not trivial.
- Consider: “How d’you break”this" up?"
- Programming languages are typically more cryptic than English
*p->f ++ -.12345e-6
- Lexical analyzer divides program text into “words” or tokens
Parsing (English)
- Once words are understood, the next step is to understand sentence structure
- Parsing = diagramming sentences
- The diagram is a tree
- Often annotated with additional information
Diagramming a Sentence
Parsing Programs
- Parsing program expressions is the same
- Consider:
if x == y then z = 1; else z = 2;
Diagrammed:
Semantic Analysis
- Once sentence structure is understood, we can try to understand “meaning”
- But, meaning is too hard for compilers
- Compilers perform limited analysis to catch inconsistencies: reject bad programs early
- Some do more analysis to improve the performance of the program
Semantic Analysis in English
- Example: “Alice said Eve left her book at home.”
- What does “her” refer to?
- Example: “Alice said Alice left her book at home.”
- How many Alices are there?
- Which one left the book?
Semantic Analysis in Programming
Programming languages define strict rules to avoid such ambiguities
Example C++ code:
{ int x = 3; { int x = 4; cout << x; // which x gets printed? } }
Optimization
No strong counterpart in English, but akin to editing (cf. poems, short stories)
- Automatically modify programs so that they
- Run faster
- Use less memory
- In general, conserve some resource
“Compilers” studies optimizations in depth
Code Generation
- Produces assembly code (usually)
- which is then assembled into executables by an assembler
- A translation into another language
- Analogous to human translation
- “Compilers”: produce machine code
Issues
- Compiling and interpreting are almost this simple, but there are many pitfalls
- Example: How are bad programs handled?
- Language design has a big impact on the compiler
- Determines what is easy and hard to compile
- Course theme: trade-offs in language design
Languages Today
- The overall structure of almost every compiler and interpreter follows the above outline
- The proportions have changed since FORTRAN
- Early: lexing, parsing most complex, expensive
- Today: optimization dominates all other phases, lexing and parsing are cheap
Trends in Languages
- Optimization for speed is less interesting. But:
- scientific programs
- advanced processors
- small devices where speed equals longer battery life
- Ideas we will discuss are used for improving code reliability:
- memory safety
- type safety
- automatic memory management
- …
Why Study Programming Languages?
- Prepare for many good jobs
- Increase capacity of expression
- Improve understanding of program behavior
- Increase ability to learn new languages
- Learn to build a large and reliable system
- Computers are the only tools that increase cognitive power, so learn to
What Will You Do In This Class?
- Reading
- Learn about different kinds of languages
- Imperative vs. Functional vs. Object-Oriented
- Static typing vs. Dynamic typing
- etc.
- Gain exposure to new languages
- Write an interpreter