Top Banner
Programming Languages (TC-2006) General concepts of programming languages Jos´ e Carlos Ortiz Bayliss [email protected]
67

General concepts of programming languages

Mar 23, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: General concepts of programming languages

Programming Languages(TC-2006)

General concepts of programming languages

Jose Carlos Ortiz [email protected]

Page 2: General concepts of programming languages

Contents

1 Introduction

2 Classification

3 The translation process

4 Abstraction

5 Parameter passing

2 / 34

Page 3: General concepts of programming languages

What is a programming language?

Programming languages evolved as a means of telling a computer whatto do.

For programmers, however, a language is more aptly defined as a meansof expressing algorithms.

A programming language is a formal computer language designed to com-municate instructions to a machine, particularly a computer. Programminglanguages can be used to create programs to control the behavior of amachine or to express algorithms.

3 / 34

Page 4: General concepts of programming languages

What is a programming language?

Programming languages evolved as a means of telling a computer whatto do.

For programmers, however, a language is more aptly defined as a meansof expressing algorithms.

A programming language is a formal computer language designed to com-municate instructions to a machine, particularly a computer. Programminglanguages can be used to create programs to control the behavior of amachine or to express algorithms.

3 / 34

Page 5: General concepts of programming languages

What is a programming language?

Programming languages evolved as a means of telling a computer whatto do.

For programmers, however, a language is more aptly defined as a meansof expressing algorithms.

A programming language is a formal computer language designed to com-municate instructions to a machine, particularly a computer. Programminglanguages can be used to create programs to control the behavior of amachine or to express algorithms.

3 / 34

Page 6: General concepts of programming languages

The diversity of programming languages

Why are there so many programming languages?

Evolution. Computer science is a young discipline that is still underdevelopment.

Special purposes. Many languages are designed for a specific problemdomain.

Personal preference. Much of the diversity of programming is simplya matter of taste. In other words: different people like different things.

4 / 34

Page 7: General concepts of programming languages

What makes a programming language successful?

Expressive power. Language features clearly have a huge impact on theirsuccess.

Learning curve. The faster you can learn it, the more likely you are to useit.

Ease of implementation. Its capability to be implemented easily on tinymachines with limited resources or in different platforms and operating sys-tems.

Open source. Most programming languages today have at least one opensource compiler or interpreter.

Excellent Compilers. Excellent compilers and the supporting tools that doan unusually good job to help the programmers to manage very large projects.

Sponsorship and costs. Other than technical merit that greatly influencesuccess.

5 / 34

Page 8: General concepts of programming languages

The classification of programming languages

Based on how they express solutions.

Based on their paradigm.

Based on their abstraction level.

6 / 34

Page 9: General concepts of programming languages

Classification based on how they express solutions

Declarative languages. Languages that focus on what the computerhas to do.

Imperative languages. Languages that focus on how the computershould do it.

7 / 34

Page 10: General concepts of programming languages

Declarative languages

Languages that focus on what the computer has to do.

cat(felix).cat(tom).mouse(mickey).mouse(jerry).eats(X, Y) :- cat(X), mouse(Y).

8 / 34

Page 11: General concepts of programming languages

Imperative languages

Languages that focus on how the computer should do it.

num = int(input("Enter a number: "))factorial = 1if num < 0:

print("Sorry, factorial does not exist for negative numbers.")elif num == 0:

print("The factorial of 0 is 1.")else:

for i in range(1,num + 1):factorial = factorial * i

print("The factorial of", num, "is", factorial, ".")

9 / 34

Page 12: General concepts of programming languages

Classification based on their paradigm

Imperative.

Declarative.

Structured.

Object oriented.

Functional.

Logic and constraint.

10 / 34

Page 13: General concepts of programming languages

Imperative programming

The control flow in imperative programming is explicit: commands showhow the computation takes place, step by step.

result = []i = 0start:

