Programming Languages - PowerPoint PPT Presentation

1 / 68
About This Presentation
Title:

Programming Languages

Description:

'A computer program does what you tell it to do, not what you want it to do. ... LISP, ML (MetaLanguage), Hope and Scheme are examples of Functional paradigm ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 69
Provided by: Preu
Category:

less

Transcript and Presenter's Notes

Title: Programming Languages


1
Programming Languages
  • "A computer program does what you tell it to do,
    not what you want it to do."    
  •  Greer's Third Law.

2
Programming Language Toolset
C
If all you have is a hammer, then everything
looks like a nail.
3
More than just a tool
  • Current view from carpentry
  • A hammer is more than just a hammer. It's a
    personal tool that you get used to and you form a
    loyalty with. It becomes an extension of
    yourself."
  • http//www.hammernet.com/romance.htm

4
Objectives
  • Explore Programming Language history and design
  • Review Programming Paradigms
  • Learn about the Traditional Programming concepts
  • Review Procedural Units
  • Understand the basics of Program Translation
  • Reading Brookshear Ch. 5

5
Language Generations
Five Generations Procedural Languages 1GL
Machine language 2GL Assembly language 3GL
High-level language Nonprocedural Languages 4GL
Very high-level language 5GL Natural language
  • Low levels closer to binary format
  • High levels - closer to human language

6
Machine Language
  • Written in strings of 0 and 1
  • Only language the computer understands
  • All other programming languages are translated to
    machine language
  • Computer dependent
  • Recall that CISC instructions wont work for a
    RISC architecture

7
Machine Language
  • Why is programming in machine language difficult?
  • Must remember the binary encoding for
    instructions or constantly look them up from some
    reference sheet
  • Difficult to read code.
  • Error prone, difficult to locate errors.
  • Recall Brookshear Machine Instructions
  • 156C
  • 166D
  • 5056
  • 306E
  • C000

8
Machine Language
  • Early programmers used machine language to
    program
  • plan and lay out data and code in memory
  • In BSML, recall starting program at 00 and
    storing data at say 20
  • use explicit addresses for jump instructions
  • In BSML, recall we had to specify the address to
    jump to
  • use explicit addresses for data storage
  • What happens if you add another instruction in
    the middle of your code?
  • It might move the rest of the instructions in
    memory which
  • changes the address of other instructions which
  • might require changes to the addresses in jump
    instructions.
  • Similar problems occur if you change the
    arrangement of data.

9
Assembly Language
  • Uses Mnemonic codes
  • Assigns Names for memory locations
  • Depends on the Computer
  • Needs an Assembler to translates from Assembly to
    machine language so the machine can execute the
    program

10
Assembly Language
  • Humans work better with mnemonics, which are
    words that are designed to aid in remembering
    information.
  • Programmers invented assembly language which uses
    mnemonic symbols rather than binary to represent
    machine instructions and registers

11
Assembly Language
  • Mnemonic names
  • Example,
  • assign the name Price to location 6C,
  • assign the name ShippingCharge to 6D
  • Assign the name TotalCost to 6E
  • Then, the following two columns are equivalent
  • 156C Load R5, Price
  • 166D Load R6, ShippingCharge
  • 5056 Add1 R0, R5, R6
  • 306E Store R0, TotalCost
  • C000 Halt

12
Assembly Language
  • Mnemonics still dont solve the problem of
    relocating addresses when code or data is shifted
  • Also, data is still referred to by its address in
    memory, we just use a name for the memory cell
    instead of a number
  • Symbolic addresses were developed to refer to
    memory locations
  • Example, Use of Labels to mark JUMP statements

13
The Assembler
  • Programmers used text files containing assembly
    language to describe programs (known as Source
    code).
  • Assembly language used mnemonics and symbolic
    addresses
  • Assemblers are software tools that translate
    source code (text) into machine language (binary)

14
Assemblers
  • Assemblers translate the mnemonic codes to
    binary codes
  • Machine instruction is different for different
    machine architecture
  • Recall RISC and CISC Instruction sets?
  • So, Assemblers must be written for every
    different type of machine
  • Programmers need to think in terms of machine
    instructions
  • Programs are not portable

15
High Level Languages 3GL
  • Identify a collection of high-level primitives
  • e.g. loops and selection constructs
  • Think and express concepts at a higher level of
    abstraction than machine language
  • Implement translation of such high-level
    primitives into machine language using an
    intermediary (Compilers)

