structure of programming languages

klipitiklopwarrenΛογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

78 εμφανίσεις

STRUCTURE OF
PROGRAMMING LANGUAGES


Dr Yasser Fouad

ISBN 0
-
321
-
33025
-
0

Book

Quote of the Day

“A language that doesn't affect

the way you think about programming,

is not worth knowing.”


-

Alan Perlis

You work in a little web search company

Your boss says:
“We will conquer the world only if our
search box answers all questions the user may ask.”

So you build
gcalc
:




CS212 helps you answer questions your boss really
cares about.

4

Then you decide to get a PhD

You get tired of the PowerPoint and its
animations.

You embed a domain
-
specific language (DSL)
into
Ruby
.




5



1
-
6

Reasons for Studying Concepts of
Programming Languages


Increased ability to express ideas


Improved background for choosing
appropriate languages


Increased ability to learn new
languages


Better understanding of significance of
implementation


Overall advancement of computing

How is this class different?

It’s about:

a)
foundations of programming langauges

b)
but also how to design your own languages

c)
how to implement them

d)
and about PL tools, such as analyzers

e)
Ans also learn about some classical C.S.
algorithms.


7

8

Why a developer needs PL

New languages will keep coming


Understand them, choose the right one.

Write code that writes code


Be the wizard, not the typist.

Develop your own language.


Are you kidding? No.

Learn about compilers and interpreters.


Programmer’s main tools.

9

Overview


how many languages does one need?


how many languages did you use? Let’s list
them here:


10

Develop your own language

Are you kidding? No. Guess who developed:


PHP


Ruby


JavaScript


perl

Done by smart hackers like you


in a garage


not in academic ivory tower

Our goal: learn good academic lessons


so that your future languages avoid known
mistakes



1
-
11

Programming Domains


Scientific applications


Large number of floating point computations


Fortran


Business applications


Produce reports, use decimal numbers and characters


COBOL


Artificial intelligence


Symbols rather than numbers manipulated


LISP


Systems programming


Need efficiency because of continuous use


C


Web Software


Eclectic collection of languages: markup (e.g., XHTML),
scripting (e.g., PHP), general
-
purpose (e.g., Java)

1
-
12

Programming Methodologies Influences


1950
s and early
1960
s:
Simple

applications; worry
about machine
efficiency


Late
1960
s: People efficiency became important;
readability
, better control structures


structured programming


top
-
down design and step
-
wise refinement


Late
1970
s: Process
-
oriented to data
-
oriented


data abstraction


Middle
1980
s: Object
-
oriented programming


Data abstraction + inheritance + polymorphism

1
-
14

Language Categories


Imperative


Central features are variables, assignment statements, and
iteration


Examples: C, Pascal


Functional


Main means of making computations is by applying functions to
given parameters


Examples: LISP, Scheme


Logic


Rule
-
based (rules are specified in no particular order)


Example: Prolog


Object
-
oriented


Data abstraction, inheritance, late binding


Examples: Java, C++


Markup


New; not a programming per se, but used to specify the layout
of information in Web documents


Examples: XHTML, XML

15


A
program

is a machine
-
compatible representation of an
algorithm


If no algorithm exists for performing a task, then the task can
not be performed by a machine


Programs and algorithms they represent collectively referred
to as
Software

Program

Favorite programming language
June
2012


Python (
3
,
054
)


Ruby (
1
,
723
)


JavaScript (
1
,
415
)


C (
970
)


C# (
829
)


PHP (
666
)


Java (
551
)


C++ (
529
)


Haskell (
519
)


Clojure (
459
)


CoffeeScript (
362
)


Objective C (
326
)


Lisp (
322
)


Perl (
311
)


Scala (
233
)


Scheme (
190
)


Other (
188
)


Erlang (
162
)


Lua (
145
)


SQL (
101
)


job listings collected from Dice.com


Java
17
,
599
(+
8.96
%)


XML
10
,
780
(+
11.70
%)


JavaScript (+
11.64
%)


HTML
9
,
587
(
-
1.53
%)


C#
9
,
293
(+
17.04
%)


C++
6
,
439
(+
7.55
%)


AJAX
5
,
142
(+
15.81
%)


Perl
5
,
107
(+
3.21
%)


PHP
3
,
717
(+
23
%)


Python
3
,
456
(+
32.87
%)


Ruby
2
,
141
(+
39.03
%)


HTML
5
(+
276.85
%)


Flash
1
,
261
(+
95.2
%)


Silverlight
865
(
-
11.91
%)


COBOL
656
(
-
10.75
%)


Assembler
209
(
-
1.42
%)


