HIGH LEVEL PROGRAMMING LANGUAGES

1. ATTRIBUTES OF HIGH-LEVEL LANGUAGES

  1. clear structure
  2. efficient to use High Level Language to solve problem,
  3. program source code readable

 

2. TYPES OF HIGH LEVEL LANGUAGES

a) General Purpose

eg. ALGOL, BASIC, Pascal

b) Special Purpose

c) Commercial

d) Scientific

 

 

3. THE CHARACTER SET

 

 

4. RESERVED WORDS

 

5. DATA TYPES AND STRUCTURES

Data handled by program as two types

  1. Variables - values change as program runs,
  2. Constants - values do not change.

Variables are given names or Identifiers in most High Level Languages.

Some High Level Languages have no named constants, eg. BASIC, FORTRAN; others have named constants, eg. Pascal, ALGOL.

Some languages need variables to be Declared before use;

Variables have Scope, ie. that part of the program in which they may be used and have a value,

Local variables declared for use within one block or nested blocks, eg. BBC BASIC, Pascal

Global variables declared at the start of the main block; valid in all blocks of program.

 

Data Types

Purpose of data types is to aid readability and understanding of program. Some languages allow any type to be stored in any variable, eg. PROLOG, LOGO; others allow conversion between types, eg. BBC BASIC, Pascal.

 

6. OPERATIONS

 

7. INPUT AND OUTPUT

Facilities simplify entry of data into system and retrieval of info. from it.

 

8. CONTROL STRUCTURES

All languages have methods for transferring control from one section to another.

a) Sequencing flow of control from one instruction to next;

instructions written one after another separated by Separator,

semi-colon in Pascal,

colon in BASIC.

BASIC uses line numbers to show sequence of instructions.

 

b) Looping repeating instructions or groups of instructions.

Most languages allow looping a set number of times

- Iterative Looping, eg.

BASIC

FOR X = 1 TO 10 STEP 2 ... NEXT X, 

Pascal

For x := 1 To 10 do ... 

Many languages allow looping while a condition is true or until a condition is true, eg.

Pascal

Repeat ... Until <condition> and 
While <Condition> do ... 

 

Some languages support Recursion;

this form of looping requires the use of local variables and passing parameters by value, eg. Pascal

Function Factorial(n : Integer) : Integer; 
Begin 
If n=1 
Then 
   Factorial := 1 
Else 
   Factorial := n * Factorial(n-1); 
End; 

 

c) Branching conditional transfer, eg. BASIC

IF <condition> THEN ... ELSE ... ,

though many BASICs do not support the ELSE construct.

d) Multiway Branching

transfer depends on value of a condition, eg BASIC

ON <expression> GOTO <line number>, <line number>

Pascal

Case <expression> of

<case list> : <statement>

<case list> : <statement>

End;

FORTRAN uses a computed GOTO eg.

GOTO <expression>

 

e) Unconditional Branching

GOTO <line number>

 

9. PROGRAM STRUCTURE

Structure of a program is specified by rules of Syntax.

Program split into Blocks, each block doing some specific tasks; blocks given different names in different languages, eg.

BASIC FORTRAN - called subroutines

Pascal, ALGOL - called Procedures, Functions,

- program structured into blocks,

- Block-structured languages,

- block starts with Begin and finishes with End

Procedures, functions, subroutines accessed by calls from different parts of program. Calls can be Nested.

Some languages allow blocks to call themselves - Recursion; a powerful programming concept as uses few lines of code in high level language; hard to translate into machine code.

Some languages allow procedure calls to pass Parameters; needs use of Local variables; procedures passing parameters support recursion.

10. DIAGNOSTICS

Detection and location of errors in program code done by compiler (interpreter).

Compiler attempts to determine position of error in source code and cause of error.

Produces Diagnostic Error Message describing error and position of error, though position is where error detected - may not be where actual error is.

eg. BASIC Type Mismatch error at line 2025

Pascal Undeclared variable at line 646.

Sophisticated languages usually have sophisticated diagnostics.

11. FILE HANDLING

Most high level languages have some facility for storing and retrieving data from backing store.

May be rudimentary as in FORTRAN.

COBOL inputs and outputs to files; language designed for file-handling; merge, update, sort facilities built in.

