# Class 02: Detour: A Quick Introduction to Pascal

Back to An Introduction to The Course. On to Parameter-Passing Strategies.

This outline is also available in PDF.

Held: Monday, 29 August 2011

Summary: We quickly explore the Pascal programming language, which will serve as our source language for the semester.

Related Pages:

Notes:

Overview:

• Leftovers: The Back End.
• A Short History of Pascal.
• Key Components of Imperative Langauges.
• Detour: BNF.
• Overall Structure.
• Variables and Types.
• Basic Operations.
• Conditionals.
• Types.
• Looping Structures.
• Procedures.

## A Short History of Pascal

• Pascal is a descendendant of Algol.
• Algol was one of the first four big programming languages:
• Fortran: Designed for mathematical computation. Popular primarily in Physics.
• COBOL: Designed for information processing. Popular primarily in Business and Government.
• LISP: Designed for AI. Popular primarily at MIT.
• Algol: Designed for comptuer scientists to use to express algorithms. Popular primarily in Europe.
• Algol is an imperative language. You should know what that means.
• Niklaus Wirth designed Pascal with two goals in mind
• Getting rid of many things he didn't like that his colleagues had added to Algol
• Creating a language good for teaching
• Many computer scientists still use a Pascal-like syntax when writing imperative algorithms.

## Why Pascal?

• We're using Pascal as our source language for a few reasons.
• It is a simple and straightforward language which should not take much time to learn.
• It was designed to be easy to compile. (We'll see evidence of this design at various places.)
• It has fairly strong typing, and typing is useful to consider when you're writing a compiler.
• Pascal has had enough of an influence that it's useful for people to see it at least once in their undergraduate careers.

## What Belongs in an Imperative Language?

• Pascal is an imperative language, so we should consider the key aspects of imperative languages as a way of helping us decide what to look at.
• Here are some things I expect in an imperative language:
• Variables
• Types for those variables (sometimes implicit, sometimes explicit)
• Type definitions
• Basic operations for input, output, and assignment
• A mechanism for sequencing operations
• Control structures for conditional executation
• Control structures for repeated evaluation
• Procedures (that permit recursion)
• We'll cover as many of these issues as we can at a high level and return to more precise details throughout the semester.

## Detour: BNF

• Backus-Naur Form (BNF) is a notation for the syntax of languages.
• Basically, you give a syntactic unit and then explain how that unit is built.
• In English, we might write
<simple sentence> ::=
<subject> <intransitive verb>
| <subject> <transitive verb> <direct object>
| ...

• We'll consider BNF in depth when we do parsing.
• Note that some of the things that Wirth describes in BNF we'll handle with a lexer, rather than a parser.

## Overall Structure

• Pascal programs have an interesting structure.
• The structure looks something like the following
program name(ports);

const
constant declarations

type
type declarations

var
variable declarations

procedure and function definitions

begin
sequence of statements separated by semicolons
end .

• All of the definitions and declarations are optional.

## Variables and Types

• Variable (and other) names in Pascal look much like they do in C and Java: Sequences of alphanumerics that begin with an alphabetic character. (I'll admit that I never remember whether other characters are permitted, and implementations seem to differ. The standard says only letters and numbers.)
• Variables must be declared (as in Java and C) and typed.
• Once you've indicated that you are declaring variables with the var keyword, you write variables names (separated by commas), a colon, and a type. For exmaple
var
x, y: integer;
z: real;

• The basic types are integer, real, and Boolean.
• The primary compound type is the array, written as
array[lowest-index..highest-index] of type

• One can also build records (something like classes, but without the procedures) with
record variable-declarations end

• Pascal also makes it easy to define types that are collections of names (enumerated types). For example
type
weekdays = (Monday, Tuesday, Wednesday, Thursday, Friday);

## Simple Statements

• Assignment is done with the := operation. For example,
x := x+1;

• The extra ln indicates that the end-of-line character should be read.
• Output is done with write and writelin.
• The extra ln indiates that the end-of-line character should be printed.
• Neighboring statements are separated by semicolons.
• Sequences of statements can be grouped together into a compound statmeent by surrounding the sequence with the keywords begin and end.

## Conditionals

• Simple
if test then statement

• With alternative
if test then statement else statement

• Case statements
case exp of
val1: statement1
val2: statement2
...
valn: statementn
end

• Wirth felt that the values should encompass all possible values of the expression.
• Implementors have usually added a default or else case.
• Many implementors have allowed ranges and groups of values on the left.
• Case statements can be a lot of fun to compile. (We'll return to the issue later in the semester.)

## Compound Types

• The primary compound type is the array, written as
array[lowest-index..highest-index] of type

• One can also build records (something like classes, but without the procedures) with
record variable-declarations end

• Pascal also makes it easy to define types that are collections of names (enumerated types). For example
type
weekdays = (Monday, Tuesday, Wednesday, Thursday, Friday);

## Loops

• For loops are intended only for enumerated types.
for var := starting-value to ending-value do
statement

• You can loop from high to low using downto rather than to
• In some implementations, you can specify a step with step val.
• Note that you can use user-defined types as well as integers.
• While loops have a form similar to that of C
while test do
statement

• Repeat loops put the test at the end
repeat
series-of-statements
until test

## Procedures

• Pascal differentiates procedures, which do not return values, from functions, which do.
• The form looks much like that of a program
procedure (typed-parameters);
var
variables
local-procedures
begin
statement-list
end

• For functions, you must also specify a return type.
• Return values from functions are treated strangely.
• You must assign to the function variable (which has the same name as the function).
• The value of that variable when the function terminates is the value of the function.

Back to An Introduction to The Course. On to Parameter-Passing Strategies.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Dec 7 10:26:31 2011.
The source to the document was last modified on Fri Aug 26 13:03:12 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2011F/Outlines/outline.02.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu