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

CompilerLanguage
DesignDesign
& 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.

Compilers

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

\psfig{figure=l1vnm.eps,height=5.5in,width=7.5in}

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, ...

Example


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

Features

$\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?

Answer:

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

and/or

$\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

\psfig{figure=l1structure.eps,height=9in,width=7.5in}

Example

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
etc.

$\bullet$ Few tools.



Andrew P. Tolmach
1999-01-03