View
231
Download
1
Tags:
Embed Size (px)
Citation preview
11
Programming Language Programming Language ConceptsConcepts
Tatjana PetkoviTatjana Petkovićć
[email protected]@utu.fi
22
Programming Language Programming Language PragmaticsPragmatics
Michael ScottMichael Scott
http://www.cs.rochester.edu/http://www.cs.rochester.edu/u/scott/pragmatics/u/scott/pragmatics/
33
ContentsContents
1 Introduction1 Introduction
2 Programming Language Syntax2 Programming Language Syntax
2.1 Specifying Syntax 2.1 Specifying Syntax
2.2 Recognizing Syntax 2.2 Recognizing Syntax
2.3* Theoretical Foundations 2.3* Theoretical Foundations
44
3 Names, Scopes, and Bindings3 Names, Scopes, and Bindings
3.1 The Notion of Binding Time 3.1 The Notion of Binding Time
3.2 Object Lifetime and Storage 3.2 Object Lifetime and Storage Management Management
3.3 Scope Rules 3.3 Scope Rules
3.4 The Binding of Referencing 3.4 The Binding of Referencing Environments Environments
3.5 Overloading and Related Concepts 3.5 Overloading and Related Concepts
3.6 Naming-Related Pitfalls in Language 3.6 Naming-Related Pitfalls in Language Design Design
55
4 Semantic Analysis4 Semantic Analysis
5 Assembly-Level Computer Architecture5 Assembly-Level Computer Architecture
6 Control Flow6 Control Flow
6.1 Expression Evaluation 6.1 Expression Evaluation
6.2 Structured and Unstructured Flow 6.2 Structured and Unstructured Flow
6.3 Sequencing 6.3 Sequencing
6.4 Selection 6.4 Selection
6.5 Iteration 6.5 Iteration
6.6 Recursion 6.6 Recursion
66
7 Data Types7 Data Types
7.1 Type Systems 7.1 Type Systems
7.2 Type Checking 7.2 Type Checking
7.3 Records (Structures) and Variants 7.3 Records (Structures) and Variants (Unions) (Unions)
7.4 Arrays 7.4 Arrays
7.5 Strings 7.5 Strings
7.6 Sets 7.6 Sets
7.7 Pointers and Recursive Types 7.7 Pointers and Recursive Types
7.8 Lists 7.8 Lists
77
8 Subroutines and Control Abstraction8 Subroutines and Control Abstraction
8.1 Review of Stack Layout 8.1 Review of Stack Layout
8.2 Calling Sequences 8.2 Calling Sequences
8.3 Parameter Passing 8.3 Parameter Passing
8.4 Generic Subroutines and Modules 8.4 Generic Subroutines and Modules
8.5 Exception Handling 8.5 Exception Handling
8.6 Coroutines 8.6 Coroutines
88
99 Building a Runnable ProgramBuilding a Runnable Program
10 Data Abstraction and Object 10 Data Abstraction and Object OrientationOrientation
11 Alternative Programming Models: 11 Alternative Programming Models: Functional and Logic LanguagesFunctional and Logic Languages
12 Concurrency12 Concurrency
13 Code Improvement13 Code Improvement
99
IntroductionIntroduction Computing devices:Computing devices:
Mechanical:Mechanical: Fingers, abacusFingers, abacus Blaise Pascal 1642 +-Blaise Pascal 1642 +- Gottfried Wilhelm von Leibnitz +-*/Gottfried Wilhelm von Leibnitz +-*/ Charles Babbage 1832 programmableCharles Babbage 1832 programmable
ElectronicalElectronical::COLOSSUS 1943COLOSSUS 1943
ENIAC (Electronic Numerical Integrator and Computer) ENIAC (Electronic Numerical Integrator and Computer) 19461946
1010
Machine languageMachine language
binary system - John Von Neumann binary system - John Von Neumann
GCD for MIPS R4000GCD for MIPS R4000
1111
coding in the true meaning of the wordcoding in the true meaning of the word code is notcode is not
reusable: monolithic ‘structure’reusable: monolithic ‘structure’ relocatable: consider adding one instruction in relocatable: consider adding one instruction in
the middlethe middle readablereadable
practically impossible to create large practically impossible to create large programsprograms
1212
Assembly languagesAssembly languages
assemblerassembler
GCD GCD
1313
Assembler Assembler translator from symbolic language to machine translator from symbolic language to machine
language (one-to-one mapping)language (one-to-one mapping) tool to assemble the symbolic program in the tool to assemble the symbolic program in the
machinemachine
AdvantagesAdvantages relocatable & reusable (copy) programsrelocatable & reusable (copy) programs macro expansion macro expansion
first step towards higher-level programmingfirst step towards higher-level programming
larger programs (like operating systems) larger programs (like operating systems) possiblepossible
1414
But,But, each kind of computer has its owneach kind of computer has its own programmers must learn to think like programmers must learn to think like
computerscomputers maintenance of larger programs is difficultmaintenance of larger programs is difficult
Higher-level languagesHigher-level languages portabilityportability natural notation (for anything)natural notation (for anything) support to software developmentsupport to software development
1515
Machine independent languagesMachine independent languages Fortran 1956Fortran 1956 Cobol 1959Cobol 1959 Algol 1958, 1960Algol 1958, 1960 ......
compilerscompilers
1616
1717
Fortran Fortran (Mathematical (Mathematical ForFormula mula TranTranslator)slator) Backus, 1957 Backus, 1957 IBM IBM compilation instead of translationcompilation instead of translation language for scientific computinglanguage for scientific computing
most important task in those daysmost important task in those days
efficiency important to replace assemblersefficiency important to replace assemblers introduced many important language introduced many important language
concepts that are still in useconcepts that are still in use Fortran 99 array operationsFortran 99 array operations
1818
Cobol Cobol ((CoCommon mmon BBusiness usiness OOriented riented LLanguage)anguage) 19591959 COBOL commetee (IBM, Honeywell, Flow-COBOL commetee (IBM, Honeywell, Flow-
Matic,...)Matic,...) at some point 60% of all business applicationsat some point 60% of all business applications
1919
Algol 60 (Algol 60 (AlgoAlgorithmic rithmic LLanguage)anguage) the first European languagethe first European language never very present in practicenever very present in practice introduced modern concepts introduced modern concepts big influence on further developmentbig influence on further development AdaAda
Basic Basic ((BBeginers eginers AAll-purpose ll-purpose SSymbolic ymbolic IInstruction nstruction CCode)ode) 19611961 popular in the eightiespopular in the eighties Visul Basic, Visual Basic for ApplicationVisul Basic, Visual Basic for Application
2020
PL/1 (PL/1 (PProgramming rogramming LLanguage One)anguage One) general-purposegeneral-purpose meant to replace Fortran, Cobol and Algolmeant to replace Fortran, Cobol and Algol
Algol 68Algol 68 the same idea of universalitythe same idea of universality
too complextoo complex hardware could not support themhardware could not support them Algol 68 compiler never completely realizedAlgol 68 compiler never completely realized
2121
PascalPascal N. WirthN. Wirth late sixtieslate sixties simple to learn, easy to use, ...simple to learn, easy to use, ... introduces subrange and enumeration types, introduces subrange and enumeration types,
unified structures, unionsunified structures, unions ’’Pascal-like’ notionPascal-like’ notion Turbo PascalTurbo Pascal free availabilityfree availability
ModulaModula
2222
an analysis from the beginning of seventiesan analysis from the beginning of seventies for the next 15-20 years predictedfor the next 15-20 years predicted software cost not in proportion to hardware costsoftware cost not in proportion to hardware cost about 450 languagesabout 450 languages
ADAADA 19831983 new attempt for the universal languagenew attempt for the universal language US DODUS DOD too big expectations never fulfilledtoo big expectations never fulfilled theoreticaly significant, data types, moduls, theoreticaly significant, data types, moduls,
abstraction, concurrency, exception handlingabstraction, concurrency, exception handling
2323
CC 19701970 UNIX, system software programmingUNIX, system software programming 1978 D. M. Ritchi and B. W. Kernighan1978 D. M. Ritchi and B. W. Kernighan 1983 ANSI C1983 ANSI C close to assembly languagesclose to assembly languages not reliable, weak type checking, no dynamic not reliable, weak type checking, no dynamic
semantic checks semantic checks C++C++
2424
object-oriented languagesobject-oriented languages data abstractiondata abstraction objects, classesobjects, classes inheritance, polimorphisminheritance, polimorphism roots in Simula 67roots in Simula 67 Smalltalk 80, Eiffel, Omega, Oberon, C++, Smalltalk 80, Eiffel, Omega, Oberon, C++,
Delta, JavaDelta, Java visul environment, interactive, visul environment, interactive, events driven programming: Visual Basic, events driven programming: Visual Basic,
DelphiDelphi
2525
Language classificationLanguage classification imperativeimperative
howhow the computer should solve the problem the computer should solve the problem first do this, then repeat that, then branch there...first do this, then repeat that, then branch there...
procedural languages (Pascal, C, Basic, ...)procedural languages (Pascal, C, Basic, ...) ‘‘computing via side-effects’computing via side-effects’ Von Neumann architecture (1946)Von Neumann architecture (1946) object-orientedobject-oriented
2626
declarative languagesdeclarative languages program = description of the problemprogram = description of the problem
a formal statement of a formal statement of whatwhat is the problem is the problem closer to humans than computerscloser to humans than computers
functional languagesfunctional languages Lisp, 1958Lisp, 1958 λλ-calculus, Church 1930-calculus, Church 1930 computing without variablescomputing without variables
logic programminglogic programming predicate logic, Fredge 1871predicate logic, Fredge 1871 Prolog, seventiesProlog, seventies computing with relationscomputing with relations
2727
The programming language The programming language spectrumspectrum
2828
sequentialsequential concurrentconcurrent
in conjuction with sequential (Fortran, C,...)in conjuction with sequential (Fortran, C,...) explicite (Java, Ada, Modula-3)explicite (Java, Ada, Modula-3)
2929
Why so many languages?Why so many languages? evolutionevolution
goto goto while, case, ... while, case, ... object-oriented object-oriented
special purposesspecial purposessymbolic data – Lispsymbolic data – Lispcharacter strings – Snobol, Iconcharacter strings – Snobol, Iconlow-level programming – Clow-level programming – Cnumeric data – Fortrannumeric data – Fortranlogic programming - Prologlogic programming - Prolog
personal preferencepersonal preferenceiteration : recursioniteration : recursionpointers : implicit dereferencingpointers : implicit dereferencing
3030
What makes a language successful?What makes a language successful?
expressive powerexpressive power
ease of use for a noviceease of use for a noviceBasic, Logo, Pascal, Java, ...Basic, Logo, Pascal, Java, ...
ease of implementationease of implementation
excellent compilersexcellent compilersFortranFortran
economics, patronage, inertiaeconomics, patronage, inertiaCobolCobol
programming vs. implementationprogramming vs. implementation conceptual clarity vs. implementation efficiencyconceptual clarity vs. implementation efficiency
3131
Language characteristicsLanguage characteristics
formally defined syntax formally defined syntax (grammars, syntax diagrams)(grammars, syntax diagrams)
data types data types (predefined, others)(predefined, others)
data structures data structures (array, record, file, set)(array, record, file, set)
control control (if, case, for, while)(if, case, for, while)
subroutinessubroutines
3232
modules modules abstract data typesabstract data typesdata + procedures + functionsdata + procedures + functionsclosedclosed
concurrency concurrency parallelismparallelism
low-level mechanisms low-level mechanisms to access registers, memory, format datato access registers, memory, format data
exception handling mechanismsexception handling mechanismsI/O proceduresI/O procedures
3333
Evaluating languagesEvaluating languages
readabilityreadability more readable less documentationmore readable less documentation factors: key words .. modularity degreefactors: key words .. modularity degree simplicitysimplicity
num = num + 1num = num + 1num += 1num += 1++num++numnum++num++
3434
readability (still...)readability (still...) orthogonalityorthogonality
small number of concepts and ways to combine small number of concepts and ways to combine themthem
control flowcontrol flow structural languagesstructural languages
data structuresdata structures records more clear than arraysrecords more clear than arrays
syntax syntax begin .. end, if .. fi (end if)begin .. end, if .. fi (end if)
3535
easy of useeasy of use
depends on the applicationdepends on the application simplicity and orthogonalitysimplicity and orthogonality
programmers accept limitted number of new conceptsprogrammers accept limitted number of new concepts small numbers of concepts and constructssmall numbers of concepts and constructs
abstraction supportabstraction support emphasses global characteristicsemphasses global characteristics subroutines, modules, classessubroutines, modules, classes
expressivityexpressivity num = num + 1 or num++num = num + 1 or num++ while or forwhile or for
3636
reliabilityreliability to decrese number of run-time errorsto decrese number of run-time errors early bindingearly binding data typesdata types
explicitly definedexplicitly defined operators types determinedoperators types determined castingcasting
exceptions handlingexceptions handling run-time errors caused by the program or systemrun-time errors caused by the program or system
aliasingaliasing mutual references to the same memory locationmutual references to the same memory location Fortran: equivalenceFortran: equivalence Pascal: pointersPascal: pointers may cause errorsmay cause errors
3737
effectivityeffectivity of a programof a program
important for real-time systemsimportant for real-time systems
of the compilerof the compiler important for often modified programsimportant for often modified programs
overalloverall important for widely used softwareimportant for widely used software
3838
Why to study programming Why to study programming languages?languages?
interesting, practicalinteresting, practical
choose the most appropriate languagechoose the most appropriate language scientific applications, system software, embedded scientific applications, system software, embedded
systems, word processorsystems, word processor C, Fortran, Java, Ada, Visual Basic, Modula-2C, Fortran, Java, Ada, Visual Basic, Modula-2
easier to learn new languageseasier to learn new languages C C →→ C++ C++ →→ Java Java Pascal Pascal →→ Modula-2 Modula-2 common concepts: types, control, naming, abstractioncommon concepts: types, control, naming, abstraction
3939
Our aim is to:Our aim is to: Understand obscure featuresUnderstand obscure features
C: unions, arrays vs. pointers, separate compilation, C: unions, arrays vs. pointers, separate compilation, varargs, ...varargs, ...
understanding the basic concepts is a necessity to understanding the basic concepts is a necessity to understand non-basic onesunderstand non-basic ones
Choose the best alternative depending on Choose the best alternative depending on implementation costsimplementation costs alternative ways of doing the same thingalternative ways of doing the same thing
x * x or of x**2x * x or of x**2 pointer arithmetic or arrayspointer arithmetic or arrays computation vs. memory (function or table)computation vs. memory (function or table)
things to avoidthings to avoid Pascal & value parameters for large typesPascal & value parameters for large types
Make good use of the environmentMake good use of the environment
4040
Simulate features where they do not existSimulate features where they do not exist Fortran (pre -90)Fortran (pre -90)
bad control structures bad control structures use comments & use comments & programmer disciplineprogrammer discipline
no recursionno recursion eliminate recursion eliminate recursion no named constants no named constants use variables use variables
C, PascalC, Pascal no modules no modules use naming & discipline use naming & discipline
Equip with basic knowledge for further Equip with basic knowledge for further study of language design and study of language design and implementation, or interactions of implementation, or interactions of languages with operating systemslanguages with operating systems
4141
Useful in designing command interpreters, Useful in designing command interpreters, programmable editors, text processors, ...programmable editors, text processors, ...
Many system programs are like languagesMany system programs are like languages command shellscommand shells programmable editorsprogrammable editors programmable applicationsprogrammable applications
Many system programs are like compilersMany system programs are like compilers read & analyze configuration files and read & analyze configuration files and
command line optionscommand line options
Easier to use and design such things once Easier to use and design such things once you know about ‘real’ languagesyou know about ‘real’ languages
4242
Compilation and interpretationCompilation and interpretation
4343
InterpretationInterpretation greater flexibilitygreater flexibility better diagnosticsbetter diagnostics excellent source-level debuggerexcellent source-level debugger cope with variables’ sizes, types, namescope with variables’ sizes, types, names write and execute on fly program pieces write and execute on fly program pieces
(Prolog, Lisp)(Prolog, Lisp)
CompilationCompilation better performancebetter performance saves time, memorysaves time, memory
4444
a mixture of botha mixture of both
compilation or interpretation?compilation or interpretation?
4545
preprocessor (in interpreted languages)preprocessor (in interpreted languages)removes comments and white space, forms removes comments and white space, forms tokens, expand abbriviations, identifies high-tokens, expand abbriviations, identifies high-level structureslevel structures
compilationcompilation thorough analysis and nontrivial thorough analysis and nontrivial
transformationtransformation
4646
examplesexamples
Basic, pure interpretedBasic, pure interpreted Fortran, pure compiledFortran, pure compiled
format interpreterformat interpreter
4747
CC preprocessorpreprocessor
removes comments, expands macros, conditional removes comments, expands macros, conditional compilationcompilation
4848
C++C++
early AT&T compilerearly AT&T compiler
4949
PascalPascalearly compilers:early compilers:- a Pascal compiler written in Pascala Pascal compiler written in Pascal- the same compiler in P-codethe same compiler in P-code- a P-code interpreter written in Pascala P-code interpreter written in Pascal
1.1. translate (by hand) the P-code interpreter into a translate (by hand) the P-code interpreter into a local languagelocal language
5050
still both for Pascal, C, other imperativestill both for Pascal, C, other imperative late bindinglate binding
Prolog, LispProlog, Lisp Java, byte code (interpreter or just-in-time Java, byte code (interpreter or just-in-time
compiler)compiler) Assembly languages run on interpreterAssembly languages run on interpreter
some compilers produce C-codesome compilers produce C-code translating automaticaly from one nontrivial translating automaticaly from one nontrivial
language to anotherlanguage to another text processors, query language processors text processors, query language processors
for databasesfor databases
5151
Programming environmentsProgramming environments
Assemblers, debuggers, preprocessors, Assemblers, debuggers, preprocessors, linkers, editors, configuration management linkers, editors, configuration management toolstools
Explicit request of the user (Unix)Explicit request of the user (Unix) Integrated enviroments (Smalltalk, Visual Integrated enviroments (Smalltalk, Visual
Studio env.)Studio env.)
5252
Overview of compilingOverview of compiling
phasesphases
front endfront endfigure out the meaning of the source programfigure out the meaning of the source program
back endback endconstruct target programconstruct target program
5353
5454
passespasses a serialized set of phases a serialized set of phases separate programs, input/output filesseparate programs, input/output files economic memory useeconomic memory use division such thatdivision such that
front end for more than one machinefront end for more than one machine back end for more than one languageback end for more than one language
5555
Lexical and Syntax AnalysisLexical and Syntax Analysis
program gcd (input,output);program gcd (input,output);var i , j : integer;var i , j : integer;beginbegin
read (i, j);read (i, j);while i<>j dowhile i<>j do
if i>j then i := i – jif i>j then i := i – jelse j := j – i;else j := j – i;
writeln (i)writeln (i)end.end.
5656
scannerscanner lexical analysislexical analysis tokens: program, gcd, (, i, ,, j, ), ;, ... , end, .tokens: program, gcd, (, i, ,, j, ), ;, ... , end, . removes comments, tags tokens with line and removes comments, tags tokens with line and
column numberscolumn numbers
parserparser syntactic analysissyntactic analysis parse treeparse tree CFG (context-free grammar)CFG (context-free grammar)
5757
programprogram →→ PROGRAMPROGRAM identifieridentifier ( ( identifier identifier more_identifiers more_identifiers ) ; ) ; blockblock . .
blockblock →→ labels constants types variables subroutineslabels constants types variables subroutines BEGIN BEGIN statement more_statementsstatement more_statements END END
more_identifiersmore_identifiers →→ , , identifier more_identifiersidentifier more_identifiers | | εε
5858
5959
Semantic AnalysisSemantic AnalysisIntermediate code generationIntermediate code generation
meaningmeaning recognizes multiple occurances of an recognizes multiple occurances of an
identifier, tracks types of identifiers and identifier, tracks types of identifiers and expressionsexpressions
symbol tablesymbol table identifier, type, internal structure, scopeidentifier, type, internal structure, scope
6060
6161
checks thatchecks that identifiers defined before usedidentifiers defined before used not used inappropriatellynot used inappropriatelly correct arguments in subrotine callscorrect arguments in subrotine calls arms in CASE distinct constantsarms in CASE distinct constants exist return values for functionsexist return values for functions
semantic action routines invoked by parsersemantic action routines invoked by parser
6262
static semantics (at compile time)static semantics (at compile time)
dynamic semantics (at run time)dynamic semantics (at run time) variables in expressions have valuesvariables in expressions have values pointers refer to valid objectspointers refer to valid objects array subscript is in the boundsarray subscript is in the bounds functions return valuesfunctions return values
exception if a dynamic check failsexception if a dynamic check fails erroneous if a program breaks a rule erroneous if a program breaks a rule
expensive to be checkedexpensive to be checked
6363
parse tree (concrete syntax tree)parse tree (concrete syntax tree)↓↓
syntax tree (abstract syntax tree)syntax tree (abstract syntax tree)decorated by attributes, i.e., pointers from decorated by attributes, i.e., pointers from
identifiers to their symbolic table entriesidentifiers to their symbolic table entries
intermediate form between front and back intermediate form between front and back end:end:
- annotated syntax tree - annotated syntax tree - traversal of some intermediate tree - traversal of some intermediate tree
(resembles asembly language)(resembles asembly language)
6464
Target code generationTarget code generation
code generation:code generation:intermediate form intermediate form → target language→ target language
traverses the symbol table to assign locations to traverses the symbol table to assign locations to variablesvariablestraverses the syntax tree generating loads and storestraverses the syntax tree generating loads and storesarithmetics, tests, branchesarithmetics, tests, branches
6565
6666
Code improvementsCode improvements
more efficientmore efficient quicker and/or less memoryquicker and/or less memory
two phases:two phases: machine independent, on intermediate formmachine independent, on intermediate form target program improvement, register target program improvement, register
distribution, reordering instructionsdistribution, reordering instructions