Programs: Programs Are Written in Programming Languages
Programs: Programs Are Written in Programming Languages
A PL is
A special purpose and limited language
A set of rules and symbols used to construct a computer
program
A language used to interact with the computer
Ms Qurat-ul-Ann
Programming Languages
Programming languages allow programmers to code
software.
The three major families of languages are:
Machine languages
Assembly languages
High-Level languages
Ms Qurat-ul-Ann
Computer Languages
– Machine Language
• Uses binary code
• Machine-dependent
• Not portable
Assembly Language
Uses mnemonics
Machine-dependent
Not usually portable
High-Level Language (HLL)
Uses English-like language
Machine independent
Portable (but must be compiled for different platforms)
Examples: Pascal, C, C++, Java, Fortran, . . .
Ms Qurat-ul-Ann
Machine
Language
The representation of a computer program which is
actually read and understood by the computer.
A program in machine code consists of a sequence of machine
instructions.
Instructions:
Machine instructions are in binary code
Instructions specify operations and memory cells involved in the
operation
Example: Operation Address
Ms Qurat-ul-Ann
Assembly Language
A symbolic representation of the machine language of a
specific processor.
Is converted to machine code by an assembler.
Usually, each line of assembly code produces one machine
instruction (One-to-one correspondence).
Programming in assembly language is slow and error-
prone but is more efficient in terms of hardware
performance.
Mnemonic representation of the instructions and data
Example:
Load Price
Add Tax
Store Cost
Ms Qurat-ul-Ann
High-level language
A programming language which use statements consisting
of English-like keywords such as "FOR", "PRINT" or “IF“, ...
etc.
Each statement corresponds to several machine language
instructions (one-to-many correspondence).
Much easier to program than in assembly language.
Data are referenced using descriptive names
Operations can be described using familiar symbols
Example:
Cost := Price + Tax
Ms Qurat-ul-Ann
Procedural Languages
Early high-level languages are typically called
procedural languages.
Procedural languages are characterized by sequential
sets of linear commands. The focus of such languages
is on structure.
Examples include C, COBOL, Fortran, LISP, Perl,
HTML, VBScript
Ms Qurat-ul-Ann
Structured Programming
A method for designing and coding programs in a
systematic, organized manner.
It combines the principles of top-down design,
modularity and the use of the three accepted control
structures of sequence, repetition and selection.
Ms Qurat-ul-Ann
Syntax & Semantics
Syntax:
The structure of strings in some language. A language's
syntax is described by a grammar.
Examples:
Binary number
<binary_number> = <bit> | <bit> <binary_number>
<bit> =0|1
Identifier
<identifier> = <letter> {<letter> | <digit> }
<letter> =a|b|...|z
<digit =0|1|...|9
Semantics:
The meaning of the language
Ms Qurat-ul-Ann
Syntax & Grammars
Syntax descriptions for a PL are themselves written in
a formal language.
E.g. Backus-Naur Form (BNF)
The formal language is not a PL but it can be
implemented by a compiler to enforce grammar
restrictions.
Some PLs look more like grammar descriptions than
like instructions.
Ms Qurat-ul-Ann
Compilers & Programs
Compiler
Ms Qurat-ul-Ann
Compilation into Assembly L
Source Assembly
Program Compiler Language
Ms Qurat-ul-Ann
Compilers & Programs
Source program
Ms Qurat-ul-Ann
Compilers & Programs
Object program
Output from the compiler
Equivalent machine language translation of the source
program
Files usually have extension ‘.obj’
Executable program
Output from linker/loader
Machine language program linked with necessary libraries &
other files
Files usually have extension ‘.exe’
Ms Qurat-ul-Ann
Program Errors
Syntax Errors:
Errors in grammar of the language
Runtime error:
When there are no syntax errors, but the program can’t
complete execution
Divide by zero
Invalid input data
Logical errors:
The program completes execution, but delivers
incorrect results
Incorrect usage of parentheses
Ms Qurat-ul-Ann
Compilation
Source Target
Program Compiler Program
Interpreter Output
Input
Ms Qurat-ul-Ann
• Translates instructions to machine code line-by-line or
statement by statement.
• It executes statement before translating the next statement
of the source program
• If there is an error in the statement , the interpreter stops
working and displays an errors message.
• The disadvantage of interpreter is that it is not very efficient
• The interpreter does not produce an object code .it must
convert the program each time it is executed .
• Visual Basic uses interpreter.
Ms Qurat-ul-Ann
Compilation vs. Interpretation
Compilation:
Syntax errors caught before running the program
Better performance
Decisions made once, at compile time
Interpretation:
Better diagnostics (error messages)
More flexibility
Supports late binding (delaying decisions about
program implementation until runtime)
Can better cope with PLs where type and size of variables
depend on input
Ms Qurat-ul-Ann