PowerBuilder (
-
18.71
%)


FORTRAN
45
(
-
33.82
%)



Languages in Common Use

21

ENIAC (
1946
, University of Philadelphia)

ENIAC program for external ballistic equations:

22

Programming the ENIAC

23

ENIAC (
1946
, University of
Philadelphia)

programming done by


rewiring the interconnections


to set up desired formulas, etc

Problem (what’s the tedious part?)


programming = rewiring


slow, error
-
prone

solution:


store the program in memory!


birth of
von Neuman
paradigm

24

Assembly


the language (UNIVAC
1
,
1950
)

Idea: mnemonic (assembly) code


Then translate it to machine code by hand (no compiler
yet)


write programs with mnemonic codes (add, sub),

with symbolic labels,


then assign addresses by hand

Example of symbolic assembler

clear
-
and
-
add a

add b

store c

translate it by hand to something like this
(understood by CPU)

B
100
A
200

C
300

Assembly Language


Use symbols instead of binary digits to
describe fields of instructions.


Every aspect of machine visible in
program:


One statement per machine instruction.


Register allocation, call stack, etc. must be
managed explicitly.


No structure: everything looks the same.


101011
00100
00010
0000000000010101

ADDI

R
4

R
2

21

ADDI R
4
,R
2
,
21

26

Assembler


the compiler (Manchester,
1952
)


a loop example, in MIPS, a modern
-
day assembly
code:


loop: addi $t
3
, $t
0
,
-
8


addi $t
4
, $t
0
,
-
4


lw $t
1
, theArray($t
3
)

# Gets the last


lw $t
2
, theArray($t
4
)

# two elements


add $t
5
, $t
1
, $t
2


# Adds them together...


sw $t
5
, theArray($t
0
)

# ...and stores the
result


addi $t
0
, $t
0
,
4


# Moves to next
"element“







# of theArray


blt $t
0
,
160
, loop


# If not past the end of








# theArray, repeat


jr $ra



High
-
level Language


Provides notation to describe problem
solving strategies rather than organize data
and instructions at machine
-
level.


Improves
programmer productivity

by
supporting features to
abstract/reuse

code,
and to improve
reliability/robustness

of
programs.


Requires a

compiler
.

28

FORTRAN I (
1954
-
57
)

Langauge, and the first compiler


Produced code almost as good as hand
-
written


Huge impact on computer science


Modern compilers preserve its outlines


By
1958
, >
50
% of all software is in
FORTRAN


29

FORTRAN I

Example: nested loops in FORTRAN


a big improvement over assembler,


but annoying artifacts of assembly remain:


labels and rather explicit jumps (CONTINUE)


lexical columns: the statement must start in column
7



The MIPS loop from previous slide, in FORTRAN:





DO
10
I =
2
,
40



A[I] = A[I
-
1
] + A[I
-
2
]


10

CONTINUE



30

Side note: designing a good language
is hard

Good language protects against bugs, but lessons take a
while.

An example that caused a failure of a NASA planetary probe:



buggy line:




DO
15
I =
1.100



what was intended (a dot had replaced the comma):




DO
15
I =
1
,
100



because Fortran ignores spaces, compiler read this
as:




DO
15
I =
1.100



which is an assignment into a variable DO
15
I, not a
loop.


This mistake is harder to make (if at all possible) with the
modern lexical rules (white space not ignored) and loop
syntax





for (
i
=
1
;
i

<
100
;
i
++) { … }

31

Goto considered harmful

L
1
: statement

if expression goto L
1

statement


Dijkstra says: gotos are harmful


use structured programming


lose some performance, gain a lot of readability


how do you rewrite the above code into structured
form?

Evolution of Programming Languages



ALGOL

-

60
(ALGOrithmic Language)

Goals

: Communicating Algorithms

Features

: Block Structure (Top
-
down design)


Recursion (Problem
-
solving strategy)


BNF
-

Specification




LISP
(LISt Processing)


Goals

: Manipulating symbolic information


Features

: List Primitives


Interpreters / Environment


Evolution of Programming Languages



Pascal



Goal

: Structured Programming, Type checking,


Compiler writing.


Features

:



Rich set of data types for efficient


algorithm design



E.g., Records, sets, ...



Variety of “readable”
single
-
entry



single
-
exit

control structures



E.g.,
for
-
loop, while
-
loop,
...



Efficient Implementation



Recursive descent parsing


Other Languages


Functional


LISP, Scheme


ML, Haskell


Logic


Prolog


Object
-
oriented


Smalltalk, SIMULA, Modula
-
3
, Oberon


C++, Java, C#, Eiffel, Ada
-
95


Hybrid


