Lectures 09 to 11

Lectures 09 to 11 - Pascal ALGOLs like language introduced...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Pascal ALGOL’s like language, introduced by Wirth (1971), but more reliable , efficient , and simple for pedagogic purposes (including for system programming). PASCAL introduced a much richer type system than ALGOL: A) type and “ Const ” declarations. B) Enumeration Types ”: To handle non-numeric data, eliminating the insecurity of overworking integers as such needed types. (exists in C, Pascal, and Ada) type month = (Jan, Feb, March, Apr, …, Dec); Day = (Sun, Mon, Tue, …, Sat); In C enum EmployeeGender {Male, Female} : enum StudentClass {Fresh, Soph, Junior, Senior} In Ada : type Days is ((Sun, Mon, Tue, …, Sat); They are true ADTs , with user defined data type, and system built-in operators: :=, succ, pred, =, ≠, <, >, ≥, ≤ Advantages: 1- High level application oriented, allowing the language to cover wider area of applications. 2- Efficient use of memory to represent the type values. 3- Secure use of type elements, the compiler protects against any meaningless operations by the users on elements of the defined types. Pred(Jan) and succ (Sat) will produce compile time errors. Question: Can we still use the standard input/output commands for enumerated types? Problems ? Yes : 1- No Input/output built in operations! (why?) 2- Overloaded enumerated literal constants when appearing in different definitions at the same environment. Ex: type favoriteColors = ( red, yellow, magenta, brown, aqua, blue, green ); TrafficLightColors = ( red, yellow, green ); for color in ‘red’ . .’green’ do
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Notice that color is implicitly typed with the specified discrete range by the compiler. The discrete range is ambiguous(!) since the compiler will not know to which type it belongs: favoriteColors or TrafficLightColors ? Hence, C and Pascal do not allow the same element name to be used in more than one enumeration type in the same scope. Solution by Ada-- for color in favoriteColors ‘(‘red’) . . favoriteColors’(’green’) do Ada allows it but to resolve the name overloading: C) Subrange Types : Pascal (and other languages like Ada) allows the programmer to define subranges of only discrete types (enumerated, int, characters), where it inherits all of its parent defined set of operations. type uppercase = ‘A’. .’Z’; index = 1..100; WeekDays = Mon. .Fri; Advantages: 1- Enhances readability. 2- Security, errors due the assignment of out of range value will be detected at compile time, in case of literal constant value; or at run time, 3- Efficient memory representation of the type values. in case of expression/variable assignment. * Notice that when the “subrange” inherits its parent’s set of operations, it introduces security loophole ”. For example, “dayOfMonth = 1. .31”, it is maybe ok to add/subtract the days of month, but what about dividing/multiplying them??!! Subranges are built in Algol 68, Pascal, and Ada; but not C++/Java (?).
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/23/2011 for the course CSE 324 taught by Professor Soliman during the Spring '11 term at NMT.

Page1 / 16

Lectures 09 to 11 - Pascal ALGOLs like language introduced...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online