Dr.
Lavika Goel
              Assistant Professor
Department of Computer Science and Engineering
 Malaviya National Institute of Technology, Jaipur
                  CS C441 / CS F441 Second Semester 2013-14   BITS Pilani, Hyderabad Campus
Today’s Agenda
• General course information
• Objectives
• Course Logistics
• Course outline
• Introduction to Programming Methodology
                                            BITS Pilani, Hyderabad  Campus
                                                          Pilani Campus
General Course Information
Course Coordinator: Dr. Lavika Goel
Office: First Floor, Dept. of CSE
Email: lavika.cse@mnit.ac.in
                                                    Pilani Campus
                                      BITS Pilani, Hyderabad  Campus
Course Motivation
• Reasons for studying concepts of programming
  languages
   • Increased capacity to express idea.
   • Improved background for choosing appropriate languages.
   • Increased ability to learn new language.
   • Better understanding of the significance of implementation.
   • Overall advancement of computing.
                                                                       Pilani Campus
                                                         BITS Pilani, Hyderabad  Campus
Programming Domains
• Scientific Applications
   •   Floating point arithmetic, Arrays and Matrices, loops and selection
   •   FORTRAN, ALGOL 60
• Business Applications
   •   Reports, Decimal numbers and Character data, Decimal arithmetic
   •   COBOL
• Artificial Intelligence
   •   Symbolic Computation mainly with names
   •   Linked list
   •   LISP, Prolog
                                                                                       Pilani Campus
                                                                         BITS Pilani, Hyderabad  Campus
Programming Domains
• System Programming
   •   Deals with low level features
   •   UNIX
• Scripting Languages
   •   Initially began with collection of commands in a file and then followed by control
       statements , functions etc.
   •   Java Script, PHP
• Special Purpose Languages
   •   Produce business reports, Instruct programmable Machine tools, System
       Simulation.
                                                                                        Pilani Campus
                                                                          BITS Pilani, Hyderabad  Campus
PM as a Course
• What is not
   – Do not teach you a programming language
   – Do not teach you how to program
• What is
   –   Introduce fundamental concepts of programming languages
   –   Discuss design issues of various language constructs
   –   Examine design/implementation choices for these constructs
   –   Compare design alternatives
• Need to be familiar in at least one PL
                                                                                  Pilani Campus
                                                                    BITS Pilani, Hyderabad  Campus
Course Logistics
                 Grading Policy
                    20%
                                  Quiz
           50%                    Midsem Examination
                                  EndSem Examination
                     30%
                                                           Pilani Campus
                                             BITS Pilani, Hyderabad  Campus
Reference Books
Ravi Sethi, "Programming Languages: Concepts and Constructs" 2nd Edition by
Addison Wesley.
Robert W. Sebesta, "Concepts of Programming Languages", 10th Edition by
Pearson Publishers.
Aho, Lam, Sethi and Ullman, "Compilers Principles, Techniques, and Tools".
Pearson Education. Low Price Edition. 2004
                                                                                 Pilani Campus
                                                                   BITS Pilani, Hyderabad  Campus
Course Outline
• Overview and motivation
• Imperative Programming:
   Describing Syntax and Semantics
   Names, Bindings, and Scopes
   Data Types
   Expressions and Assignment
   Control Structures
   Subprograms
• Object Oriented Programming: Abstract Data Types, Encapsulation,
  Information Hiding.
• Functional and Logic Programming Languages
                                                                    Pilani Campus
                                                      BITS Pilani, Hyderabad  Campus
Course summary
 • Principles are emphasized more than details.
 • Methods are emphasized more than results.
 • Semantics is emphasized more than syntax.
                                                                Pilani Campus
                                                  BITS Pilani, Hyderabad  Campus
What is a programming language?
 A language that is intended for the expression of computer
 programs and that is capable of expressing any computer
 program.
                                                               Pilani Campus
                                                 BITS Pilani, Hyderabad  Campus
Programming Language
 A programming language is an artificial language designed
   to express computations or algorithms that can be
   performed by a computer
 A program is computer coding of
   an algorithm that
    – Takes input                                Input
    – Performs some calculations on
      the input
                                               Program
    – Generates output
                                                Output
                                                              Pilani Campus
                                                BITS Pilani, Hyderabad  Campus
Who Needs Programming
Languages?
• Computers' native tongue is machine language
•   Programmers need higher level languages, because:
    – They can't write machine language correctly
    – They can't read machine language fluently
    – They can't express their ideas in machine language
      efficiently
    – Life is too short to program in machine language.
•   A formal language is not only a man-machine interface
    but also a person-to-person language!
                                                             Pilani Campus
                                               BITS Pilani, Hyderabad  Campus
Von Neumann Architecture
• Stored program concept
• Data and instruction have same format
• Interpret sequences as data and instructions
                                                               Pilani Campus
                                                 BITS Pilani, Hyderabad  Campus
The von Neumann
Architecture
                                Pilani Campus
                  BITS Pilani, Hyderabad  Campus
Imperative Lang. & von
Neumann Architecture
• 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
                                                            Pilani Campus
                                              BITS Pilani, Hyderabad  Campus
The von Neumann Architecture
• Fetch-execute-cycle (on a von Neumann architecture
  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
                                                                      Pilani Campus
                                                        BITS Pilani, Hyderabad  Campus
Language Categories
• Imperative
  • Central features are variables, assignment statements, and
    iteration
  • Include languages that support object-oriented programming
  • Include scripting languages
  • Include the visual languages
  • Examples: C, Java, Perl, JavaScript, Visual BASIC, .NET, C++
• Functional
  • Main means of making computations is by applying functions to
    given parameters
  • Examples: LISP, Scheme
                                                                   Pilani Campus
                                                     BITS Pilani, Hyderabad  Campus
Language Categories
• Logic
   • Rule-based (rules are specified in no particular order)
   • Example: Prolog
                                                                Pilani Campus
                                                  BITS Pilani, Hyderabad  Campus
Programming Paradigms
 Imperative – action oriented, sequence of actions.
 Functional - symbolic data processing.
 Object-Oriented - classes of objects.
 Logic - logic reasoning.
                                                                Pilani Campus
                                                  BITS Pilani, Hyderabad  Campus
Imperative Programming
• General purpose imperative languages: Fortran, Pascal and C.
• Fortran: Familiar notations and efficiency, machine language programs
  are more efficient as they do not require translation.
• Language of choice for scientific programming: used mathematical
  notations, machine independent.
• Algol 60 was developed as a common language to share programs and
  describe numerical processes- widely admired language.
• Pascal was designed as a teaching language, similar to Algol 60 in
  syntactic constructs with minor differences.
• C was developed as an implementation language for software for UNIX
  OS. C provides a rich set of operators, terse syntax and efficient access
  to the machine.
                                                                         Pilani Campus
                                                           BITS Pilani, Hyderabad  Campus
Functional Programming
•   LISP (list processor): language designed for applications in artificial intelligence.
•   Example of a List with 3 elements of which the third is a sublist-
     (Shakespeare wrote (the tempest))
•   Designed primarily for symbolic data processing.
•   It has been used for symbolic calculations in differential and integral calculus,
    mathematical logic, game playing, etc.
•   Scheme: version of LISP popular for teaching and research due to its clean
    design.
•   Common LISP: is an advancement over proliferations of LISP i.e. MacLISP/
    InterLISP. MacLISP emphasized performance and production quality while
    InterLISP introduced the notion of a programming environment with a structured
    editor tied to the syntax.
•   CLOS is an object oriented extension, Common Lisp Object System.
•   Other functional languages: ISWIM (not practically implemented), ML, Miranda,
    Haskell
                                                                                    Pilani Campus
                                                                      BITS Pilani, Hyderabad  Campus
Object oriented Programming
• Key concept of OO Programming: class of objects, classification of
  objects into classes and subclasses (generalization and
  specialization).
• Simula: Designed as both a programming language and a
  description language.
• C++ and Smalltalk: popular OO languages, descendant of Simula by
  taking the notion of objects and classes.
• C++ is an advancement over C which adds object oriented features
  to imperative programming in C.
• Smalltalk was designed as part of personal computing environment,
  it is an interactive system with a graphical user interface.
                                                                      Pilani Campus
                                                        BITS Pilani, Hyderabad  Campus
Logic Programming
• Prolog: was developed for natural language processing.
• It uses a specialized form of logical reasoning to answer
  queries.
• Used for a variety of applications from databases to
  expert systems.
• Prolog programs have the expressiveness of logic.
                                                               Pilani Campus
                                                 BITS Pilani, Hyderabad  Campus
A short history of programming
Languages
 1950s : LISP, FORTRAN
 1960s : AlGOL 60, ISWIM, Simula
 1970s : C, Pascal, Prolog, Smalltalk
 During 1970 : a lot of PLs were designed.
 1980s : C++, ML, Miranda, Haskell, CommonLISP
   –   Numerically based languages. (FORTRAN,ALGOL)
   –   Business languages. (COBOL)
   –   Artificial intelligence languages. (LISP, Prolog)
   –   Systems languages. ( C)
                                                              Pilani Campus
                                                BITS Pilani, Hyderabad  Campus
A short history of programming languages
(cont.)
  50s and 60s :
          – Early high level languages : FORTRAN, COBOL,
            ALGOL60
          – Early functional languages : LISP
          – Next leap forward: Algol68, SIMULA67, BASIC
  70s:
          –   High level and structured programming: Pascal
          –   Systems programming: C, modula-2
          –   Logical programming: Prolog
          –   Improvement of functional programming: Scheme
                                                                   Pilani Campus
                                                     BITS Pilani, Hyderabad  Campus
A short history of programming languages
(cont.)
  80s:
          – Development of functional programming:
            CommonLisp, ML, Haskell, Miranda
          – Need for reliability and maintainability: Ada
          – Object-oriented programming: Smalltalk, C++
  90s:
          – Fourth-generation languages
          – Visual languages : Delphi
          – Scripting languages : Perl
                                                                     Pilani Campus
                                                       BITS Pilani, Hyderabad  Campus
What Makes a Good PL?
 Language evaluation criteria:
 Readability: the ease with which programs can be read
   and understood
 Writability: the ease with which a language can be used to
   create programs
 Reliability: a program performs to its specifications under
   all conditions
 Cost
                                                                Pilani Campus
                                                  BITS Pilani, Hyderabad  Campus
Language Evaluation Criteria
                                             CRITERIA
       Characteristic      READABILITY   WRITABILITY    RELIABILITY
 Simplicity                    X              X              X
 Orthogonality                 X              X              X
 Data types                    X              X              X
 Syntax design                 X              X              X
 Support for abstraction                      X              X
 Expressivity                                 X              X
 Type checking                                               X
 Exception handling                                          X
 Restricted aliasing                                         X
                                                                        Pilani Campus
                                                          BITS Pilani, Hyderabad  Campus
Features Related to Readability
 Overall simplicity: language is more readable if
    – Fewer features and basic constructs
      Readability problems occur whenever program’s author uses a subset different
      from that familiar to reader
    – Fewer feature multiplicity (i.e., doing the same operation with different ways)
    – Minimal operator overloading
 Orthogonality
    – A relatively small set of primitive constructs can be combined in a relatively small
      number of ways
    – The combination is legal.
    – Too much orthogonality can also cause problems
                                                                                        Pilani Campus
                                                                          BITS Pilani, Hyderabad  Campus
Features Related to Readability
 Control statements
    – Sufficient control statements for structured prog.
       can read program from top to bottom w/o jump
 Data types and structures
    – Adequate facilities for defining data type & structure
 Syntax considerations
    – Identifier or keywords
    – Special words and methods of forming compound statements
    – Form and meaning: self-descriptive constructs, meaningful keywords
                                                                                   Pilani Campus
                                                                     BITS Pilani, Hyderabad  Campus
Writability
 Simplicity and orthogonality
    – But, too orthogonal may cause errors undetected
 Support for abstraction
    – Ability to define and use complex structures or operations in ways that allow
      details to be ignored
    – Abstraction in process (e.g. subprogram), data
 Expressivity
    – A set of relatively convenient ways of specifying operations
    – Example: the inclusion of for statement in many modern languages
                                                                                      Pilani Campus
                                                                        BITS Pilani, Hyderabad  Campus
Reliability
 Type checking
    – Testing for type errors, e.g. subprogram parameters
 Exception handling
    – Intercept run-time errors & take corrective measures
 Aliasing
    – Presence of two or more distinct references for the same memory location
 Readability and writability
    – A language that does not support “natural” ways of expressing an algorithm will
      necessarily use “unnatural” approaches, and hence reduced reliability
                                                                                     Pilani Campus
                                                                       BITS Pilani, Hyderabad  Campus
Cost
 Training programmers to use language
 Writing programs (closeness to particular applications)
 Compiling programs
 Executing programs: run-time type checking
 Language implementation system: availability of free
   compilers
 Reliability: poor reliability leads to high costs
 Maintaining programs
                                                                Pilani Campus
                                                  BITS Pilani, Hyderabad  Campus
Language Design Trade-Offs
 Reliability vs. cost of execution
     – e.g., Java demands all references to array elements be checked for proper
       indexing but that leads to increased execution costs
 Readability vs. writability
     – e.g., use of many powerful operators (and a large number of new symbols),
       allowing complex computations to be written in a compact program but at the
       cost of poor readability
 Writability (flexibility) vs. reliability
     – e.g., C++ pointers are powerful and very flexible but not reliably used
                                                                                        Pilani Campus
                                                                          BITS Pilani, Hyderabad  Campus
Language Implementation
 Compiler - source code translation into machine code (all at
    once)
 Interpreter - machine is brought up to the language (one
    statement at a time)
                                                                Pilani Campus
                                                  BITS Pilani, Hyderabad  Campus
Implementation Methods
• Compilation
   • Programs are translated into machine language.
• Pure Interpretation
   • Programs are interpreted by another program known
     as an interpreter.
• Hybrid Implementation Systems
   • A compromise between compilers           and        pure
     interpreters.
                                                            Pilani Campus
                                              BITS Pilani, Hyderabad  Campus
Layered View of Computer
                           The operating system and
                           Language implementation are
                           layered over machine interface
                           of a computer
                                                    Pilani Campus
                                      BITS Pilani, Hyderabad  Campus
The Compilation
Process
                                Pilani Campus
                  BITS Pilani, Hyderabad  Campus
Compilation
• Translate high-level program (source language) into
  machine code (machine language).
• Compilation process has several phases:
   •   lexical analysis: converts characters in the source program into lexical units e.g.:
       identifiers / keywords, operators, punctuation…
   •   syntax analysis: transforms lexical units into parse trees which represent the
       syntactic structure of program.
   •   Intermediate code generation & Semantics analysis: generate intermediate code
       and does type checking.
   •   Code optimization: (optional)
   •   code generation: machine code is generated.
                                                                                        Pilani Campus
                                                                          BITS Pilani, Hyderabad  Campus
Additional Compilation
Terminologies
• Load module (executable image):
   • the user and system code together.
• Linking and loading:
   • the process of collecting system program units and
     linking them to a user program.
   • As well as linking other user programs to it.
                                                           Pilani Campus
                                             BITS Pilani, Hyderabad  Campus
Compiled C
   Source                        Linker
            compiler     .o
    code                           and
                       files
    in C                         loader
                                Machine
                               code (exe)
                                                          Pilani Campus
                                            BITS Pilani, Hyderabad  Campus
Pure Interpretation
• No translation
 Program interpreted by another program (interpreter) without translation
   – Interpreter acts a simulator or virtual machine
   – Machine is brought to the level of language by building higher level
      machine called an interpreter that can run the language directly.
• Easier implementation of programs (run-time errors can easily and
   immediately be displayed)
• Slower execution (10 to 100 times slower than compiled programs)
   Decoding of higher level language programs is more complex, decoding has
   to be done every time a statement is executed.
• Often requires more space
• Used in APL, SNOBOL, LISP.
• Now rare for traditional high-level languages
• Significant comeback with some Web scripting languages (e.g., JavaScript,
  PHP)
                                                                              Pilani Campus
                                                                BITS Pilani, Hyderabad  Campus
Pure Interpretation Process
                                            Pilani Campus
                              BITS Pilani, Hyderabad  Campus
Comparisons
 • Interpretation is slower as decoding of higher level language programs is
   more complex, decoding has to be done every time a statement is executed.
 • Compiled languages have bias towards static properties since all compiling
   decisions are made at translation time. Interpreted languages can deal with
   dynamic properties.
 • Interpretation is more flexible: due to direct running on the source code,
   interpreter can allow to add features or correct errors in the source code.
                                                                              Pilani Campus
                                                                BITS Pilani, Hyderabad  Campus
Hybrid Implementation Systems
• 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 since the source
  language statements are decoded only once.
• 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).
                                                                                     Pilani Campus
                                                                       BITS Pilani, Hyderabad  Campus
Hybrid
Implementation
Process
                        Pilani Campus
          BITS Pilani, Hyderabad  Campus
Just-in-Time Implementation
Systems
• 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.
                                                               Pilani Campus
                                                 BITS Pilani, Hyderabad  Campus
Summary
•   The study of programming languages is valuable for a number of reasons:
     • Increase our capacity to use different constructs
     • Enable us to choose languages more intelligently
     • Makes learning new languages easier
•   Most important criteria for evaluating programming languages include:
     • Readability, writability, reliability, cost
•   Major influences on language design have been
     • machine architecture and software development methodologies
•   The major methods of implementing programming languages are:
     • compilation, pure interpretation, and hybrid implementation
                                                                              Pilani Campus
                                                                BITS Pilani, Hyderabad  Campus