16
3GL Third Generation Languages
  • 1960s
  • Languages designed for specific types of problems
    and used syntax familiar to the people in that
    field
  • FORTRAN math
  • COBOL business
  • Compiler translates from high-level language to
    machine language

17
3GL Third Generation Languages
  • FORTRAN
  • 1954
  • Represent complex mathematical formulas
  • C/C has replaced FORTRAN
  • COBOL
  • 1959
  • Business
  • Large complex data files
  • Formatted business reports

18
Third Generation Languages FORTRAN
FORTRAN
19
Language Translation Interpreters Compilers
  • High level languages were invented to
  • break away from machine-dependence
  • allow the expression of abstract concepts
  • Compilers and interpreters are software tools
    that translate high-level source code (text) into
    machine language (binary)

20
Language Translation
  • Interpreters
  • read each line of text
  • translates the text into machine language
  • executes the line before reading the next line
  • Compilers
  • read all text
  • translate everything
  • create executable files that represent the source
    code

21
Programming Paradigms
  • Procedural (Imperative)
  • Cobol, Fortran, C, Pascal
  • Focused at expressing an algorithm
  • Declarative
  • Prolog, SQL
  • Focused at expressing the solution to a problem
  • Functional
  • Scheme, Lisp, ML
  • Focused at identifying functional components
  • Object-Oriented
  • Smalltalk, C, Java
  • Programs are sets of cooperating components that
    contain both data and operations

22
Programming Paradigms Procedural
  • Procedural the development of a sequence of
    commands that, when followed, manipulate the data
    to produce the desired result
  • Our Algorithms discussion is based on this
    paradigm
  • Find an algorithm to solve the problem and
    express that algorithm as a sequence of simple
    and unambiguous commands
  • C, FORTRAN are examples of procedural paradigm

23
Programming Paradigms Declarative (Logic)
  • Declarative design and state the problem
    precisely, rather than design a solution
  • The trick is to discover and implement a general
    problem-solving algorithm so that
  • all you have to do is state the problem in the
    correct format and it already has a solution
  • Useful in simulating systems (economic,
    geographical, political) in order to test
    hypothesis
  • The general solution is to recompute the value of
    certain key parameters (like GDP, temperature
    etc) over passage of time
  • Then design the problem at hand precisely by
    defining the correct parameters
  • Prolog is an example of declarative paradigm

24
Programming Paradigms Functional
  • Functional View the process of program
    development as connecting predefined black
    boxes
  • each of which accepts input and produces output
  • in such a way as to produce the required
    input-to-output relationship.
  • Mathematicians refer to such boxes as
    functions and hence the name.
  • LISP, ML (MetaLanguage), Hope and Scheme are
    examples of Functional paradigm
  • Primitives in this paradigm are elementary
    functions which can be used to create more
    complex functions as needed to solve the problem
  • Example Compute the average of a given set of
    numbers

25
Programming Paradigms Functional
  • Example Compute the average of a given set of
    numbers
  • The three boxes or functions are
  • Sum, Count, Divide
  • Both Sum and Count functions accept the same
    input whereas Sum box computes the sum, the
    Count box keeps track of the count
  • The output from these two boxes are the input
    for the third box Divide, which produces the
    desired output.
  • In LISP, the above program is written as
  • (Divide (Sum Numbers) (Count Numbers))

26
Evolution of programming paradigms
  • AI The functional and logical paradigms were
    very popular a decade or so ago as a result of
    the Japanese 5th Generation initiative,
  • which adopted a logic programming language as its
    focus in an attempt to use parallel architectures
    to solve AI problems.
  • At that time, groups in the UK and the USA
    emphasized the functional paradigm as an easier
    way to program parallel architectures

27
Evolution of programming paradigms
  • OOP The object oriented paradigm is now much
    more fashionable - and is more widely used in
    industry today
  • One reason for this is because nearly all object
    oriented languages are quite close to the
    imperative paradigm - though this need not be the
    case - and the change from imperative programming
    to object oriented (hereafter OOP) programming is
    less of a shock for programmers brought up on
    imperative programming languages.

28
OOP Languages
  • It may appear that, having learnt C, C is the
    obvious choice of the OO languages.
  • The problem with C is that it incorporates some
    OO ideas, but not necessarily in a particularly
    clean way
  • As a result it is not an elegant language, and
    nor is it a good example of the different
    paradigm. C also derives a lot from C, but is
    just too new.
  • Fortunately Java borrows quite a lot from C as
    well and seems to be easier to learn for
    beginners in OOP.

29
Programming paradigms at a glance
30
Traditional Programming Concepts (TPC)
  • Variables and Data Types
  • Describe data and data format
  • Data Structures
  • Complex data types
  • Constants and Literals
  • Using a value directly
  • Assignment Statements
  • Assign values to variables
  • Assign result of an evaluation to a variable
  • Control Statements
  • Selection
  • Loop

31
TPC Variables and Data Types
  • Variable is a location in main memory referenced
    by a name rather than a numeric address
  • Variable holds a value value associated with
    that name can change during the program execution
  • Variables need to be declared before they can
    be used in the program
  • Variable declaration includes specifying what
    type of quantity it can hold numeric,
    character, Boolean and so on known as the data
    type
  • Variable declaration in C
  • float price
  • int age
  • char x

32
TPC Data Structures
  • Data Structure refers to the conceptual
    arrangement of data for easier manipulation,
    depending on the computation
  • Arrays homogenous block structure of cells, with
    each cell in the array holding a value of the
    same data type can be 1D or 2D
  • Linked Lists a list of boxes linked by pointers
    to the next box can be doubly linked, circular
    linked
  • Trees conceptual arrangement with a single root,
    with parent-child relationship between nodes
  • In JS, a one-dimensional array (1D array) is
    written as arrayNameindex. Remember?
  • In C, a 2D array holding integers can be declared
    as
  • int Scores 2 9
  • To be interpreted as a 2 rows and 9 column table
  • More on these special data structures when we do
    chapter 7

33
TPC Constants and Literals
  • Literal is a fixed quantity that is used over and
    over literally in a computation.
  • In calculations of air traffic in the vicinity of
    an airport, if the altitude above sea level is
    645 feet and is a quantity that is used over and
    over in calculations, it is used explicitly as
    is.
  • For example,
  • EffectiveAltitude AltimeterReading 645
  • Where EffectiveAltitude and AltimeterReading are
    variables

34
TPC Constants and Literals
  • Constant is a fixed quantity that is referenced
    by a descriptive name rather than the literal
    value
  • In calculations of air traffic in the vicinity of
    an airport, if the altitude above sea level is
    645 feet and is a quantity that is used over and
    over in calculations, we can declare it as a
    constant
  • Constant AirportAltitude 645
  • If EffectiveAltitude and AltimeterReading are the
    same variables as before, then, the same formula
    now can be written as
  • Effective Altitude Altimeter Reading
    AirportAltitude

35
TPC Constants and Literals
  • Why is constant a better way to program than
    literal?
  • Consider the case where a program uses the
    airport altitude 25 times in its calculations.
  • Literal method would have the literal value 645
    appearing 25 times in that program
  • Constant method would have the value 645
    appearing only once at the initial declaration
  • Constant AirportAltitude 645
  • Any subsequent use of this value would be done
    using the variable AirportAltitude, not the
    literal value 645.
  • So far so good.
  • What if we want to use this program with another
    airport with airport altitude 1000 feet?
  • Literal method would involve making 25 changes,
    whereas Constant method would involve making only
    one change.

36
TPC Assignment statements
  • Assign a right-hand-side (rhs) quantity to a
    left-hand-side (lhs) quantity.
  • Z X Y
  • rhs quantity X Y is evaluated first and the
    result is assigned to the lhs variable Z
  • Name Ann
  • The value Ann is assigned to the variable Name
  • Many High level languages allow arithmetic
    assignment of the form
  • variable operator value

37
TPC Control statements
  • Imperative statement that alters the execution
    sequence of a program
  • Selection
  • If Else, or multi-way switch
  • True/false condition to select the set of
    instructions that need to be executed
  • Repetition
  • While () and for( ) loop structures
  • Initialize, modify, terminate the condition to
    control the repetition of a set of instructions
    as needed

38
Procedural Units
  • Procedure is an abstract modular tool that can be
    used by several different program units.
  • A Procedure needs to be defined first, and then
    invoked via procedure call wherever needed
  • Control transfers from current statement in the
    program unit to the procedure unit by means of a
    JUMP control at the time of procedure call
  • Control is returned to the original program unit
    once the procedure unit has finished executing
  • Procedures have their own set of local variables
  • Values from one program unit to another procedure
    unit can be passed via parameters declared for
    that procedure unit
  • Reading Chapter 5 Section 5.3