numPeople = length(people)if i >= numPeople goto endp = people[i]nameLength = length(p.name)if nameLength <= 5 goto nextupperName = toUpper(p.name)addToList(result, upperName)

next:i = i + 1goto start

end:return sort(result)

11 / 34

Page 14: General concepts of programming languages

Structured programming

Structured programming is a kind of imperative programming where thecontrol flow is defined by nested loops, conditionals, and subroutines, ratherthan via goto instructions.

result = [];for i = 0; i < length(people); i++ {

p = people[i];if length(p.name)) > 5 {

addToList(result, toUpper(p.name));}

}return sort(result);

12 / 34

Page 15: General concepts of programming languages

Object oriented programming

Object oriented programming is based on the sending of messages to objects.Objects respond to messages by performing operations. Messages can havearguments, so “sending messages” looks a lot like calling subroutines.

result = [];for p in people {

if p.name.length() > 5 {result.add(p.name.toUpper());

}}return result.sort();

13 / 34

Page 16: General concepts of programming languages

Declarative programming

The control flow in declarative programming is implicit: the programmerstates only what the result should look like, not how to obtain it. No loops,no assignments, etc.

select upper(name)from peoplewhere length(name) > 5order by name

14 / 34

Page 17: General concepts of programming languages

Functional programming

The real power of this paradigm comes from passing functions to functions(and returning functions from functions).

let(f, fun(people,if(equals(people, emptylist),

emptylist,if(greater(length(name(head(people))), 5),append(to_upper(name(head(people))), f(tail(people))),f(tail(people))))),

sort(f(people)))

15 / 34

Page 18: General concepts of programming languages

Classification by abstraction level

Low-level programming languages.

High-level programming languages.

Very high-level programming languages.

16 / 34

Page 19: General concepts of programming languages

Low-level programming languages

Provide little or no abstraction from a computer’s architecture or func-tions in the language map closely to processor instructions.

Generally this refers to either machine code or assembly language.

The word “low” refers to the scarce amount of abstraction between thelanguage and machine language.

17 / 34

Page 20: General concepts of programming languages

High-level programming languages

Provide a strong abstraction from the details of the computer.

They use natural language elements or may automate significant areasof computing systems (for example, memory management).

They make the process of developing a program simpler and moreunderstandable relative to a lower-level language.

18 / 34

Page 21: General concepts of programming languages

Very high-level programming languages

Provide a very high level of abstraction.

They are used primarily as a professional programmer productivity tool.

They are usually domain-specific languages, limited to a very specificapplication, purpose, or type of task.

19 / 34

Page 22: General concepts of programming languages

Final remarks on the classification of programminglanguages

All these categories are fuzzy and open to debate.

For example, many authors do not consider functional programming tobe declarative.

20 / 34

Page 23: General concepts of programming languages

The translation process

We write a computer programs by using a high-level language. A high-level language is one which is understandable by us.

Computers do not understand high-level languages, they only under-stand machine code (0’s and 1’s).

A program written in high-level language is called a source code andwe need to convert the source code into machine code and this isaccomplished my compilers and interpreters.

21 / 34

Page 24: General concepts of programming languages

Compilation

At the highest level of abstraction, the compilation and execution of a program ina high-level language should look something like this:

Source program

Compiler

Target programInput Output

The compiler translates the high-level source program into an equivalent targetprogram. Once the target program has been produced it can be executed on itsown.

22 / 34

Page 25: General concepts of programming languages

Compilation

At the highest level of abstraction, the compilation and execution of a program ina high-level language should look something like this:

Source program

Compiler

Target programInput Output

The compiler translates the high-level source program into an equivalent targetprogram. Once the target program has been produced it can be executed on itsown.

22 / 34

Page 26: General concepts of programming languages

Compilation

At the highest level of abstraction, the compilation and execution of a program ina high-level language should look something like this:

Source program

Compiler

Target programInput Output

The compiler translates the high-level source program into an equivalent targetprogram. Once the target program has been produced it can be executed on itsown.

