A Notation to Describe the Syntax of Oberon

Cours A Notation to Describe the Syntax of Oberon, tutoriel & guide de travaux pratiques en pdf.

Elementary Data Types

We have previously stated that all variables need be declared. This means that their names are introduced in the heading of the program. In addition to introducing the name (and thereby enabling a compiler to detect and indicate misspelled identifiers), declarations have the purpose of associating a data type with each variable. This data type represents information about the variable which is permanent in contrast, for example, to its value. This information may again aid in detecting inconsistencies in an erroneous program, inconsistencies that are detectable by mere inspection of the program text without requiring its interpretation.
The type of a variable determines its set of possible values and the operations which may be applied to it. Each variable has a single type that may be deduced from its declaration. Each operator requires operands of a specific type and produces a result of a specific type. It is therefore visible from the program text, whether or not a given operator is applicable to a given variable.
Data types may be declared in the program. Such constructed types are usually based on composition of basic types. There exist a number of most frequently used, elementary types that are basic to the language and need not be declared. They are called standard types and will be introduced in this chapter, although some have already appeared in previous examples.
In fact, not only variables have a type, but so do constants, functions, operands and (results of) operators. In the case of a constant, the type is usually deducible from the constant’s notation, otherwise from its explicit declaration.
First we describe the standard data types of Oberon, and thereafter elaborate on the form of declarations of variables and constants. Further kinds of data types and declarations are deferred to later chapters.

The Type INTEGER

This type represents the whole numbers, and any value of type INTEGER is therefore an integer. Operators applicable to integers include the basic arithmetic operations
Sign inversion is denoted by the monadic minus sign. Furthermore, there exist the operators ABS(x) and ODD(x), the former yielding the absolute value of x, the latter the Boolean result « x is odd ».
Every computer will restrict the set of values of type INTEGER to a finite set of integers, usually the interval -2N-1 … 2N-1-1, where N is a small integer, often 16 or 32, depending on the number of bits a computer uses to represent an integer. If an arithmetic operation produces a result that lies outside that interval, then overflow is said to have occured. The computer will give an appropriate indication and, usually, terminate the computation. The programmer should ensure that overflows will not result during execution of the program.

The Type REAL

Values of type REAL are real numbers. The available operators are again the basic arithmetic operations and ABS. Division is denoted by / (instead of DIV). Constants of type REAL are characterized by having a decimal point and possibly a decimal scale factor. Examples of real number denotations are 1.5 1.50 1.5E2 2.34E-2 0.0
The scale factor consists of the capital letter E followed by an integer. It means that the preceding real number is to be multiplied by 10 raised to the scale factor. Hence 1.5E2 = 150.0, 2.34E-2 = 0.0234
The important point to remember is that real values are internally represented as pairs consisting of a fractional number and a scale factor. This is called floating-point representation. Of course, both parts consist of a finite number of digits. As a consequence, the representation is inherently inexact, and computations involving real values are inexact because each operation may be subject to truncation or rounding.
The following program makes that inherent imprecision of computations involving operands of type REAL apparent. It computes the harmonic function H(n) = 1 + 1/2 + 1/3 + … + 1/n in two different ways, namely once by summing terms from left to right, once from right to left. According to the rules of arithmetic, the two sums ought to be equal. However, if values are truncated (or even rounded), the sums will differ for sufficiently large n. The correct way is evidently to start with the small terms.

The Type BOOLEAN

A BOOLEAN value is one of the two logical truth values denoted by the standard identifiers TRUE and FALSE. Boolean variables are usually denoted by identifiers which are adjectives, the value TRUE implying the presence, FALSE the absence of the indicated property. A set of logical operators is provided which, together with BOOLEAN variables, form BOOLEAN expressions. These operators are & (and), OR, and ~ (not).

The Type CHAR

Every computer system communicates with its environment via some input and output devices. They read, write, or print elements taken from a fixed set of characters. This set constitutes the value range of the type CHAR. Unfortunately, different brands of computers may use different character sets, which makes communication between them (i.e. the exchange of programs and data) difficult and often tedious. However, there exists an internationally standardised set, the ISO set.
The ISO standard defines a set of 128 characters, 33 of which are so-called (non-printing) control characters. The remaining 95 elements are visible printing characters shown in the following table. The set is ordered, and each character has a fixed position or ordinal number. For example, A is the 66th character and has the ordinal number 65. The ISO standard, however, leaves a few places open, and they can be filled with different characters according to national desires to establish national standards. Most widely used is the American standard, also called ASCII (American Standard Code for Information Interchange). Here we tabulate the ASCII set. The ordinal number of a character is obtained by taking its row number and adding its column number. These numbers are customarily given in hexadecimal form, and we follow this habit here too. The first two columns contain the control characters; they are customarily denoted by abbreviations hinting at their intended meaning. However, this meaning is not inherent in the character code, but only defined by its interpretation. At this point it suffices to remember that these characters are (usually) not printable.

Part 1
1. Introduction
2. A First Example
3. A Notation to Describe the Syntax of Oberon
4. Representation of Oberon Programs
5. Statements and Expressions
6. Control Structures
6.1 Repetitive Statements
6.2 Conditional Statements
7. Elementary Data Types
7.1 The Type INTEGER
7.2 The Type REAL
7.3 The Type BOOLEAN
7.4 The Type CHAR
7.5 The Type SET
8. Constant and Variable Declarations
9. Arrays
Part 2
10. Procedures
11. The Concept of Locality
12. Parameters
12.1 Variable Parameters
12.2 Value Parameters
12.3 Open Array Parameters
13. Function Procedures
14. Recursion
15. Type Declarations
16. Record Types
17. Dynamic Data Structures and Pointers
18. Procedure Types
Part 3
19. Modules
20. Definitions and Implementations
21. Program Decomposition into Modules
22. The concept of sequence
22.1. About input and output
22.2. Files and Riders
22.3. Texts, Readers and Writers
22.4. Standard Input and Output
Part 4
23. Object-oriented Programming
23.1. The origins of object-oriented programming
23.2. Type extensions and inhomogeneous data structures
23.3. Methods
23.4. Handlers and Messages
Appendix: Syntax, Keywords, Standard functions

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *