CS301 W'99 Lecture Notes
Lecture 1
Programming Languages and Compiler Design

& Implementation& Implementation

Course Goals

$\bullet$ Improve understanding of languages and machines.

$\bullet$ Learn practicalities of translation.

$\bullet$ Learn ``anatomy'' of programming languages.

$\bullet$ Apply computer science theory to practical problems (using tools).

$\bullet$ Do large programming project.


A compiler is a translator from ``high-level'' language to assembly code/object language.

Language L $\longrightarrow$ \fbox{\ \bf TRANSLATOR\ } $\longrightarrow$ Language L'

Examples of translators:

Pascal,C, etc. $$ Compiler $$ Machine Code
Java $$ Compiler $$ Byte Code
Ratfor $$ Preprocessor $$ Fortran
Tex $$ Text Formatter $$ Postscript
SQL $$ DB Optimizer $$ Query plan

We study common features of translators, by building one.

Language Design

We study languages from an implementor's viewpoint.

$\bullet$ How do compilation feasibility and runtime efficiency affect language design?

(There are more ``theoretical'' approaches to studying programming languages, and there are interesting languages that don't compile easily...)

``Von Neumann'' Machine


Key Characteristics

$\bullet$ Sequential control flow + labels + jumps

$\bullet$ Small set of built-in data types and operators (e.g., byte, integer, floating point)

$\bullet$ Flat linear address space.

$\bullet$ Memory hierarchy (registers faster than memory faster than disk).

``High-Level'' Languages

E.g., Fortran, Pascal, C, Cobol, Java, ...


\begin{code}PROCEDURE rev (a:real array, n:int)
LOCAL VAR i,j: int; x: real;
... x := a[i]; a[i] := a[j]; a[j] := x;
i := i + 1; j := j - 1;


$\bullet$ Expressions (arithmetic, logical)

$\bullet$ Control structures (loops, conditionals, etc.)

$\bullet$ Type declarations and type checking

$\bullet$ Composite types (arrays, records, etc.)

$\bullet$ Procedures/Functions, with private scope

$\bullet$ Abstraction facilities!

How can we make high-level language and Von Neumann machine meet?


$\bullet$ Translate HLL into lower-level code (in traditional compiler, to machine code.)


$\bullet$ Build a ``higher level'' virtual machine (in traditional interpreter, perhaps a stack machine.)

In practice, we do some of both, even in a compiler, since generated machine code makes use of a runtime library and operating system.

Compiler Structure: Want Simplicity and Flexibility



Language Definition

Syntax is easy.

$\bullet$ Well-understood.

$\bullet$ Good theory: regular and context-free languages and automata.

$\bullet$ Good tools, even for complex cases.

Semantics are hard.

$\bullet$ Inherently complex.

$\bullet$ Variety of choices:

Informal -- Reference Manual
Operational -- Definitional interpreter
    ($\uparrow$ we will focus here)
Axiomatic -- Logic
Denotational -- Mathematical functions

$\bullet$ Few tools.

Andrew P. Tolmach