Pascal & BASIC use version of input/output statements in which channel number of file specified.

 

LANGUAGE DEFINITION

A language may be defined by;

Generative methods - procedure to generate all legal sentences in the language,

Recognition methods - decides legality of any sentence.

Generative method more common when defining programming languages.

Recognition close to methods used by most compilers.

Language definition systems do not work with real languages, eg. English, as these too ambiguous, metaphorical, imprecise. Works for computer languages.

 

BACKUS-NAUR FORM (BNF)

Notational method of presenting language generation. Developed by John Backus & Peter Naur in 1960 to produce ALGOL60. (Backus developed FORTRAN in 1957).

BNF may be regarded as a special language - Meta-language.

A program consists of a sequence of elementary symbols, eg. names of data items, punctuation, reserved words.

Permitted elementary symbols in any language termed Terminal Symbols. Terminal symbols combined forming complex constructs, eg. conditions, statements, programs.

Each construct which is made from terminal symbols called a Syntactic Category.

Syntax definition specifies precisely how a particular syntactic category built from

members of other syntactic categories, terminal symbols of the language.

Built into a set of rules termed a Production.

Set of terminal symbols, set of syntactic categories, set of productions define the Syntax of a Language

 

SYNTAX OF A PROGRAMMING LANGUAGE IN BNF

1. Definitions and Notation

a) ::= or means 'is defined by'

b) | means or

c) < > enclose syntactic categories (syntactic variables)

d) Terminal symbol an elementary symbol of the language not needing further definition, eg. digits, letters, etc.

e) Syntactic Category composed of a set of elementary symbols; also called syntactic variable

f) Production the formulation of a syntactic category from e) and d) above, eg. <operator> ::= + | - | * | /

g) { } mean that the enclosed category or symbol is repeated 0 to times.

 

2. Generation of a Legal Sentence

<statement> ::= <conditional> | <loop> | <assignment>

<conditional> ::= if <condition> then <statement>

<loop> ::= while <condition> do <statement>

<assignment> ::= <set name to> <expression>

<expression> ::= <name> | <number> | <name> <operator> <name>

<name> ::= <letter> {<letter> | <digit>}

<letter> ::= A | B | C ... X | Y | Z

<digit> ::= 0 | 1 ... 8 | 9

<operator> ::= + | - | / | *

<condition> ::= <expression> <conditional operator> <expression>

<conditional operator> ::= = | <> | AND | OR | > | < | >= | <=

<number> ::= <digit> {<digit>}

 

 

3. Recursion

Considering production for language fragment above;

<statement> ::= <conditional> | <loop> | <assignment>

recursive production, ie. defined in terms of itself. OK as long as is possible to define <statement> non-recursively.

conditional expresses selection or decision,

loop expresses iteration,

assignment explains how to calculate a value and assign to data item; this non-recursive category and terminates production.

Recursion powerful feature of BNF, allows complex programs to be built from few productions.

 

SYNTAX DIAGRAMS

A diagrammatic, less ambiguous way of expressing syntax of a language.

Syntactic categories represented by rectangles; terminal symbols by circles; arrows show path of definition.

Simple syntax for Pascal

a) Expression

b) Simple Expression

c) Term

 

d) Factor

 

e) Simple Type

f) Type

g) Variable

h) Identifier

i) Constant

j) Unsigned Number

 

 

An alternative method of analysing the syntax of a program is to use state tables. The program is examined character by character. The state table contains a row for each state and a column for each character which may be encountered. An entry in the table contains either a jump to another state, and exit instruction or an error condition. An exit instruction indicates that the structure defined by the state table has been recognised.The state table below is for the recognition of signed, decimal numbers. The symbol * indicates that the end of the number has been reached.

For each new number entry is at state 1.

STATE

 

NEXT CHARACTER

     
 

+ or -

Digit

.

E

*

1

 

4

2

3

 

2

 

5

     

3

6

7

     

4

 

4

2

3

EXIT

5

 

5

 

3

EXIT

6

 

7

     

7

 

7

   

EXIT

 

Valid numbers include:                  Invalid numbers include:

21.3   2.3E-2    3.4E6  .62                     -12 97.     2.3E4.5

 

 

The syntax diagram for valid numbers from the state table above.