39
Procedural Units Control transfer
40
Procedural Units Procedure Call
41
Procedural Units Separate copies maintained
42
Procedural Units Preserve calling environment
43
Procedures vs. Functions
  • Function is similar to Procedure except that a
    value is transferred back to the calling program
    as the value of the function
  • Procedures do not pass back a return value and
    preserve the calling environment
  • Functions pass back a return value that can be
    used by the calling program whenever needed

44
Grammar
  • Every language has a distinct grammar, which is a
    set of rules for creating expressions in the
    language
  • (define (sum n m) ( n m)) in Scheme
  • x n m in C
  • The syntax of a language consists of the grammar
    combined with an alphabet
  • Most languages use the normal alphabet, e.g.
    Scheme
  • Some languages use special symbols, e.g. APL

45
Syntax Diagram
  • Pictorial representation of programs grammatical
    structure
  • Example ifthenelse structure
  • Terms in rectangles require further description
    and are called non-terminals
  • Terms in ovals are called terminals (a.k.a Key
    words or Reserved words)

46
Language Syntax
  • Syntax is usually represented in a formal way
    using what is known as Context Free Grammar (CFG)
  • One of the widely used formalism is called BNF
    Backus-Naur form named after the originators of
    this formalism

47
Syntax BNF
  • The valid and legal syntax of a language can be
    described using a special notation known as
    Backus-Naur form (BNF)
  • John Backus and Peter Naur
  • Originally developed to describe ALGOL language
    (1950s and 1960s)

48
BNF
  • The meta-symbols of BNF are
  • meaning "is defined as"
  • meaning "or"
  • lt gt angle brackets used to surround category
    names.
  • The angle brackets distinguish syntax rule names
    (also called non-terminal symbols) from terminal
    symbols which are written exactly as they are to
    be represented.

49
BNF
  • Consider a small child that can only form the
    sounds from its limited vocabulary just using M,
    B, D, A, O
  • In BNF form, this can be defined as
  • ltconsonantgt M B D
  • ltvowelgt A O
  • ltbaby-talkgt ltconsonantgt ltvowelgt
  • In English, the above three BNF statements read
  • Consonant is (defined as) M or B or D
  • Vowel is (defined as) A or O
  • Baby-talk is (defined as) a consonant followed by
    a vowel

50
BNF
  • From this specification you can assume the child
    can make the following set of baby-talk
  • MA MO BA BO DA DO
  • baby-talk ltconsonantgt X ltvowelgt
  • Suppose the child is capable of a brief pause
    between sounds PAUSE, we can define
  • ltbabblegt ltbaby-talkgt PAUSE ltbabblegt
    ltbaby-talkgt

51
Date Syntax in BNF
  • Purpose Write BNF
  • Who Groups of 2-3
  • Task Write a BNF specification for date syntax
    dd-mmm-yyyy in the form 04-Nov-2002
  • Allow illegal but syntactically correct dates
    (e.g. 93-Feb-2999)
  • Date is always expressed with two digits and year
    with four digits, while month uses 3-letter
    abbreviation
  • Product Alphabet and BNF specification of the
    syntax
  • Time 5 minutes

52
Date Syntax BNF
  • Possible Solution
  • ltmonthgt JanFebMarAprDec
  • ltdigitgt 0123456789
  • ltddgt ltdigitgtltdigitgt
  • ltyyyygt ltdigitgtltdigitgtltdigitgtltdigitgt
  • ltdategt ltddgt - ltmonthgt - ltyyyygt

53
OOP vs. Functional JS vs. Scheme?
  • Students interested in learning more about
    functional paradigm using Scheme programming
    language can use the information posted on the
    class web page for a self-paced study.
  • We will learn a little more about OOP and
    introduce you to JavaScript (JS) to help you do
    your group project.

54
Scheme Syntax
  • ltlettergt ltagtltZgt
  • ltdigitgt 1230
  • ltintegergt ltdigitgtltintegergtltdigitgt
  • ltrationalgt ltintegergt / ltintegergt
  • ltinexactgt i ltintegergt . ltintegergt
  • ltbooleangt true false
  • ltvargt ltlettergt ltvargt ltlettergt
  • ltcongt ltintegergt ltrationalgt ltinexactgt
    ltbooleangt
  • ltprmgt - .

55
Scheme Syntax
  • ltexpgt ltvargt
  • ltcongt
  • (ltprmgt ltexpgt ltexpgt)
  • (ltvargt ltexpgt ltexpgt)
  • (cond (ltexpgt ltexpgt) (ltexpgt ltexpgt))
  • ltdefgt (define (ltvargt ltvargt ltvargt) ltexpgt)
  • More on this later