22 / 34

Page 27: General concepts of programming languages

Compilation

At the highest level of abstraction, the compilation and execution of a program ina high-level language should look something like this:

Source program

Compiler

Target program

Input Output

The compiler translates the high-level source program into an equivalent targetprogram. Once the target program has been produced it can be executed on itsown.

22 / 34

Page 28: General concepts of programming languages

Compilation

At the highest level of abstraction, the compilation and execution of a program ina high-level language should look something like this:

Source program

Compiler

Target programInput Output

The compiler translates the high-level source program into an equivalent targetprogram. Once the target program has been produced it can be executed on itsown.

22 / 34

Page 29: General concepts of programming languages

Compilation

At the highest level of abstraction, the compilation and execution of a program ina high-level language should look something like this:

Source program

Compiler

Target programInput Output

The compiler translates the high-level source program into an equivalent targetprogram. Once the target program has been produced it can be executed on itsown.

22 / 34

Page 30: General concepts of programming languages

Interpretation

An alternative style of implementation for high-level languages is known asinterpretation.

Source program

InterpreterInput Output

Unlike a compiler, an interpreter must control the execution of the applica-tion. In fact, the interpreter implements a virtual machine whose “machinelanguage” is the high-level programming language. The interpreter readsstatements in that language one at a time, executing them as it goes along.

23 / 34

Page 31: General concepts of programming languages

Interpretation

An alternative style of implementation for high-level languages is known asinterpretation.

Source program

InterpreterInput Output

Unlike a compiler, an interpreter must control the execution of the applica-tion. In fact, the interpreter implements a virtual machine whose “machinelanguage” is the high-level programming language. The interpreter readsstatements in that language one at a time, executing them as it goes along.

23 / 34

Page 32: General concepts of programming languages

Interpretation

An alternative style of implementation for high-level languages is known asinterpretation.

Source program

Interpreter

Input Output

Unlike a compiler, an interpreter must control the execution of the applica-tion. In fact, the interpreter implements a virtual machine whose “machinelanguage” is the high-level programming language. The interpreter readsstatements in that language one at a time, executing them as it goes along.

23 / 34

Page 33: General concepts of programming languages

Interpretation

An alternative style of implementation for high-level languages is known asinterpretation.

Source program

InterpreterInput Output

Unlike a compiler, an interpreter must control the execution of the applica-tion. In fact, the interpreter implements a virtual machine whose “machinelanguage” is the high-level programming language. The interpreter readsstatements in that language one at a time, executing them as it goes along.

23 / 34

Page 34: General concepts of programming languages

Interpretation

An alternative style of implementation for high-level languages is known asinterpretation.

Source program

InterpreterInput Output

Unlike a compiler, an interpreter must control the execution of the applica-tion. In fact, the interpreter implements a virtual machine whose “machinelanguage” is the high-level programming language. The interpreter readsstatements in that language one at a time, executing them as it goes along.

23 / 34

Page 35: General concepts of programming languages

Compilation vs. interpretation

In general, interpretation leads to greater flexibility and better diagnos-tics.

Better to detect errors.

More portability of code.

Compilation generally leads to better performance.

Better execution time.

24 / 34

Page 36: General concepts of programming languages

Compilation vs. interpretation

In general, interpretation leads to greater flexibility and better diagnos-tics.

Better to detect errors.

More portability of code.

Compilation generally leads to better performance.

Better execution time.

24 / 34

Page 37: General concepts of programming languages

Compilation vs. interpretation

In general, interpretation leads to greater flexibility and better diagnos-tics.

Better to detect errors.

More portability of code.

Compilation generally leads to better performance.

Better execution time.

24 / 34

Page 38: General concepts of programming languages

Compilation vs. interpretation

In general, interpretation leads to greater flexibility and better diagnos-tics.

Better to detect errors.

More portability of code.

Compilation generally leads to better performance.

Better execution time.

24 / 34

