CS 152 Programming Language Paradigms February 5 Class
CS 152: Programming Language Paradigms February 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www. cs. sjsu. edu/~mak
Language Syntax o Example: Syntax of the Java if statement: An if statement consists of the reserved word if followed by a left parenthesis followed by an expression followed by a right parenthesis followed by a statement. Optionally, this is followed by the else part which consists of the reserved word else followed by a statement. n n This syntax rule is written in English. Later, we’ll use a more precise notation called Backus Naur Form (BNF). _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 2
Language Semantics o Semantics: The meaning of a language’s statements. n n o What is the effect of executing the statement at run time? It can be difficult to provide a comprehensive description of meaning in all contexts. Example: Semantics of the Java if statement: First evaluate the expression following the reserved word if. If the expression’s value is true, then execute the statement following the expression, otherwise skip over the statement. If there is an else part, execute the statement following the reserved word else if the expression’s value is false, otherwise skip over that statement. SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 3
Syntax and Semantics o How are a language’s syntax and semantics used? o A well-specified syntax allows programs in the language to be automatically read and translated. o Well-understood semantics allows the program to be translated into a equivalent program. n Equivalent programs have similar runtime behaviors. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 4
Programming Language Translation o There are two major types of language translators: n n o compiler interpreter Source program: The program to be translated, written in a high-level programming language. n Example: A Java program. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 5
Programming Language Translation o Both types of language translators will read a source program and. . . n n Check the program for syntax errors. Convert the program into o Intermediate code, such as tree structures (parse trees) Symbol tables that contain information about the program’s named elements, such as its variables. An interpreter can then use the intermediate code and the symbol tables to execute the program. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 6
Compilers o A compiler takes the intermediate code and the symbol tables and generates a lower-level form of the program. n n o This lower-level form is closer to machine language. Example: An equivalent assembly language program. Target language: The language that a compiler translates a source language into. n n Example: Assembly language Other tools, such as an assembler, translates the compiler’s target language into machine language code. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 7
Compilers o Object code: The machine language code generated by an assembler. o The object code can then linked with other object code and then loaded into memory for execution. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 8
Compilers and Interpreters Compiler and Interpreter Compiler Code generator Assembly code Assembler Execution Program executes on its own. Intermediate code Source program Machine code Front-end translator Symbol tables Executor Execution Program executes under the control of the interpreter. Interpreter SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 9
Compilers and Interpreters o A compiler generates object code, but an interpreter does not. n n o Executing the source program from object code can be several orders of magnitude faster than executing the program by interpreting the intermediate code and the symbol table. But an interpreter requires less effort to get a source program to execute = faster turnaround time. An interpreter maintains control of the source program’s execution. n n Interpreters often come with interactive source-level debuggers that allow you to refer to source program elements, such as variable names. AKA symbolic debugger _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 10
Compilers and Interpreters o Therefore. . . n n Interpreters are useful during program development. Compilers are useful to run released programs in a production environment. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 11
The Future of Programming Languages o In the 1960 s, computer scientists wanted a single universal programming language to meet all needs. o In the late 1970 s and early 1980 s, they wanted specification languages that would allow users to define the needs and then generate the application. n n n o Tell the system what you want it to do. The system figures out how to do it. This is what logic programming languages attempt to do Programming has not become obsolete. n n New languages arise to support new technologies. Example: Web programming. SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 12
Popularity of Programming Languages SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 13
Popularity of Programming Languages, cont’d SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 14
Popularity of Programming Languages, cont’d http: //www. tiobe. com/index. php/content/paperinfo/tpci/index. html SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 15
Good Programming Language Design o What is good programming language design? n n o What criteria should be used to judge a language? How should success or failure of a language be defined? A language design is successful if it satisfies any or all of these criteria: n It achieves the goals of its designers. o n n Therefore, it must have a clear goal. It attains widespread use in an application area. It serves as a model for other languages that are successful. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 16
Good Programming Language Design, cont’d o When designing a new language, choose a clear overall goal. n o Keep the goal in mind throughout the design process. This is especially important for special purpose languages n n Build the abstractions for the target application area into the language design. Example: SQL for relational databases _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 17
Historical Overview o In the early days, machines were extremely slow and memory was scarce. n o Efficiency of execution was a primary design criterion. n o Program speed and memory usage were prime concerns. Early FORTRAN code mapped closely to machine code, minimizing the amount of translation required by the compiler Writability of a language enables a programmer to use it to express computation clearly, correctly, concisely, and quickly. n n Writability of FORTRAN was less important then efficiency. Readability was even less of a concern. SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 18
Historical Overview, cont’d o Algol 60 was designed to express algorithms in a logically clear and concise way. n The language imposed structure on its programs. o o o n o block structured control statements structured array type It also incorporated recursion. COBOL attempted to improve readability of programs by trying to make them look like ordinary English. n However, this made them long and verbose. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 19
Historical Overview, cont’d o In the 1970 s and early 1980 s, the emphasis was on simplicity and abstraction, along with reliability. n n o Mathematical definitions for language constructs. Mechanisms to allow a translator to partially prove the correctness of a program before translation. This led to strong data typing. Examples: Pascal, Modula 2, C, Ada In the 1980 s and 1990 s, the emphasis was on logical or mathematical precision. n n This led to a renewed interest in functional languages. Examples: Lisp, Scheme, ML, Haskell _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 20
Historical Overview, cont’d o The most influential design criteria of the last 25 years. . . n n o The object-oriented approach to abstraction. The use of libraries and other object-oriented techniques increased the reusability of existing code. In addition to the early goals of efficiency, nearly every design decision still considers n n n readability abstraction complexity control _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 21
Target Code Efficiency o Strong data typing n n o Enforced at compile time. No need at run time to check data types before executing operations. Example: FORTRAN IV n n All data declarations and subroutine calls had to be known at compile time. Memory space is allocated once at the beginning of program execution. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 22
Programmer Efficiency o Program reliability is an programmer efficiency issue. n Unreliable programs require programmer time to diagnose and correct. o Programmer efficiency is also impacted by the ease with which errors can be found and corrected. o Since roughly 90% of time is spent on debugging and maintaining programs, maintainability may be the most important index of programming language efficiency. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 23
Regularity o Regularity refers to how well the features of a language are integrated. o Greater regularity implies: n n n o Fewer restrictions on the use of particular constructs. Fewer strange interactions between constructs. Fewer surprises in general in the way the language features behave. Languages that satisfy the criterion of regularity are said to adhere to the Principle of Least Astonishment. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 24
Regularity, cont’d o Regularity involves three concepts: n n n o Generality Orthogonal design Uniformity Otherwise classify a feature or construct as irregular. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 25
Regularity, cont’d o Generality n n o Orthogonal design n n o Avoid special cases in the availability or use of constructs. Combine closely related constructs into a single more general one. Combine constructs in any meaningful way. No unexpected restrictions or behaviors. Uniformity n n Similar things look similar and have similar meanings. Different things look different. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 26
Generality o Avoid special cases wherever possible. o Example: Pascal procedures and functions n n n o Example: Pascal constants n o You can nest procedures and functions. You can pass procedures and functions as parameters to other procedures and functions. However, you cannot assign procedures and functions to variables or store them in data structures. You cannot compute a constant value with an expression. Example: C operators n You cannot use == to compare two structures. SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 27
Orthogonality o Constructs do not behave differently in different contexts. n n o Nonorthogonal: context-dependent restrictions. Lack of generality: restrictions regardless of context. Example: Function return types n n n Pascal: Only scalar and pointer types. C and C++: All data types except array types. Ada and Python: All data types. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 28
Orthogonality, cont’d o Example: Variable declarations n n o C: Declare local variables only at a block beginning. C++ and Java: Declare local variables anywhere in a block prior to their use. Example: Java primitive and reference types n Primitive types use value semantics. o n Reference (object) types use reference semantics. o o Copy values during assignments. An assignment creates two references to the same object. Algol 68: Orthogonality was a major design goal. n Best example of a language where you can combine constructs in all meaningful ways. SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 29
Uniformity o The appearance and behavior of language constructs are consistent. o Example: C++ semicolon n n o Required after a class definition. Forbidden after a function definition. Example: Pascal function return n Return a function value by assigning the value to the function name. Easily confused with a standard assignment statement. Other languages use a return statement. _ SJSU Dept. of Computer Science Spring 2014: February 5 CS 152: Programming Language Paradigms © R. Mak 30
- Slides: 30