56
Compiler Parse Tree
  • The compiler will build a parse tree to represent
    the program based on the grammar
  • A parse tree is a graphical representation of the
    syntactical structure of a statement

57
Example Syntax Diagram and Parse Tree for
Expression x y z
58
Compiling a Program
  • The steps completed by a compiler are
  • Lexical Analysis identify the individual tokens
    in the language statements reading symbol by
    symbol
  • Syntax and Semantics Analysis Build a parse
    tree by reading the tokens analyzed by Lexical
    analyzer and Identify the intent of the program
    statements
  • Code Generation Produce machine language
    instructions

59
Program Translation Process
60
Program Translation Parsing
  • Early parsers required fixed-format for source
    code
  • Modern 3GL are free-format languages
  • Example
  • if cost lt cashOnHand pay cash else use
    credit card
  • is understood by the modern parsers correctly,
    but, makes it harder to read for humans, so, we
    write it as
  • if (cost lt cashOnHand)
  • pay cash
  • else
  • use credit card
  • Semi-colons are typically used to indicate end of
    each statement
  • Key words are defined which cannot be used in
    programs to represent anything other than what
    they are defined for

61
Choosing a Language
  • Choose based on
  • What is available
  • Required interface
  • What do you know best?
  • More importantly Which language lends itself
    best to the problem at hand?

62
Some of the widely used 3GL
  • Visual Basic
  • 1987
  • Create complex user interfaces
  • Uses standard Windows features
  • Event-driven user controls the program
  • C
  • 1972
  • Efficient code
  • Portability
  • C
  • Enhancement of C with OOP concepts

63
What about Java?
  • The Hype about Java
  • Java is a simple but powerful language for
    creating safe, portable, robust object-oriented,
    multithreaded programs.
  • So, what does it all mean?
  • Simple and Powerful
  • It should be easy to learn, but you can achieve a
    lot. There should be no surprising features, and
    no fiendishly complicated ones either.
  • Safe and Robust
  • Java is intimately connected with the Internet.
  • you can use Java to write programs which run on
    other peoples computers when they read your Web
    page.
  • In such circumstances, programmers and users
    want assurance that bugs in the code cannot
    accidentally do damage to the remote computers on
    which it is run, and that a malicious programmer
    cannot deliberately do damage.
  • So a lot of thought has gone into making the
    behavior of programs predictable, and being able
    to run them securely.

64
Java
  • Portable
  • i.e. it must be architecture neutral.
  • It is very easy to write C programs which can
    discover the word-length of your machine, the
    precision of your floating-point representation,
    etc. These are not architecturally neutral!
  • Another issue related to portability is character
    sets. Java wants to be able to represent
    information in any language - and therefore does
    not want to be tied to a character set associated
    mainly with English. A character in Java is
    encoded using Unicode, which uses 16 bits per
    character!
  • Aside
  • Java is Multithreaded
  • Not many languages have threads built-in, but
    such concurrency is very useful if you are trying
    to provide a graphical user interface (GUI) to an
    application or if you are communicating with
    another computer. It is possible to add library
    features to C for this sort of thing, but then
    the responsibility for handling the interactions
    between threads is usually just an added burden
    on the programmer. In Java threads have been
    there from the start - and are therefore easier
    to manage.

65
4GLVery High-Level Languages
  • Programmer specifies the desired results the
    language develops the solution
  • Many times more productive with a 4GL than a
    procedural language
  • Query Languages
  • Retrieve information from databases
  • Easy to learn and use

66
5GL Natural Languages
  • Resemble natural or spoken English
  • Translates human instructions into code the
    computer can execute

67
OOP Object-Oriented Programming
  • Object
  • Self-contained unit of data and instructions
  • Includes
  • Related facts (data)
  • Related functions (instructions to act on that
    data)
  • Example
  • Object cat
  • Data feet, nose, fur, tail
  • Functions eat, purr, scratch, walk
  • Cat Kitty, Susan
  • More on OOP in another lecture presentation

68
Summary
  • All programs must be translated into machine
    language in order to run
  • There are at least three identifiable programming
    paradigms, each with its own special features
  • High level languages provide machine independence
    and better productivity for developers and need
    some intermediary like compliers to translate to
    machine language
  • Languages are specified using grammars. This
    allows compilers to translate the programs
    unambiguously by creating appropriate parse trees
  • Traditional programming concepts include
  • Variables, Data Types, Data Structures
  • Assignment Statements
  • Control Statements like Selection and Repetition
Write a Comment
User Comments (0)
About PowerShow.com