Page 39: General concepts of programming languages

Compilation vs. interpretation

In general, interpretation leads to greater flexibility and better diagnos-tics.

Better to detect errors.

More portability of code.

Compilation generally leads to better performance.

Better execution time.

24 / 34

Page 40: General concepts of programming languages

Compilation vs. interpretation

In general, interpretation leads to greater flexibility and better diagnos-tics.

Better to detect errors.

More portability of code.

Compilation generally leads to better performance.

Better execution time.

24 / 34

Page 41: General concepts of programming languages

A more recent translation process

While the conceptual difference between compilation and interpretation isclear, most language implementations include a mixture of both. Theytypically look like this:

Source program

Translator

Intermediate program

Virtual machineInput Output

25 / 34

Page 42: General concepts of programming languages

A more recent translation process

While the conceptual difference between compilation and interpretation isclear, most language implementations include a mixture of both. Theytypically look like this:

Source program

Translator

Intermediate program

Virtual machineInput Output

25 / 34

Page 43: General concepts of programming languages

A more recent translation process

While the conceptual difference between compilation and interpretation isclear, most language implementations include a mixture of both. Theytypically look like this:

Source program

Translator

Intermediate program

Virtual machineInput Output

25 / 34

Page 44: General concepts of programming languages

A more recent translation process

While the conceptual difference between compilation and interpretation isclear, most language implementations include a mixture of both. Theytypically look like this:

Source program

Translator

Intermediate program

Virtual machineInput Output

25 / 34

Page 45: General concepts of programming languages

A more recent translation process

While the conceptual difference between compilation and interpretation isclear, most language implementations include a mixture of both. Theytypically look like this:

Source program

Translator

Intermediate program

Virtual machine

Input Output

25 / 34

Page 46: General concepts of programming languages

A more recent translation process

While the conceptual difference between compilation and interpretation isclear, most language implementations include a mixture of both. Theytypically look like this:

Source program

Translator

Intermediate program

Virtual machineInput Output

25 / 34

Page 47: General concepts of programming languages

Abstraction

Data. It refers to the easiness of a programming language to describeand manipulate the data.

Control. It refers to the tools the programming language provides tocontrol de flow of the instructions.

Modular. It refers to the way in which the programming languagestructures the code of a program.

26 / 34

Page 48: General concepts of programming languages

Data abstraction

Objects.

Abstract data types.

User defined data types.

Structured data: arrays, registers, sets, files.

Atomic data / scalars: integers, reals, characters,booleans.

Memory: bits and bytes.

27 / 34

Page 49: General concepts of programming languages

Data abstraction

Objects.

Abstract data types.

User defined data types.

Structured data: arrays, registers, sets, files.

Atomic data / scalars: integers, reals, characters,booleans.

Memory: bits and bytes.

27 / 34

Page 50: General concepts of programming languages

Data abstraction

Objects.

Abstract data types.

User defined data types.

Structured data: arrays, registers, sets, files.

Atomic data / scalars: integers, reals, characters,booleans.

Memory: bits and bytes.

27 / 34

Page 51: General concepts of programming languages

Data abstraction

Objects.

Abstract data types.

User defined data types.

Structured data: arrays, registers, sets, files.

Atomic data / scalars: integers, reals, characters,booleans.

Memory: bits and bytes.

27 / 34

Page 52: General concepts of programming languages

Data abstraction

Objects.

Abstract data types.

User defined data types.

Structured data: arrays, registers, sets, files.

Atomic data / scalars: integers, reals, characters,booleans.

Memory: bits and bytes.

27 / 34

Page 53: General concepts of programming languages

Data abstraction

Objects.

Abstract data types.

User defined data types.

Structured data: arrays, registers, sets, files.

Atomic data / scalars: integers, reals, characters,booleans.

Memory: bits and bytes.

27 / 34

Page 54: General concepts of programming languages

Control abstraction

Recursion.

Loop structures: while, do, repeat, for.

