CCCCCCCCCCCCCCCCC C C CC C
CCCCCCCCCCCCCCCCC C C CC C
An algorithmis a computational procedure that takes values as inputand produces values as output, in order to solve a well defined computational problem: The statement of the problem specifies a desired relationship between the inputand the output. The algorithm specifies how to achieve that input/outputrelationship. A particular value of the inputcorresponds to an instance of the problem.
A programming language is an artificial language designed for expressing algorithms on a computer: Need to expressan infinite number of algorithms (Turing complete). Requires an unambiguous syntax, specified by a finitecontext free grammar Should have a well defined compositional semanticsfor each syntactic construct: operational vs. axiomatic vs. denotation Often requires a practical implementation i.e. pragmatics: Implementation on a real machine vs. virtual machine translation vs. compilation vs. interpretation.
Reasons for studying concepts of PLs: Increased ability to express ideas/algorithmsNatural language: y The depth at which people can think is influenced by the expressive power of the language they use (also Sapir-Worf hypothesis).
Programming languages: y The complexity of the algorithms that people implement is influenced by the set of constructs available in the programming language. y Kenneth E. Iversion (inventor of APL programming language):Notation as a tool of thought (Turing award lecture).
y Many programmers use the language with which they are most familiar, even though poorly suited for the new project y Ideal: use the most appropriate language. y Features important for a given project are included already in the language design n, as opposed to being simulated => elegance & safety.
Example: a small subprogram that is called very frequently can be a highly inefficient design choice. More details can be learned by studying compiler design. Better use of languages that are already known.
Application Domains
Scientific applications: y Large numbers of floating point computations; extensive use of arrays y Fortran, Matlab Business applications: y Produce reports, use decimal numbers and characters
y y
y COBOL Artificial intelligence: Symbols rather than numbers manipulated; use of linked listsLISP Lately, many AI applications (e.g. statistical or connectionist approaches) are written in Java, C++, Python.
y y y y y
Systems programming: Need efficiency because of continuous use. C (UNIX almost entirely written in C). Web Software:
Eclectic collection of languages: markup (e.g., XHTML). scripting for dynamic content: ------client side, using scripts embedded in the XHTML document. Examples: Javascript, PHP. ------ server side, using the Common Gateway Interface. Examples: JSP, ASP, PHP. y general-purpose, executed on Example: Java, C++,
programs.
Readibility:
Overall simplicityA manageable set of basic features and constructs. Minimal feature multiplicity.Example: increment operators in C
combined in a relatively small number of ways.Example: addition in assembly on IBM mainframe vs. VAX minicomputers. C. Every possible combination is legalExample: returning arrays & records in
Data types and structure: Adequate predefined data types and structuresExample: Booleanvs. Integer The presence of adequate facilities for defining data structuresExample:
Syntactic design: Identifier forms (allow long names). Special words and methods of forming compound statements. Form and meaning: self-descriptive constructs, meaningful keywords.statickeyword in C has
Writability:
Expressivity
A set of relatively convenient ways of specifying operations. Strength and number of operators and predefined functions. Examples: Increment operators in C. Short circuit operators in Ada. Counting loops with forvs. whilein Java.
Reliability:
Type checking:
Testing for type errors at compile-time vs. run-time. Examples:(+) Ada, Java, C#, C++. () C.
Exception handling
Intercept run-time errors, take corrective measures and continue. Examples:(+) Ada, C++, Java.
Aliasing
Presence of two or more distinct referencing methods for the same memory location:Pointers in C, references in C++.
Cost:
Training programmers to use the language. Writing programs (closeness to particular applications). Compiling programs: --Tradeoff between compile vs. execution time through optimization. Executing programs: Example: many run-time type checks slow the execution (Java). Language implementation system: Availability of free compilers. Reliability: poor reliability leads to high costs. Maintaining programs: Corrections & adding new functionality.
Other Criteria:
Portability
The ease with which programs can be moved from one implementation to another (helped by standardization). Generality The applicability to a wide range of applications. Well-definedness The completeness and precision of the languages official definition.
Imperative languages, most dominant, because of von Neumann computers: ~Data and programs stored in memory Memory is separate from CPU Instructions and data are piped from memory to CPU Basis for imperative languages: Variables model memory cells Assignment statements model piping Iteration is efficient
computer) initialize the program counter: repeat forever fetch the instruction pointed by the counter increment the counter decode the instruction execute the instruction end repeat Von Neumann Bottleneck:~ Connection speed between a computers memory and its processor determines the speed of a computer. Program instructions often can be executed much faster than the speed of the connection; the connection speed thus results in a bottleneck. Known as the von Neumann bottleneck; it is the primary limiting factor in the speed of computers
data abstraction + inheritance + polymorphism Smaltalk, Ada 95, C++, Java, CLOS, Prolog++
Language Paradigms:
Imperative:Designed around the von Neumann architecture. ~Computation is performed through statements that change a programs state. Central features are variables, assignment statements, and iteration; sequencing of commands, explicit state update via assignment. May include : OO programming languages, scripting languages, visual languages. Examples: Fortran, Algol ,Pascal, C/C++, Java, Perl, JavaScript, Visual BASIC .NET Functional :Main means of making computations is by applying functions to given parameters. Examples: LISP, Scheme, ML, Haskell May include OO concepts. Logic :Rule-based (rules are specified in no particular order). Computations are made through a logical inference process. Example: Prolog, CLIPS. May include OO concepts.
Implementation Methods:
Compilation: Programs are translated into machine language & system calls. Interpretation: Programs are interpreted by another program an interpreter. Hybrid: Programs are translated into an intermediate language that allows easy interpretation. Just-in-Time: Hybird + compile subprograms code the first time they are called.
Compilation:
Translate high-level program (source language) into machine code (machine language). Slow translation, fast execution. Compilation process has several phases: lexical analysis: converts characters in the source program into lexical units (e.g. identifiers, operators, keywords). syntactic analysis: transforms lexical units into parse trees which represent the syntactic structure of program. semantics analysis: check for errors hard to detect during syntactic analysis; generate intermediate code. code generation: machine code is generated.
Linking: the process of collecting system program units and other user libraries and linking them to a user program. Load module(executable image): the user and system code together.
Interpretation
Interpreter usually implemented as a read-eval-printloop: -read expression in the input language (usually translating it in some internal form) evaluates the internal form of the expression print the result of the evaluation loops and reads the next input expression until exit. Interpreter acts as a virtual machine for the source language: fetch-executecycle replaced by the read-eval-printloop. usually has a core component, called the interpretter run-time , that is a compiled program running on the native machine.
Interpretation:
Easier implementation of programs (run-time errors can easily and immediately be displayed). Slower execution (10 to 100 times slower than compiled programs). Often requires more memory space. Now rare for traditional high-level languages. Significant comeback with some Web scripting languages (e.g., JavaScript, PHP)
Hybrid Implementation:
A compromise between compilers and pure interpreters. A high-level language program is translated to an intermediate language that allows easy interpretation. Faster than pure interpretation. Examples: Perl programs are partially compiled to detect errors before interpretation. Initial implementations of Java were hybrid: the intermediate form, byte code, provides portability to any machine that has a byte code interpreter and a run-time system (together, these are called Java Virtual Machine)
Just-in-Time Implementation
Initially translate programs to an intermediate language. Then compile the intermediate language of the subprograms into machine code when they are called. Machine code version is kept for subsequent calls. JIT systems are widely used for Java programs. .NET languages are implemented with a JIT system.
Preprocessors:
Preprocessor is run before compiler, as a macro expander. Macros are commonly used to: specify that code from another file is to be included; define simple expressions/functions. A well-known example: C preprocessor expands #include, #define, and similar macros.