cpsc 388 – compiler design and construction

21
CPSC 388 – Compiler Design and Construction Evolution of Programming Languages Programming Language Basics Make

Upload: alessa

Post on 06-Jan-2016

39 views

Category:

Documents


2 download

DESCRIPTION

CPSC 388 – Compiler Design and Construction. Evolution of Programming Languages Programming Language Basics Make. 1940. 1950. 1960. 1970. 1980. 1990. 2000. Evolution of Programming Languages. Postscript. Machine Code. Prolog. Assembly. Pascal. Python. Fortran. C. Java. Cobol. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CPSC 388 – Compiler Design and Construction

CPSC 388 – Compiler Design and Construction

Evolution of Programming LanguagesProgramming Language BasicsMake

Page 2: CPSC 388 – Compiler Design and Construction

Evolution of Programming Languages

1940 1950 1960 1970 1980 1990 2000

Machine Code

Assembly

FortranCobol

Lisp

C

SchemeBasic

Postscript

Java

PythonPascal

Prolog

Page 3: CPSC 388 – Compiler Design and Construction

Types of Programming Languages

Imperative Languages

Declarative Languages

Languages which specify HOW a computation is to be done.

Languages which specify WHAT computation is to be done.

C, C++, C#, Java, Python, Perl, …

ML, Prolog, Haskell, …

Page 4: CPSC 388 – Compiler Design and Construction

Programming Language Basics

Static/Dynamic Distinction Environments and States Static Scope and Block Structure Explicit Access Control Dynamic Scope Parameter Passing Aliasing

Page 5: CPSC 388 – Compiler Design and Construction

Static / Dynamic Distinction

Static

Dynamic

Example:public static int x;

Issue can be decided at compile time

Issue cannot be decided until runtime

Page 6: CPSC 388 – Compiler Design and Construction

Environments and States

Static vs. Dynamic binding of names to locationsGlobals can be static, others dynamic

Static vs. Dynamic binding of locations to valuesConstants can be static, others dynamic (Strings in

Java are imutable)

names locations(variables)

values

environment state

Page 7: CPSC 388 – Compiler Design and Construction

Static Scope and Block Structuremain() {

int a = 1;int b = 1;{

int b = 2;{

int a = 3;cout << a << b;

}{

int b = 4;cout << a << b;

}cout << a << b;

}cout << a << b;

}

Block

Declaration D “belongs” to block BIf B is the most closely nestedblock containing D.

Scope of declaration D is the blockContaining D and all sub-blocksThat don’t redeclare D.

Page 8: CPSC 388 – Compiler Design and Construction

Explicit Access Control

Classes introduce new scoping for data members.

Subclasses act like sub-blocks public, private, and protected limit

access to data members

Page 9: CPSC 388 – Compiler Design and Construction

Dynamic Scope

Use of name x refers to the declaration of x in the most recently called,not-yet-terminated, procedure with such a declaration.

class Foo {public void x(){}

}

class Bar extends Foo {public void x(){}

}

…Foo foo;…foo.x();…

Which declarationOf x() is called?

Page 10: CPSC 388 – Compiler Design and Construction

Dynamic Scoping vs. Static Scoping

Static is most closely related declaration in space

Dynamic is most closely related declaration in time

Page 11: CPSC 388 – Compiler Design and Construction

Parameter Passing

How do actual parameters associate to formal parameters?

Call by Value

Call by Reference

A copy of actual parameter is madeand placed in formal parameter

The address of actual parameter is passedas value of the formal parameter

Page 12: CPSC 388 – Compiler Design and Construction

Aliasing

When two names refer to the same location in memory

Effects optimization step of compilers

Page 13: CPSC 388 – Compiler Design and Construction

Make Utility Defines which components go together to

make a program. Defines how to "put the pieces together". Keeps track of dependencies among

components. Helps avoid doing unnecessary work and

forgetting to do necessary work (in particular, it is usually used to control (re)compilation of code that depends on code in other files).

Page 14: CPSC 388 – Compiler Design and Construction

Using Make

Create a file named “makefile” or “Makefile”

A makefile contains a set of rules containing a target (usually a file name) a list of files on which the target depends a command to be executed to create the

target file

Page 15: CPSC 388 – Compiler Design and Construction

Example makefileexamples.class: examples.java IO.class Sequence.class

javac examples.javaSequence.class: Sequence.java NoCurrentException.class

javac Sequence.javaNoCurrentException.class: NoCurrentException.java

javac NoCurrentException.javaIO.class: IO.java

javac IO.java

Target Dependencies Command

Line must begin with a tab

Page 16: CPSC 388 – Compiler Design and Construction

Dependencies "A depends on B" means: if B

changes, then A may need to be recreated

Target xxx.class dependency list should include all files that define classes that are used in xxx.java (and xxx.java)

What classes use what other class in last example?

Page 17: CPSC 388 – Compiler Design and Construction

Running Make

Typemake target-name

Or just type make

The first target will be created Try it out (login to linux machine)

Page 18: CPSC 388 – Compiler Design and Construction

More with Make

Create targets to do useful thingsclean:

rm –f *.class *~

Use variables in makefileJC = /usr/bin/javac

JFLAGS = -g

IO.class: IO.java

$(JC) $(JFLAGS) IO.java

Page 19: CPSC 388 – Compiler Design and Construction

More with Make

test: examples.class

java examples $(INPUT)

then type the command:make test INPUT=in.data

Page 20: CPSC 388 – Compiler Design and Construction

Make and Slow Javacexamples.class: examples.java Sequence.java NoCurrentException.java IO.java

javac examples.java Sequence.java NoCurrentException.java IO.javaSequence.class: Sequence.java NoCurrentException.class

javac Sequence.javaNoCurrentException.class: NoCurrentException.java

javac NoCurrentException.javaIO.class: IO.java

javac IO.java

Page 21: CPSC 388 – Compiler Design and Construction

More About Make

http://www.gnu.org/software/make/manual/make.html