Decision structures: it-then, switch-case.

Jumps to code lines: goto, gosub.

Sequences, conditional and unconditional jumps tolocations in memory: jump, call.

28 / 34

Page 55: General concepts of programming languages

Control abstraction

Recursion.

Loop structures: while, do, repeat, for.

Decision structures: it-then, switch-case.

Jumps to code lines: goto, gosub.

Sequences, conditional and unconditional jumps tolocations in memory: jump, call.

28 / 34

Page 56: General concepts of programming languages

Control abstraction

Recursion.

Loop structures: while, do, repeat, for.

Decision structures: it-then, switch-case.

Jumps to code lines: goto, gosub.

Sequences, conditional and unconditional jumps tolocations in memory: jump, call.

28 / 34

Page 57: General concepts of programming languages

Control abstraction

Recursion.

Loop structures: while, do, repeat, for.

Decision structures: it-then, switch-case.

Jumps to code lines: goto, gosub.

Sequences, conditional and unconditional jumps tolocations in memory: jump, call.

28 / 34

Page 58: General concepts of programming languages

Control abstraction

Recursion.

Loop structures: while, do, repeat, for.

Decision structures: it-then, switch-case.

Jumps to code lines: goto, gosub.

Sequences, conditional and unconditional jumps tolocations in memory: jump, call.

28 / 34

Page 59: General concepts of programming languages

Modular abstraction

Objects.

Functions and procedures.

Sequences of instructions with jumps tosubroutines: gosub, endsub.

Sequences of instructions with jumps to locationsin memory with return: ret, call.

29 / 34

Page 60: General concepts of programming languages

Modular abstraction

Objects.

Functions and procedures.

Sequences of instructions with jumps tosubroutines: gosub, endsub.

Sequences of instructions with jumps to locationsin memory with return: ret, call.

29 / 34

Page 61: General concepts of programming languages

Modular abstraction

Objects.

Functions and procedures.

Sequences of instructions with jumps tosubroutines: gosub, endsub.

Sequences of instructions with jumps to locationsin memory with return: ret, call.

29 / 34

Page 62: General concepts of programming languages

Modular abstraction

Objects.

Functions and procedures.

Sequences of instructions with jumps tosubroutines: gosub, endsub.

Sequences of instructions with jumps to locationsin memory with return: ret, call.

29 / 34

Page 63: General concepts of programming languages

Parameter passing

When a procedure body is executed, its formal parameter is bound to adenoted value. Where does that value come from? It must be passed fromthe actual parameter in the procedure call.

By value.

By reference.

By need.

30 / 34

Page 64: General concepts of programming languages

Call by value

void callByValue(int x, int y){

x = x + 1;y = y + 1;

}

int main(){

int a = 1;callByValue(a, a);

}

31 / 34

Page 65: General concepts of programming languages

Call by reference

void callByReference(int &x, int &y){

x = x + 1;y = y + 1;

}

int main(){

int a = 1;callByReference(a, a);

}

32 / 34

Page 66: General concepts of programming languages

Call by need (lazy evaluation)

Under lazy evaluation, an operand in a procedure call is not evaluateduntil it is needed by the procedure body. If the body never refers tothe parameter, then there is no need to evaluate it.

void callByNeed(int x, int y){

x = x + 1;}

int neverEndsIfPositive(int x){

while(x > 0){

x++;}

}

int main(){

int a = 1;callByNeed(a, neverEndsIfPositive(10));

}

33 / 34

Page 67: General concepts of programming languages

Bibliography

[Gabbrielli and Martini, 2010] Gabbrielli, M. and Martini, S. (2010).Programming Languages: Principles and Paradigms.Springer Publishing Company, Incorporated, 1st edition.

[Tucker and Noonan, 2001] Tucker, Jr., A. B. and Noonan, R. E. (2001).Programming Languages: Principles and Paradigms.McGraw-Hill Higher Education, 2nd edition.

34 / 34