Programming Languages (TC-2006) General concepts of programming languages Jos´ e Carlos Ortiz Bayliss [email protected]
Programming Languages(TC-2006)
General concepts of programming languages
Jose Carlos Ortiz [email protected]
Contents
1 Introduction
2 Classification
3 The translation process
4 Abstraction
5 Parameter passing
2 / 34
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
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
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
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
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
The classification of programming languages
Based on how they express solutions.
Based on their paradigm.
Based on their abstraction level.
6 / 34
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
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
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
Classification based on their paradigm
Imperative.
Declarative.
Structured.
Object oriented.
Functional.
Logic and constraint.
10 / 34
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
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
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
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
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
Classification by abstraction level
Low-level programming languages.
High-level programming languages.
Very high-level programming languages.
16 / 34
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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