Python, Ruby, Scala


Application specific languages and
tools

35

Programming Languages
3
/
4


C


Bell labs


Dennis Ritchie,
1973


C++


Bjarne Stroustrup,
1980


Hybrid OOP


Java


Sun Microsystems (formally announced in May
1995
)


Pure OOP


Web programming

Scripting vs Systems Programming Languages


Designed for gluing
applications : flexibility


Interpreted


Dynamic typing and
variable creation


Data and code
integrated : meta
-
programming
supported


Examples
: PERL, Tcl,
Python, Ruby, PHP,
Scheme, Visual Basic,
Scala
, etc.


Designed for building
applications :
efficiency


Compiled


Static typing and
variable declaration


Data and code
separated : cannot
create/run code on the
fly


Examples
: PL/
1
, Ada,
Java, C, C++, C#,
Scala
, etc.

Current Trend


Multiparadigm languages


Functional

constructs for programming in the
small


Focus on conciseness and correctness


Object
-
Oriented
constructs for programming in
the large


Focus on programmer productivity and code evolution



Example languages


Older
: Python, Ruby,


Recent
: Scala, F#, etc

Scheme (
dialect of LISP
)


Recursive definitions


Symbolic computation : List Processing


Higher
-
order functions


Dynamic type checking


Functional + Imperative features


Automatic storage management


Provides a uniform executable platform for
studying, specifying, and comparing
languages.

Java vs Scala

//Java
-

what we're used to seeing


public String buildEpochKey(String... keys) {

StringBuilder s = new StringBuilder("elem")


for(String key:keys) {


if(key != null) {


s.append(".")


s.append(key)


}


}


return s.toString(). toLowerCase()

}

Java vs Scala

//Scala



def buildEpochKey(keys: String*): String = {


("elem" +: keys) filter(_ != null)


mkString(".")


toLowerCase

}

1
-
41

Implementation Methods


Compilation


Programs are translated into machine
language


Pure Interpretation


Programs are interpreted by another
program known as an interpreter


Hybrid Implementation Systems


A compromise between compilers and
pure interpreters

1
-
42

Compilation


Translate high
-
level program (source language)
into machine code (machine language)


Slow translation, fast execution


Compilation process has several phases:


lexical analysis: converts characters in the source program
into lexical units


syntax analysis: transforms lexical units into
parse trees
which represent the syntactic structure of program


Semantics analysis: generate intermediate code


code generation: machine code is generated


1
-
43

The Compilation Process

Additional Compilation Terminologies


Load module

(executable image): the user
and system code together


Linking and loading
: the process of
collecting system program and linking
them to user program

1
-
45

Pure Interpretation


No translation


Easier implementation of programs (run
-
time errors can easily and immediately
displayed)


Slower execution (
10
to
100
times slower
than compiled programs)


Often requires more space


Becoming rare on high
-
level languages


Significant comeback with some Web
scripting languages (e.g., JavaScript)

1
-
46

Hybrid Implementation Systems


A compromise between compilers and pure
interpreters


A high
-
level language program is
translated to an intermediate language that
allows easy interpretation


Faster than pure interpretation


Examples


Perl programs are partially compiled to detect errors
before interpretation


Initial implementations of Java were hybrid; the
intermediate form,
byte code
, provides portability to any
machine that has a byte code interpreter and a run
-
time
system (together, these are called
Java Virtual Machine
)


1
-
47

Just
-
in
-
Time Implementation Systems


Initially translate programs to an
intermediate language


Then compile intermediate language into
machine code


Machine code version is kept for
subsequent calls


JIT systems are widely used for Java
programs


.NET languages are implemented with a JIT
system

1
-
48

Programming Environments


The collection of tools used in software
development


UNIX


An older operating system and tool collection


Nowadays often used through a GUI (e.g., CDE, KDE, or
GNOME) that run on top of UNIX


Borland JBuilder


An integrated development environment for Java


Microsoft Visual Studio.NET


A large, complex visual environment


Used to program in C#, Visual BASIC.NET, Jscript, J#, or
C++

What Does This C Statement Mean?


*p++ = q++

increments p

increments q

modifies *p

Does this mean…

… or

… or

*p = *q;

++p;

++q;

*p = *q;

++q;

++p;

tp = p;

++p;

tq = q;

++q;

*tp = *tq;

Languages


Simula


Smalltalk


Algol


Cobol


F#


Prolog


Pascal


Modula
-
2


ADA


PL/I


CORBA


PERL


BASIC


JAVASCRIPT


LISP


MIRANDA


ML


SCHEMA


SNOBOL


APL


DELPHI


MAYA

1
-
50