© A Barnes, 2006 1 CS2130/Ilect 1 CS2130 Program Language Concepts Lecture 1 -- Introduction Module Outline: Credits: 10 Aims of the Module Students should gain: 1. knowledge and understanding of the basic sequential imperative paradigm (including reasonable practical ability to use one such language 2. understanding of programming language concepts and structure, emphasising the role of abstraction for practical software design and construction 3. comparison of the standard imperative and object-oriented paradigms. 4. knowledge and understanding of concurrent programming and the need for synchronisation of access to shared resources 5. understanding of software concepts and acquisition of associated skills that will be of value in a range of subsequent modules. Summary of Content 1. Introduction to programming in a standard imperative language (e.g. C or Ada). 2. Fundamental programming language concepts: values, types; binding, scope, visibility, variables, storage, lifetime; expressions, commands, state, function and procedure abstraction, parameter mechanisms. 3. Review of encapsulation and information hiding: packages, abstract objects, abstract data types, reusability via generic abstraction. 4. Concurrent programming and synchronisation in Ada and Java, comparison with other languages Topics will be illustrated by examples and practical exercises using a variety of appropriate languages, e.g. Pascal, Java, C, C++, Ada 95 Ada 2005, Lisp, Prolog, SR. Summary of Methods and Frequency of Teaching 3 hours lectures per week for weeks 1-3. 2 hours per week lectures from week 4 onwards 1 hour practical class per fortnight from week 2 onwards Summary of Methods of Assessment Written examination: 75% (1.5 hours) Practical assignment: 25% Students will be expected to complete practical programming exercises on all major topics covered. Practical work will be assessed through coursework assignments and related examination questions. © A Barnes, 2006 2 CS2130/Ilect 1 Prerequisites CS1310 Java Programming Foundations CS1410 Java Program Development CS2310 Data Structures and Algorithms with Java Corequisites CS2230 Operating Systems Reading DA Watt (with W Findlay) Programming Language Design Concepts Wiley (2004) R W Sebasta Concepts of Programming Languages (7th ed.) Pearson Educ. 2004 or (5th/6th editions, 2002/3). L Wilson & R Clark, Comparative Programming Languages (3rd ed.), Addison-Wesley, 2001 H Bal & D Grune, Programming Language Essentials, Addison-Wesley, 1994 A Wellings, Concurrent & Real-Time Programming in Java, Wiley, 2004 D A Watt, Programming Language Concepts and Paradigms, Prentice Hall, 1990 J G P Barnes, Programming in Ada 95 (2nd ed.), Addison-Wesley, 1998 A Burns & A Wellings, Real-Time Systems & Programming Languages, Pearson /Addison- Wesley (3rd ed. ) 2001. Notes In addition to the reading list it may be necessary to consult text-books or programming manuals for other languages (particularly Ada, Java and C/C++). There will be one continuous assessment exercise distributed in towards the middle of term 2 and handed in around week 7 or 8 of the term (dates to be confirmed). The exact form of the continuous assessment remains to be decided, but it is likely to be an individual programming exercise(s) in Java (and possibly Ada or C). Students will be informed in due course of the arrangements. A number of exercises will be set throughout the course. Completion and submission of most of these exercises will not be required and will not be formally assessed. However they form an integral part of the course and you are strongly advised to complete these. Some of the exercises will form the basis of examination questions and other exercises need to be mastered in order to understand material later in the module. Students resitting the module with attendance should note that the module content has changed considerably from last year. As current second year students have studied Java rather than Ada, there will be more programming examples in Java. There will be a brief introduction to standard imperative programming languages (primarily Ada and C). Students resitting the module with attendance will be required to complete the continuous assessment even if their CA mark at the first attempt was satisfactory. There will be no formal tutorials or problem classes for this module. However there will be a number of practical (lab) classes in MB473 You will be split into four smaller groups for these lab classes. Owing to staff shortages the lab classes will be fortnightly with two groups having classes in odd numbered weeks and the other two groups in even numbered weeks. Labs will start in the second week of teaching period 2 (week 3 of term 2). If you have questions about the module I will be available in the scheduled lab periods (whether or not your class is scheduled for that week) and in my office from 16.00-18.00 on © A Barnes, 2006 3 CS2130/Ilect 1 Monday afternoons. I will NOT be available at other times except by prior email appointment (Email: barnesa@aston.ac.uk). I will from time to time send email containing information relevant to this module to the mailing-lists for students enrolled on this module seas_cs2130@aston.ac.uk It is your responsibility to look at your email regularly each week. Note that email will not be sent to individual student email accounts at (say) hotmail.com or other ISP's. Spare copies of the lecture hand-outs, problem sheets etc. will be placed on the table outside my office (MB212B). If you miss a lecture then you should collect ONE copy from there. Please do not collect multiple copies at lectures for 'absent friends'. A sufficient number of hand-outs will be duplicated for everyone registered for the module, when these are exhausted no more will be duplicated. Students who miss lectures for a substantial period through illness or other good cause should inform the CS office or their year tutor of their absence (you should do this as a matter of course anyway). If I am informed of this absence in good time I will endeavour to save a copy of the hand-outs for the student concerned. Copies of the hand-outs will be available on-line in MS-Word format and PDF in the following location: http://www-users.aston.ac.uk/~barnesa/cs2130/ or in meaningfully named links from the above. The simplest way to reach this site is by following the CS2130 link from my home page: http://www-users.aston.ac.uk/~barnesa// Other material (example programs and some exercise solutions etc.) will be placed in the same locations from time to time. There will be no material for this module on WebCT. As mistakes and omissions in the notes are detected correction and addendum sheets will be circulated and the on-line versions will be updated. Reasons for Studying Concepts of Programming Languages Increased capacity to express programming ideas. If a programmer's vocabulary and knowledge of programming concepts is rudimentary and lacking in depth of abstraction, then that programmer will be limited in the form of control structures and data structures that they can use. It is difficult to conceptualise various programming constructs unless one has the means to describe them verbally and/or in writing. Also the ability to read and understand technical language manuals or programs written by other (better educated) programmers will be hampered. Even if a particular language does not support a particular construct directly, knowledge of the construct in another language and its advantages may motivate a programmer to simulate the construct (and so enjoy (at least some of) its benefits. Increased ability to learn new programming languages The time taken to learn a new programming language and use it effectively can be substantially reduced if one is aware of similar constructs and concepts in other languages that one already knows. If one has a good theoretical grounding and understands the © A Barnes, 2006 4 CS2130/Ilect 1 advantages and disadvantages of certain constructs, then potential pitfalls and misunderstandings can often be avoided. Improved background for choosing a programming language. Knowledge of the strengths and weaknesses of available programming languages can aid in the choice of the most appropriate language for a particular programming project. Different languages may be appropriate for different types of project (Ada for safety critical applications, Java for Web-based programming, a scripting language such as Python for rapid prototyping. Better understanding of the significance of implementation. Sometimes the seemingly arbitrary limitations of a particular programming language construct can be traced to a difficulty in an efficient implementation of the construct in its full generality. Increased ability to design good quality new languages. For example avoiding designing new languages with some of the well-known shortcomings of existing languages when there are equally simple constructs without these problems. Overall advancement of computing and an improvement in the quality of software. Traditionally the most popular languages have not always been the best languages available for the software development process. Requirements of a Programming Language In this course we will only consider high-level programming languages, that is languages which are (more or less) machine independent. Hence we will not consider assembly language programming. A programming language should be universal, that is every problem capable of solution must have a solution expressible in that language. This is not such a formidable requirement as any algorithm can be expressed in terms of a very small set of control structures: namely sequencing, selection with IF and iteration with WHILE (Böhm and Jacopini, 1966)1. Other programming constructs (CASE, FOR, functions and procedures, packages etc.) may be more convenient and/or lead to better quality (more robust) code but they are not strictly necessary A programming language should be reasonably natural for problem solving at least in the area for which it is designed. A language whose only data types are numbers (integers and reals) and arrays might be fine for 'number-crunching' applications but would not be natural for (say) text processing A programming language must be implementable on a computer. Mathematical notation in its full generality is not implementable; as the work of Post and Turing showed there are functions one can define rigorously in mathematical notation which are not computable by a terminating algorithmic scheme. A programming language must be implementable on a computer in a reasonably efficient manner. Difficulties in implementation of languages such as Algol 68 and Ada 83 and hence the lack of availability of good quality compilers at a reasonable cost meant that they did not become as popular as simpler languages such as C and Pascal although arguably they were more powerful, better structured languages. Of course efficiency is a relative concept: a C or Pascal program might be 2-5 times slower than an assembly program expressing the 1 Böhm, C and Jacopini, G. Communications of the ACM 9, 366–71 (1966). © A Barnes, 2006 5 CS2130/Ilect 1 same algorithm whereas a Prolog or SmallTalk program might be 50 times slower than an assembly language version. The programmer is prepared to accept such a large efficiency 'hit' because of the greater expressivity of these languages (at least for some types of problem) leading to a substantially reduced program development time compared with lower level languages. Language Processors High level languages are implemented by compiling them into machine instructions that may be directly executed on a particular computer architecture or by interpreting each command as it is encountered at run-time or by a combination of the two by compiling the source program into some intermediate form which is then interpreted. Languages such as C, C++ and Ada are compiled whereas Lisp2 and scripting languages such as Perl and Python are normally interpreted. Typically an interpreted program runs more slowly by a factor of ten or so compared with a compiled version of the same program. In Java the source code is compiled to Java byte-code3. Java byte-code is not directly executable on any existing architecture, it is interpreted on the Java Virtual Machine (JVM). Java byte-code can be regarded as the machine-code of a virtual architecture. Emacs Lisp (which is used to implement all extensions to the basic Emacs editor) uses a similar system: functions are byte-compiled into Emacs byte-code and later executed on the Emacs virtual machine. Compilation to an intermediate language speeds up the interpretation process at run time and increases program portability; the compilation to byte-code is machine- independent and the program can be run on any machine on which a virtual machine for that byte-code has been implemented. Note that it is possible to compile languages other than Java to Java byte code which can then be executed by a JVM. For example the JGnat system can compile Ada to Java byte-code. Similar tools exist for other languages e.g. Eiffel and C. Compilers, interpreters and other programs which process other computer programs, are referred to as language processors. Some languages notably C and C++ have a pre-processor which is called before the compiler proper which performs such tasks as removing comments and including text from standard library header files. Other language processors include language sensitive editors pretty-printers Examples of language sensitive editors are Emacs with its various language modes, AdaGide for Ada or the editors in Eclipse or JDeveloper for Java. These automatically indent code, render keywords in a different font or colour, detect missing semi-colons etc. Pretty-printers specially format programs (so that, for example) keywords are printed in a different font) and then spool them to a printer. The javadoc utility is also a language processor; it takes the source code for a Java class and extracts specially formatted JavaDoc comments to produce documentation for the class. 2 Some Lisp systems also allow programs to be compiled into machine code. 3 In some Java systems Java code is compiled to native machine code by ‘just-in-time’ compilers as the program is run. © A Barnes, 2006 6 CS2130/Ilect 1 Programming Language Concepts Every programming language has syntax and semantics: The syntax of a programming language is concerned with the form of expressions, commands and declarations etc. and with how these are put together to form a program. The semantics of a programming language is concerned with the meaning of programs, that is how they behave when executed. A program may be syntactical correct, that is each component command and declaration may be of the correct form and these may be ordered correctly, and yet be invalid semantically. For example a procedure may be called with the wrong number or wrong type of parameters, the operands in an expression may be of different types when they are required to be of the same type or the programmer may have referenced a non-existent field in a record. In this course we will be mainly concerned with semantic issues. A given construct may be provided in several languages with differences in syntax which are merely superficial. For example the following while loops are essentially equivalent: Ada Java, C and C++ + WHILE condition LOOP while (condition) { commands to be repeated commands to be repeated END LOOP; } Semantic features are more important. We need to appreciate subtle differences between apparently similar constructs such as the CASE statement in Ada and the switch statement in C and Java. As an example of semantic differences consider the following: given that the first pair of declarations are essentially equivalent we might be misled into thinking that the second pair are also equivalent. However in the Java/C version only the variable B is initialised: Ada Java, C and C++ A, B : Integer; int A, B; C, D : Integer := 1; int C, D = 1; // initialises D only -- initialises both C & D // should be int C = 1, D = 1; // to initialise both variables As a matter of style to avoid any possible confusion, it would be better to write the initialising declarations on separate lines: C : Integer := 1; int C = 1; D : Integer := 1; int D = 1; We will consider those semantic concepts that are so important they are supported (or ought to be supported) in any good programming language and consider how well a given language supports particular concepts and whether it confuses distinct concepts.