07
Document Sample


Programming Languages
10/11/2011
Che-Rung Lee
10/11/2011 CS135601 Introduction to Information Engineering 1
Why is machine code not good?
assembly enough?
Machine code Assembly language
156C Op-code+Operand LD $5, Price
166D (registers, memory LD $6, ShippingCharge
locations, values)
5056 ADDI $0, $5, $6 Registers
30CE Assembler ST $0, TotalCost
C000 HLT Identifiers:
Descriptive names
What does this mean? Mnemonic names for op-codes
for memory
One-to-one correspondence
locations
• Still need to think like a machine
• Inherently machine-dependent
10/11/2011 CS135601 Introduction to Information Engineering 2
Third generation languages
• Uses high-level primitives
– Similar to the pseudocode in Algorithm lesson
– Each primitive corresponds to a sequence of
machine language instructions
• Machine independent (mostly)
– Ex: little endian(Intel) and big endian(HP,IBM)
Memory address 0x0000 0x0001 0x0002 0x0003
Big endian 12 34 56 78
Little endian 78 56 34 12
10/11/2011 CS135601 Introduction to Information Engineering 3
The evolution of programming
languages
• Why do people need/invent so many
different programming languages?
– In fact, there are many more Isn’t C good enough?
10/11/2011 CS135601 Introduction to Information Engineering 4
Programming paradigms
Imperative paradigm Object-oriented paradigm
A sequence of commands A collection of objects that
that manipulate data to can perform actions and
produce the desired results. interact with other objects.
(C,JavaScript,Fortran,Matlab) (C++,Java,C#,VisualBasic)
Declarative paradigm Functional paradigm
Describe the problem to be A composition of functions
solved rather than the (in math sense) that accept
algorithms.(Prolog,Verilog,VHDL inputs and produce outputs.
,Lex/Yacc,AMPL,SQL,HTML) (LISP, Mathematica)
10/11/2011 CS135601 Introduction to Information Engineering 5
Outline
• “Traditional” programming languages
– Imperative programming languages
• Object-oriented programming languages
• Declarative programming languages
• Functional programming languages
• *Parallel programming
10/11/2011 CS135601 Introduction to Information Engineering 6
“Traditional” 3rd generation
Programming Languages
Imperative programming languages
(procedural programming languages)
10/11/2011 CS135601 Introduction to Information Engineering 7
Programming primitives
• Declarative statements: define customized
names to be used later in the program
• Imperative statements: describe the steps
in the underlying algorithms
• Comments: enhance the readability of a
program for handy explanation
• Directives: assist compiler/interpreter to
generate codes, documents
10/11/2011 CS135601 Introduction to Information Engineering 8
Declarative statements
• Variables are identifiers or customized names
defined to refer some memory locations
• Literals are fixed, predetermined values
• Constants are customized names initiated with
fixed values (meaningful literals)
• Data type: for variables or constants
– Primitive data types: integer, real, character,
Boolean. (predefined in languages)
– Derived data types: customized data type
10/11/2011 CS135601 Introduction to Information Engineering 9
Imperative statements
• Assignment statements
– variables = expression
• Control statements: alter the execution
sequence of the program
– Unconditional jump: goto
– Conditional jump: if-then-else, switch-case
– Loop: for, while-loop, do-until
• Function invocation: we will talk this later
10/11/2011 CS135601 Introduction to Information Engineering 10
Directives
• Not part of executable code or declaration,
but statements to assist code generation
– In C/C++, “#include <stdio.h>”
– In shell script, “#!/usr/bin/bash”
• Directives used for another purposes
– Macro preprocessor in C/C++, “#ifdef”
– JavaDoc uses tags and comments to
Talk
create document for Java programs later
– OpenMP uses them to parallelize programs
10/11/2011 CS135601 Introduction to Information Engineering 11
Procedural unit
• A program unit written independently of
other program units yet associated with
them through a transfer/return process
– Two kinds of procedural units
• Function: a procedural unit that returns values
• Procedure: a procedural
unit that does not
return values
10/11/2011 CS135601 Introduction to Information Engineering 12
Procedure declaration
• Header(interface): type name (parameters)
– Type: procedure or function
• For a function, define the data type of returned value
• Body: variable declarations + commands
Header
Variable declaration
Imperative statements
10/11/2011 CS135601 Introduction to Information Engineering 13
Procedure invocation
Call by value Call by reference
10/11/2011 CS135601 Introduction to Information Engineering 14
Variable scope
Global variable
• What is the output? int main(){
int var1=3;
– 3 or 5 or 7 or else? foo();
printf(“var1=%d\n”,var1);
• Global and local }
Local variable
void foo(){
– The same name int var1=5;
but at different bar();
}
memory addresses Local variable
void bar(){
void bar(){
• What is the output }
int var1=7;
var1=7;
}
for this one?
10/11/2011 CS135601 Introduction to Information Engineering 15
Memory allocation
• Global variables are allocated at heap;
local variables are allocated at stack.
• When a procedure call is
finished, local variables are var1 for main
popped out from the stack. Heap
– Why uses stack for local
variables?
• Dynamic allocated variables var1 for bar Stack
are in heap, no matter local var1 for foo
or global. (what’s problem?)
10/11/2011 CS135601 Introduction to Information Engineering 16
Language implementations
• Interpreter: interprets and executes a
program statement by statement
• Compiler: translates high level program
primitives into machine codes.
– The translation process
10/11/2011 CS135601 Introduction to Information Engineering 17
Lexical analyzer
• Breakdown a program into a list of tokens
a = b + 32; Token Type
a Variable
= Assignment operator
b Variable
+ Addition operator
32 Integer
; End of statement
10/11/2011 CS135601 Introduction to Information Engineering 18
Parser
• Group tokens into meaningful structures
– “meaningful” is defined by the grammatical
rules of the programming language.
– It can be hard even for human
The man the horse that won the race threw was not hurt.
– Grammar: the rules to define the syntax
– Syntax diagram: flow chart for grammar
– Parse tree (output of parser): the hierarchical
structure of tokens
10/11/2011 CS135601 Introduction to Information Engineering 19
An example x+yz
• Syntax diagram for • Parse tree for x+yz
algebraic expression
10/11/2011 CS135601 Introduction to Information Engineering 20
Code generation
• Translates parse trees to machine codes
• Why are the compiled programs more
efficient than the interpreted programs?
Source code Interpreted code
x = y + z; Memory I/O
Load $1, y
w = x + z; (Load, Store) is
Load $2, z
much slower than
Compiled code Addi $1, $2, $3
Store $3, x computation.
Load $1, y
Load $2, z Load $1, x
Addi $1, $2, $3 Load $2, z
Addi $2, $3, $4 Addi $1, $2, $3
Store $4, w Store $3, w
10/11/2011 CS135601 Introduction to Information Engineering 21
Object-Oriented
Programming Languages
10/11/2011 CS135601 Introduction to Information Engineering 22
E-pet game
• Suppose you want to design a computer
game about E-pet
– If you call its name, the pet will run to you.
– If you pat the pet, it will be happy.
– If you feed the pet, it will eat happily.
–…
10/11/2011 CS135601 Introduction to Information Engineering 23
How to design?
Puppy Kitty Chick
Name Name Name
Four legs Four legs Two legs
One tail One tail Two wings
Color Color Color
Bark Meow Cheep
Wag Tail Jump Flip wings
Bite Bite Peck
Puppy Kitty Chick
Run With 4 legs With 4 legs With 2 legs, 2 wings
Happy Bark, wag tail Meow, jump Cheep, flip wings
Eat Bite Bite Peck
10/11/2011 CS135601 Introduction to Information Engineering 24
First implementation
• Implement procedures for puppy_run,
puppy_happy, puppy_bite, kitty_run, …
• Implement procedures for callName, pat,
and feed. Procedure feed(p)
if p is a puppy
Procedure callName(p) Procedure pat(p) call puppy_eat(p)
if p is a puppy if p is a puppy call puppy_happy(p)
call puppy_run(p) call puppy_happy(p) elseif p is a kitty
elseif p is a kitty elseif p is a kitty call kitty_eat(p)
call kitty_run(p) call kitty_happy(p) call kitty_happy(p)
elseif p is a chick elseif p is a chick elseif p is a chick
call chick_run(p) call chick_happy(p) call chick_eat(p)
call chick_happy(p)
10/11/2011 CS135601 Introduction to Information Engineering 25
Problems for impl 1
• When adding or removing an E-pet, you
need to change all the procedures (pat,…)
• Want to reuse codes as much as possible
– The code of puppy_run and kitty_run, and
puppy_eat and kitty_eat are the same
• What’s the benefit of code reusing?
– Reduce development cost and maintenance cost
• Think about if you want add a feature to all E-pet
• Reduce code size
10/11/2011 CS135601 Introduction to Information Engineering 26
Second implementation
Procedure callName(p) Procedure pat(p) Procedure feed(p)
call run(p) call happy(p) call eat(p)
call happy(p)
Procedure happy(p)
Procedure run(p) if p is a puppy Procedure eat(p)
if p is a puppy or kitty call puppy_happy(p) if p is a puppy or kitty
call runWith4Legs(p) elseif p is a kitty call Bite(p)
elseif p is a chick call kitty_happy(p) elseif p is a chick
call runWith2Legs(p) elseif p is a chick call Peck(p)
call chick_happy(p)
• We don’t need to change callName, pat,
and feed when adding/remove pets, and
we reuse common codes for puppy&kitty
10/11/2011 CS135601 Introduction to Information Engineering 27
Problems for impl 2
• We didn’t really solve the problems of impl 1.
When adding/removing pets, we still need to
change the code for run, happy, eat.
– We do not want to change existing code when
adding or removing some “objects”.
• We make the code more complicated.
– EX: if we want a puppy running with its tail
wagging, we need to change the procedure run
Code Function
reusing isolation
10/11/2011 CS135601 Introduction to Information Engineering 28
Object-oriented programming
• Object: relevant data + procedures to
process the data
– The definition of an object is called a class
• Use the relations of objects to achieve
code reuse, like inheritance.
• Use polymorphism to allow variations, by
which more codes can be reused.
• Use encapsulation to hide information.
10/11/2011 CS135601 Introduction to Information Engineering 29
Inheritance
• Allows new classes to be defined in terms
of previously defined classes Puppy
Chick Bark
Two-leg pet
Wings Wag tail
Pet Two legs
FlipWings Kitty
Name Run Cheep
Meou
Color
Jump
Run Four-leg pet Bite-pet
Eat Calf
Four legs Bite
MakeSound moo
One tail
Run Chew-pet Lamb
Chew Baa
10/11/2011 CS135601 Introduction to Information Engineering 30
Polymorphism
• Allows procedure calls to be interpreted by
the object that receives the call
– Dynamic binding: function binding in runtime
class Puppy extends Pet {
void pat(Pet p) { p.happy(); }
public void happy()
{ bark(); wagTail();}
Pet p1 = new Puppy(); }
pat(p1);
class Kitty extends Pet
{ public void happy()
Pet p2 = new Kitty(); { meou(); jump(); }
pat(p2); }
10/11/2011 CS135601 Introduction to Information Engineering 31
Encapsulation
• A way of restricting access to the internal
components of an object
– Private versus public
Puppy p1 = new Puppy();
class Puppy extends Pet
{ private char[]: name; p1.name = “spot”;
public char[] getName() char[] a = p1.name;
{ return area; }
} char[] b = p1.getName();
10/11/2011 CS135601 Introduction to Information Engineering 32
Declarative
Programming Languages
10/11/2011 CS135601 Introduction to Information Engineering 33
What is a declarative language?
• A programming paradigm that expresses
the problem to be solved rather than the
algorithms.
– Imperative languages need to describe
algorithms explicitly.
– Uses backend engine to “solve” problems.
– It is usually domain specific.
• Prolog,HTML,Verilog,VHDL,Lex/Yacc,AMPL,SQL
– Many languages hybrid declarative and
imperative paradigms.
10/11/2011 CS135601 Introduction to Information Engineering 34
Prolog
• PROgramming in LOGic: A logic
programming language for general logic
problems solving.
1: witch(X) <= burns(X) and female(X). Resolution
2: burns(X) <= wooden(X). Rules --- running ---
3: wooden(X) <= floats(X). witch(girl) yes
4: floats(X) <= sameweight(duck, X). --- finished ---
5:
6: female(girl).
7: sameweight(duck,girl). Facts
8:
9: ? witch(girl). Query
10/11/2011 CS135601 (Sir Bedevere). Information Engineering
{\fB After Monty Python Introduction to\fP} 35
Verilog
• A hardware description language used in
the design, verification, and implementation
of digital logic chips.
1 module addbit (a, b, ci, sum, co)
a b ci
2 input a, b, ci;
3 output sum, co;
4 //Port Data types addbit
5 wire a, b, ci, sum, co;
6 //Code starts here
7 assign {co,sum} = a + b + ci; co sum
8 endmodule
10/11/2011 CS135601 Introduction to Information Engineering 36
VHDL
• VHSIC (Very High Speed Integrated
Circuits) Hardware Description Language:
– a design-entry language for field-program-
mable gate arrays and application-specific
integrated circuits in electronic design
automation of digital circuits.
10/11/2011 CS135601 Introduction to Information Engineering 37
Lex / Yacc
• Lex is a program that generates lexical
analyzers; yacc (Yet Another Compiler Compiler)
is a parser generator based on grammar.
Token definition: Grammar definition:
number [0-9] expr : expr '+' expr
identifier [a-z][a-z0-9]* { $$ = node( '+', $1, $3 ); }
lex yacc
Source code Lexical analyzer Token Parser Parse
HelloWorld.c (lex.yy.c) stream (y.tab.c) tree
10/11/2011 CS135601 Introduction to Information Engineering 38
SQL
• Structured Query Language: A database
language designed for managing data in
relational database management systems.
SELECT Title, Authors Title Authors
-------------------------------------------------- -------
FROM Book
SQL Examples and Guide 3
WHERE price > 100.00 The Joy of SQL 1
ORDER BY Authors; An Introduction to SQL 2 Pitfalls of SQL 1
We may talk more on this in the database lesson.
10/11/2011 CS135601 Introduction to Information Engineering 39
HTML
• Hyper Text Markup Language: describes
the display and format of text, graphics,
hyperlink to other html files…
10/11/2011 CS135601 Introduction to Information Engineering 40
JavaDoc
• Generates API documentation in HTML
format from comments in Java source code.
/**
* Returns an Image object that can then be painted on the screen.
* Input: a source
* @param url an absolute URL giving the base location of the image
* @param name the location of the image, relative to the url argument
code with tagged
* @return the image at the specified URL comments
* @see Image
*/
public Image getImage(URL url, String name)
getImage
public Image getImage(URL url, String name)
Returns an Image object that can then be painted on the screen. Output: an HTML
Parameters: document for the
url - an absolute URL giving the base location of the image
name - the location of the image, relative to the url argument
function
Returns:
the image at the specified URL
See Also:
10/11/2011 CS135601 Introduction to Information Engineering 41
Image
Functional
Programming Languages
10/11/2011 CS135601 Introduction to Information Engineering 42
Functional languages
• Computation=evaluation of math functions.
– The output value of a “function” depends only
on the arguments that are input to the function
• It avoids state and mutable data.
– Imperative programming emphasizes
changes state.
– We will see an example for their differences.
• It uses recursion instead of iteration (loop)
10/11/2011 CS135601 Introduction to Information Engineering 43
LISP: LIst Processing Language
• The first functional programming language
• Represent program code and data as lists
• Every list returns a value, which will be
used as an input of its upper level list.
– The return value of the top level will be output
– Ex: (+ (* 3 (+ 1 (- 4 2 (+ 3 4))))) outputs ?
• Many variations (dialects)
– Common lisp, Scheme, emacs lisp
10/11/2011 CS135601 Introduction to Information Engineering 44
Syntax
• Atom: symbol or number
• List: consists of 0 or more expression
– Ex: (42 69 613)
– The first atom in the list is an “operator”
• Recursion: (defun factorial (n)
(if (<= n 1)
– compute n! 1
(* n (factorial (- n 1)))))
10/11/2011 CS135601 Introduction to Information Engineering 45
List evaluation
• Ex: (+ 1 2 3 (* 4 5) 6) evaluate the list first
Step 1. (+ 1 2 3 X 6) Step 7. the operator is *,
– X = (* 4 5) multiply all atoms
Step 2. the operation is +, Step 8. found 4, integer
add all atoms Step 9. found 5, integer
Step 3. found 1, integer Step 10. end of list,
Step 4. found 2, integer evaluate 4*5=20
Step 5. found 3, integer Step 11. found 6, integer
Step 6. found (* 4 5), list Step 12. end of list,
10/11/2011
evaluate 1+2+3+20+6=32
CS135601 Introduction to Information Engineering 46
Functional vs. imperative
• A function for checkbook balancing
constructed from two simpler functions
(Find_diff
(Find_sum Old_balance Credits)
(Find_sum Debits))
Functional program
Total_credits = sum of all credits
Temp_balance = Old_balanace +
Total_credits
Total_debit = sum of all debits
Balance = Temp_balance –
Total_debits
Imperative program
10/11/2011 CS135601 Introduction to Information Engineering 47
Parallel Programming
10/11/2011 CS135601 Introduction to Information Engineering 48
Why parallel computing?
• Before:
– Save time and money
– Solve larger problems
• Now:
– Multi-core and many-core processors become
the major architecture of computers
– Devices such as GPU (graphics processing
unit) have large number of cores
10/11/2011 CS135601 Introduction to Information Engineering 49
An example
• Problem: compute f (x,y)=x3+2x2y3+4xy2+3y
• Number of processors: 2 (P1 and P2)
• Problem is divided into small problems x3,
2x2y3, 4xy2, 3y and put them into a queue Q
• Algorithm: give x and y, and set f = 0.
1. P1 and P2 read x and y.
2. P1 and P2 get a small problem from Q,
compute it, and add the result to the
variable f, until Q is empty.
10/11/2011 CS135601 Introduction to Information Engineering 50
Problems
• Can P1 and P2 read x or y simultaneously?
– Most memory cannot be read simultaneously.
• How can f be updated by P1 and P2?
– The read-and-update f must be “atomic”.
– Like the critical region idea in the OS lesson.
• There could be dead lock
P1: P2:
– Suppose P1 has x
wait(y) wait(x)
and P2 has y.
send(x) send(y)
10/11/2011 CS135601 Introduction to Information Engineering 51
Parallel programming methods
• For distributed systems: message passing
– Explicit network communication.
– Tight coupled systems: MPI, PVM
– Loosely coupled systems: RPC, RMI, CORBA
• For multi-core processor: shared memory
– Multithreading: P-thread, OpenMP
– Parallel programming language: HPF, UPC
• For specific multiprocessor
– Hardware specific languages: Cuda, OpenCL
10/11/2011 CS135601 Introduction to Information Engineering 52
MPI
• Message Passing Interface: a library
specification for message-passing
programming model
10/11/2011 CS135601 Introduction to Information Engineering 53
RPC
• Remote Procedure Call: allows a program
to call a remote procedure like to call a
local one
– Remote procedure: the execution of the
procedure is on another machine
Client Server
RPC Procedure execution
6 1 3 4
stub 2 skeleton
5
10/11/2011 CS135601 Introduction to Information Engineering 54
P-thread
• POSIX threads: a standard programming
interface for threads programming.
– We have talked about thread in the OS lesson
• Three classes of functions
– Thread management:
– Mutexes:
– Condition variables:
10/11/2011 CS135601 Introduction to Information Engineering 55
OpenMP
• Open Multi-Processing: an Application
Program Interface (API) comprised of
three primary components:
– Compiler Directives
– Runtime Library Routines
– Environment Variables
• Features
– Multi-platform, available in C/C++ and Fortran
– Incrementally parallelizing serial programs
10/11/2011 CS135601 Introduction to Information Engineering 56
OpenMP example
#include <omp.h>
Header file
main () {
int nthreads, tid;
/* Fork a team of threads with each thread having a private tid variable */
#pragma omp parallel private(tid)
{ /* Obtain and print thread id */ Compiler derivative
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid); Runtime functions
/* Only master thread does this */
if (tid == 0) {
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and terminate */
}
10/11/2011 CS135601 Introduction to Information Engineering 57
Compiling and running
• Compiling
– For visual c++, using “/openmp” to enable
OpenMP support
• Running
– The number of threads
is decided by the
environment variable
OMP_NUM_THREADS
10/11/2011 CS135601 Introduction to Information Engineering 58
OpenCL
• Open Computing Language: a framework
for writing programs that execute across
heterogeneous platforms consisting of
CPUs, GPUs, and other processors.
– A language for writing kernel functions
– APIs for programming on platforms
• Task parallelism and data parallelism.
10/11/2011 CS135601 Introduction to Information Engineering 59
Related courses
• C, C++, Java, JavaScript
– 計算機程式設計,Web程式設計,物件導向程式設
計,軟體實驗,高等程式設計實作,網路程式設計
• Assembly:
– 軟體實驗,計算機結構,嵌入式系統概論
• Matlab:科學計算,影像處理簡介
• Visual basic:多媒體技術概論 (maybe)
10/11/2011 CS135601 Introduction to Information Engineering 60
Related courses (cont’)
• Parallel programming:平行程式設計
• Language translation, lex/yacc:編譯器設計
• Language theory:正規語言
• Programming languages: 程式語言
• Verilog, VHDL Learn how to use them and the
algorithms in the backend engine.
– 數位邏輯設計,硬體實驗,硬體描述語言與合成
• SQL:資料庫系統概論
• Lisp or prolog:人工智慧概論 (maybe)
10/11/2011 CS135601 Introduction to Information Engineering 61
References
• http://www.allisons.org/ll/Logic/Prolog/(prolog example)
• http://www.asic-world.com/ (verilog example)
• http://www.ee.ccu.edu.tw/~wl/FPGA/VHDL 20training.pdf
(VHDL example)
• http://en.wikipedia.org/wiki/SQL (SQL example)
• https://computing.llnl.gov/tutorials/parallel_comp/
(parallel programming)
• Textbook: chap 6
10/11/2011 CS135601 Introduction to Information Engineering 62
Get documents about "