Programming Language Concepts Tatjana Petković tatpet@utu.fi... Assembler translator from symbolic language to machine language one-to-one mapping tool to assemble the symbolic pr
Trang 1Programming Language
Concepts
Tatjana Petković tatpet@utu.fi
Trang 43 Names, Scopes, and Bindings
3.1 The Notion of Binding Time
3.2 Object Lifetime and Storage
Trang 78 Subroutines and Control Abstraction
8.1 Review of Stack Layout
Trang 89 Building a Runnable Program
10 Data Abstraction and Object
Orientation
11 Alternative Programming Models:
Functional and Logic Languages
12 Concurrency
Trang 9+- Gottfried Wilhelm von Leibnitz +-*/
Charles Babbage 1832 programmable
Electronical:
COLOSSUS 1943
ENIAC (Electronic Numerical Integrator and Computer)
Trang 10 Machine language
binary system - John Von Neumann
GCD for MIPS R4000
Trang 11 coding in the true meaning of the word
code is not
reusable: monolithic ‘structure’
relocatable: consider adding one instruction in
the middle
readable
practically impossible to create large
programs
Trang 12 Assembly languages
assembler
GCD
Trang 13 Assembler
translator from symbolic language to machine
language (one-to-one mapping)
tool to assemble the symbolic program in the
machine
Advantages
relocatable & reusable (copy) programs
macro expansion
first step towards higher-level programming
larger programs (like operating systems)
possible
Trang 14 But,
each kind of computer has its own
programmers must learn to think like
computers
maintenance of larger programs is difficult
Higher-level languages
portability
natural notation (for anything)
support to software development
Trang 15 Machine independent languages
Trang 17 Fortran (Mathematical Formula Translator)
Backus, 1957
IBM
compilation instead of translation
language for scientific computing
most important task in those days
efficiency important to replace assemblers
introduced many important language concepts
that are still in use
Fortran 99 array operations
Trang 18 Cobol (Common Business Oriented Language)
Trang 19 Algol 60 ( Algo rithmic L anguage)
the first European language
never very present in practice
introduced modern concepts
big influence on further development
Ada
Basic (Beginers All-purpose Symbolic
Instruction Code)
1961
popular in the eighties
Visul Basic, Visual Basic for Application
Trang 20 PL/1 ( P rogramming L anguage One)
hardware could not support them
Algol 68 compiler never completely realized
Trang 21 Pascal
N Wirth
late sixties
simple to learn, easy to use,
introduces subrange and enumeration types,
unified structures, unions
’Pascal-like’ notion
Turbo Pascal
free availability
Modula
Trang 22 an analysis from the beginning of seventies
for the next 15-20 years predicted
software cost not in proportion to hardware cost
too big expectations never fulfilled
theoreticaly significant, data types, moduls,
abstraction, concurrency, exception handling
Trang 23 C
1970
UNIX, system software programming
1978 D M Ritchi and B W Kernighan
1983 ANSI C
close to assembly languages
not reliable, weak type checking, no dynamic
semantic checks
C++
Trang 24 visul environment, interactive,
events driven programming: Visual Basic,
Delphi
Trang 25Language classification
imperative
how the computer should solve the problem
first do this, then repeat that, then branch there
procedural languages (Pascal, C, Basic, )
‘computing via side-effects’
Von Neumann architecture (1946)
object-oriented
Trang 26 declarative languages
program = description of the problem
closer to humans than computers
Trang 27The programming language
spectrum
Trang 28 sequential
concurrent
in conjuction with sequential (Fortran, C, )
explicite (Java, Ada, Modula-3)
Trang 29Why so many languages?
evolution
goto while, case, object-oriented
special purposes
symbolic data – Lisp
character strings – Snobol, Icon
low-level programming – C
numeric data – Fortran
logic programming - Prolog
personal preference
iteration : recursion
pointers : implicit dereferencing
Trang 30What makes a language successful?
Basic, Logo, Pascal, Java,
Trang 31Language characteristics
formally defined syntax
(grammars, syntax diagrams)
Trang 32modules
abstract data types
data + procedures + functions
Trang 33Evaluating languages
readability
more readable less documentation
factors: key words modularity degree
simplicity
num = num + 1num += 1
++numnum++
Trang 35
easy of use
depends on the application
simplicity and orthogonality
abstraction support
expressivity
Trang 38
Why to study programming
languages?
interesting, practical
choose the most appropriate language
scientific applications, system software, embedded
systems, word processor
C, Fortran, Java, Ada, Visual Basic, Modula-2
easier to learn new languages
C → C++ → Java
Pascal → Modula-2
Trang 39Our aim is to:
Understand obscure features
C: unions, arrays vs pointers, separate compilation,
varargs,
understanding the basic concepts is a necessity to
understand non-basic ones
Choose the best alternative depending on
implementation costs
alternative ways of doing the same thing
things to avoid
Make good use of the environment
Trang 40 Simulate features where they do not exist
Fortran (pre -90)
bad control structures use comments &
programmer discipline
no recursion eliminate recursion
no named constants use variables
C, Pascal
no modules use naming & discipline
Equip with basic knowledge for further
study of language design and
implementation, or interactions of
Trang 41 Useful in designing command interpreters,
programmable editors, text processors,
Many system programs are like languages
command shells
programmable editors
programmable applications
Many system programs are like compilers
read & analyze configuration files and
command line options
Easier to use and design such things once you know about ‘real’ languages
Trang 42Compilation and interpretation
Trang 43 Interpretation
greater flexibility
better diagnostics
excellent source-level debugger
cope with variables’ sizes, types, names
write and execute on fly program pieces
Trang 44a mixture of both
compilation or interpretation?
Trang 45 preprocessor (in interpreted languages)
removes comments and white space, forms tokens, expand abbriviations, identifies high-level structures
compilation
thorough analysis and nontrivial
transformation
Trang 46 Basic, pure interpreted
Fortran, pure compiled
format interpreter
Trang 47 C
removes comments, expands macros, conditional compilation
Trang 48 C++
early AT&T compiler
Trang 49 Pascal
early compilers:
- a Pascal compiler written in Pascal
- the same compiler in P-code
- a P-code interpreter written in Pascal
1 translate (by hand) the P-code interpreter into a
local language
Trang 50 still both for Pascal, C, other imperative
late binding
Prolog, Lisp
Java, byte code (interpreter or just-in-time
compiler)
Assembly languages run on interpreter
some compilers produce C-code
translating automaticaly from one nontrivial
language to another
text processors, query language processors
for databases
Trang 51Programming environments
Assemblers, debuggers, preprocessors,
linkers, editors, configuration management tools
Explicit request of the user (Unix)
Integrated enviroments (Smalltalk, Visual
Studio env.)
Trang 54 passes
a serialized set of phases
separate programs, input/output files
economic memory use
division such that
front end for more than one machine
back end for more than one language
Trang 55Lexical and Syntax Analysis
writeln (i)
end.
Trang 56 scanner
lexical analysis
tokens: program, gcd, (, i, ,, j, ), ;, , end,
removes comments, tags tokens with line and
Trang 57program → PROGRAM identifier ( identifier
BEGIN statement more_statements END
Trang 59Semantic Analysis Intermediate code generation
meaning
recognizes multiple occurances of an
identifier, tracks types of identifiers and expressions
symbol table
identifier, type, internal structure, scope
Trang 61 checks that
identifiers defined before used
not used inappropriatelly
correct arguments in subrotine calls
arms in CASE distinct constants
exist return values for functions
semantic action routines invoked by parser
Trang 62 static semantics (at compile time)
dynamic semantics (at run time)
variables in expressions have values
pointers refer to valid objects
array subscript is in the bounds
functions return values
exception if a dynamic check fails
erroneous if a program breaks a rule
Trang 63parse tree (concrete syntax tree)
↓ syntax tree (abstract syntax tree)
decorated by attributes, i.e., pointers from identifiers to their symbolic table entries
intermediate form between front and back end:
- annotated syntax tree
- traversal of some intermediate tree
(resembles asembly language)
Trang 64Target code generation
code generation:
intermediate form → target language
traverses the symbol table to assign locations to
variables
traverses the syntax tree generating loads and stores arithmetics, tests, branches
Trang 66Code improvements
more efficient
quicker and/or less memory
two phases:
machine independent, on intermediate form
target program improvement, register
distribution, reordering instructions