Chapter 5 Programming Languages - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Chapter 5 Programming Languages

Description:

Can complicate the task of modifying the program should it become necessary ... Coercion is frowned upon by many language designers. ... – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 29
Provided by: Will149
Category:

less

Transcript and Presenter's Notes

Title: Chapter 5 Programming Languages


1
Chapter 5 Programming Languages
  • COSC 1309
  • Logic Design

2
5.1 Historical Prespective
  • Before programming languages
  • Programmers had to program using the computers
    Opcodes (native machine language)
  • Assembly Language was created which used
    mnemonics (short phrases like LD for Load, ST
    for Store JMP for Jump etc)
  • Also, special names were given to registers like
    R1, R2, R3 . To registers in the CPU.

3
Assembly Language
  • In the beginning programmers using assembly
    language would write code on paper and manually
    translate it to machine code.
  • The above translation procedure was recognized as
    something that a computer could do so the
    Assembler was born.

4
Why other Programming languages
  • Unfortunately programs written in assembly
    language
  • were machine dependent
  • could not be easily transported to another
    machine design because it was built to fit a
    machines particular instruction set and registers
  • Programmer was stuck to think in small steps

5
Programming Languges
  • First generation --- Machine Language
  • Second generation --- Assembly Language
  • Third generation --- High level languages like
    Fortran and Cobol emerged
  • Grace Hopper created the first compiler ( program
    that translates a high level language to machine
    code
  • Interpreters also emerged as a way to translate a
    high level language into machine code.

6
Programming Paradigms
  • Programming languages has developed along
    different paths or alternate approaches to the
    programming process (programming paradigms)
  • Imperative paradigm a.k.a procedural paradigm
  • Traditional approach finding an algorithm to
    solve the problem and then expressing the
    algorithm as a sequence of commands
  • Ex Pascal, Fortran, C, Cobol, BASIC

7
Programming Paradigms
  • Declarative Paradigm
  • Asks the programmer to describe the problem
  • The trick is to discover a general problem
    solving algorithm
  • Problems can then be expressed in a form
    compatible with this algorithm and then applying
    the algorithm
  • Ex Prolog and GPSS

8
Programming Paradigms
  • Functional Paradigm
  • Views the process of program development as
    connecting predefined black boxes
  • Each accepts inputs and produces outputs.
  • These boxes are referred to as functions
  • The primitives of functional languages are
    elementary functions
  • Ex ML,Scheme , Lisp --- used in AI research

9
Programming Paradigms
  • Object oriented languages (OOP)
  • Units of data are viewed as active objects
    rather than the passive units envisioned by
    traditional imperative paradigms.
  • A template is used called a Class and from that
    class an object is created which has access to
    member variables and functions
  • Ex C , Java , C , Smalltalk, Visual Basic

10
5.2 Traditional Programming
  • This section covers concepts found in the
    imperative object oriented programming
    languages
  • Look at Appendix D for a more extensive
    background of imperative OO languages

11
5.2 Variables and Data Types
  • All locations in main memory have to be
    referenced by a descriptive name rather than a
    numeric address this is known as a variable.
  • The type of data and the operations that can be
    performed on that data are called the data
    type.
  • Ex int, float, double, char, long, string,
    boolean

12
5.2 Data Structure
  • Variables in programs are often associated with a
    data structure --- a conceptual shape or
    arrangement of data
  • For example
  • Text is normally viewed as a long string of
    characters
  • Where as sales records may be viewed as a
    rectangular table of numeric values

13
5.2 Data Structure
  • One common data structure is the homogeneous
    array
  • Which is a block of values of the same type such
    as a one-dimensional list or a two dimensional
    table with rows and columns.
  • Example declaration of a two dimensional array
  • int Scores 2 9
  • A heterogeneous array is a block of data in which
    different elements can have different types.

14
5.2 Constants and Literals
  • Sometimes a fixed or predetermined value is used
    in a program this called a literal
  • EX EffectiveAlt ? Altimeter 645
  • The 645 is called the literal
  • Often literals are not used because they
  • Mask the meaning of statements in which they
    appear
  • Can complicate the task of modifying the program
    should it become necessary

15
5.2 Constants and Literals
  • To solve the problems of literals, programming
    languages allow descriptive names to be assigned
    specific, non-changeable values.
  • Such names are called constants
  • Ex in programming language Ada
  • AirportAlt constant Integer 645

16
5.2 Assignment Statements
  • Once variable names and constants are declared
    then the programmer can begin to describe the
    algorithm.
  • This is done by using imperative statements
  • Or Assignment Statements which requests that a
    value be assigned to a variable
  • More precisely , stored in the memory area
    identified by the variable
  • Operator Precedence is maintained PEMDAS in the
    assignment statements

17
5.2 Control Statements
  • A control statement is an imperative statement
    that alters the execution sequence of the program
  • EX
  • If then , If then else
  • While loop
  • Switch statement

18
Chapter 5
  • 5.4 Language Implementation

19
The Translation Process
  • The process of converting a program from one
    language to another is called translation.
  • Source program the program in its original form
  • Object Program -- the translated version of the
    source program
  • The Translation Process consists of three
    activities
  • Lexical Analysis
  • Parsing
  • Code Generation

20
Lexical Analysis
  • The process of recognizing which strings of
    symbols from the source program represent a
    single entity.
  • Read the source program symbol by symbol and
    identifies which groups of symbols represent
    single units classifying those units as
  • Numeric Values, words, arithmetic operators,
    skipping over comments and so on.
  • As each unit is classified, the lexical analyzer
    generates a bit pattern known as a token to
    represent the unit and hands the token to the
    parser.

21
Parser
  • Views the program in terms of lexical units
    (tokens) rather than individual symbols.
  • Groups units into statements
  • Identifying the grammatical structure of the
    program and recognizing the role of each
    component.
  • Earlier languages required that statements be
    placed in a certain position on the page these
    were called fixed-format languages
  • Today the position is not critical thus we call
    most programming languages free-format languages

22
Parser
  • In order for the parser to identify statements
    regardless of the spacing key words and
    characters must be added
  • For example
  • Ending each statement with a semicolon and or
    using reserved words like if , else, then, while,
    for ..
  • The parsing process is based on a set of syntax
    rules this defines the syntax of the programming
    language.
  • One way of expressing the syntax rules is by
    using a syntax diagram which is a pictorial
    representation of a programs grammatical
    structure.

23
Syntax diagram of the if then else statement
then
If
Boolean Expression
Statement
else
Statement
24
Parser
  • Notice that the terms that actually appear in an
    if-then-else statement are enclosed in ovals
    these are called terminals
  • Those that appear in rectangles such as Boolean
    Expressions and statements require further
    description these are called non-terminals

25
Parser
  • Using the syntax rules described on page 239
    figure 5.18 xy X z can be represented in a
    pictorial form by a parse tree. (see fig. 5.19)
  • The syntax rules describing a programs
    grammatical structure must not allow two distinct
    parse trees for one string, since this would lead
    to ambiguities.

26
Parser
  • As a parser analyzes the declarative statements
    in a program, it records the information in these
    statements in a table called the symbol table.
  • The symbol table contains such information as
    which variables have been declared and what data
    types and data structures are associated with
    those variables.
  • Z?XY if X and Y for example are not of the same
    data type then the parser will choose to convert
    one to the other type like int to real this is
    called Coercion

27
Parser
  • Coercion is frowned upon by many language
    designers. They argue that the need for coercion
    usually indicates a flaw in the programs design
    and therefore should not be accommodated by the
    parser.
  • Most modern languages are Strongly Typed, which
    means that all activities requested by a program
    must involve data of agreeable types without
    coercion.
  • Parsers for these languages report all type
    conflicts as errors.

28
Code generation
  • The final activity in the translation process is
    code generation.
  • This is the process of constructing the
    machine-language instructions to implement the
    statements recognized by the parser.
  • One of the issues that code generation faces
    besides translating the statements into machine
    code is producing efficient code.
  • Example consider x?yz w?xz these
    statements could be translated as individual
    statements which would produce inefficient code.
  • The idea is that since x and z are already in the
    CPUs general purpose registers there is no need
    to load them from memory again. Insights such as
    these are called code optimization.
  • Read page 242 Last Paragraph
Write a Comment
User Comments (0)
About PowerShow.com