Note: More information about Pascal is available in this tutorial.
Another good Pascal tutorial is available here.
1. Lexical conventions
µPascal is case insensitive, except for string constants (see below). That is, corresponding upper- and lower-case letters are indistinguishable, unless they occur inside a string constant or a comment.
1.1. Ignored tokens
A comment is a sequence of characters enclosed within a pair of matching braces { ... }, or (*... *). Comments are always ignored. Note that pairs of { ... *) and (* ... }do not represent comments and that nested comments are not permitted. However, comments can extend over several lines.
Other ignored tokens include the newline, horizontal tab, carriage return, and white space.
1.2. Basic tokens of µPascal
In what follows, sequences of characters enclosed within two double quotes (") denote terminals (tokens). Any other sequence of characters represents the name of a lexical class, e.g. letter (see below).
In later sections we will use the following lexical definitions:
| letter |
::= |
"a" | "b" | ... | "z" | "A" | "B" | ... | "Z" |
| digit |
::= |
"0" | "1" | ... | "9" |
1.2.1. Identifiers
An identifier is a finite sequence of letters and digits which begins with a letter. Upper and lower case letters are allowed but there is no distinction between the corresponding lower and upper case letters. Identifiers may be of any length, but only the first ten characters are significant.
| identifier |
::= |
letter ( letter | digit )* |
1.2.2. Numbers
An unsigned integer is a sequence of digits.
| unsigned_integer |
::= |
digit digit* |
A floating point number is defined as follows:
| floating_point |
::= |
unsigned_integer "." unsigned_integer | unsigned_integer "." unsigned_integer exponent |
| exponent |
::= |
( e | E ) ( ( + | - ) unsigned_integer | unsigned_integer ) |
A number must be separated from an identifier or a keyword by at least one token (usually an ignored token).
1.2.3. String constants
A string constant is a sequence of characters enclosed within two single quotes ('). A sequence of two consecutive single quotes is treated as a character in the string in which it occurs and does not terminate the string.
A string constant may not extend beyond the end of the line. A pair of matching braces {} or pairs (* *) within a string constant is not treated as a comment. Corresponding upper and lower case letters are considered different within string constants.
1.2.4. Operators
| add_op |
::= |
"+" | "-" |
| mul_op |
::= |
"*" | "/" | "div" |
| rel_op |
::= |
"<" | "<=" | "=" | "<>" | ">=" | ">" |
1.2.5. Other µPascal tokens
Other recognized tokens appearing in the grammar below are enclosed within double quotes, e.g. ";". Reserved words (keywords) are boldfaced, quoted identifiers, e.g. "begin". Tokens which were defined above are printed in boldface only, e.g. identifier.
2. µPascal grammar
| program |
-> |
program_header block "." |
| program_header |
-> |
"program" identifier ";" |
| block |
-> |
declarations compound_statement |
| declarations |
-> |
declarations declaration | e |
| declaration |
-> |
"var" variable_decls | procedure_decl |
| variable_decls |
-> |
variable_decls variable_decl | |
| |
|
variable_decl |
| variable_decl |
-> |
identifier_list ":" identifier ";" |
| procedure_decl |
-> |
procedure_header block ";" |
| procedure_header |
-> |
"procedure" identifier |
| |
|
"(" parameter_list ")" ";" | |
| |
|
"procedure" identifier ";" |
| parameter_list |
-> |
parameter_group | |
| |
|
parameter_list ";" parameter_group |
| parameter_group |
-> |
"var" identifier_list ":" identifier | |
| |
|
identifier_list ":" identifier |
| compound_statement |
-> |
"begin" statement_list "end" |
| statement_list |
-> |
statement | statement_list ";" |
| |
|
statement |
| statement |
-> |
e /* empty statement */ | |
| |
|
identifier | |
| |
|
identifier "(" expression_list ")" | |
| |
|
identifier ":=" expression | |
| |
|
"while" expression "do" statement | |
| |
|
"repeat" statement_list "until" expression | |
| |
|
"if" expression "then" statement | |
| |
|
"if" expression "then" statement |
| |
|
"else" statement | |
| |
|
compound_statement |
| expression_list |
-> |
expression | expression_list "," expression |
| expression |
-> |
simple_expression | |
| |
|
simple_expression rel_op simple_expression |
| simple_expression |
-> |
term | add_op term | |
| |
|
simple_expression add_op term |
| term |
-> |
factor | term mul_op factor |
| factor |
-> |
identifier | |
| |
|
constant | |
| |
|
"(" expression ")" |
| constant |
-> |
string_constant | number |
| number |
-> |
unsigned_integer | |
| |
|
floating_point |
| identifier_list |
-> |
identifier | |
| |
|
identifier_list "," identifier |
3. Additional Comments
µPascal is a small subset of Pascal. Any program written in µPascal should compile and run correctly when using the GNU Pascal (gpc).
3.1. Data types
µPascal features two standard (predefined) types: integer and real.
3.2. Blocks and predefined identifiers
There are five predefined identifiers. integer and real represent the two basic data types. readln, write, and writeln are the names of standard I/O procedures. readln and write take only one actual parameter, while writeln takes none. readln can be used to read a single number of type integer or real, depending on the type of the actual parameter. write may be used to write out a number (integer or real) or a string constant. The argument to readln is passed by reference, while the argument to write is passed by value. writeln causes printing of a line break.
µPascal follows the usual scoping rules. Note that a nested procedure is not visible outside of its scope. Procedures may be recursive.
Each variable or procedure identifier (other than predefined) must be declared prior to its use.
3.3. Expressions
There are five binary arithmetic operators: +, -, *, /, div. div represents division of integers producing an integer result. + and - have lower precedence than *, /, and div. In addition, + and - may be used as unary operators. Given integer arguments, each operation returns integer result, except for / (division), which always returns a real result. If any of the arguments is of type real, the result is real. div may be applied only to integer arguments.
There are six binary relational operators: <, <=, =, <>, >=, >. These may be applied to numeric operands only and they yield a Boolean result (true or false).
3.4. Statements
UGA Micro Pascal has seven different statements: the empty statement, procedure call, assignment, if statement, while statement, repeat statement, and the compound statement. The semantics of each statement is the same as in Pascal. Remember that in Pascal, you may not assign a double value to an integer variable (the reverse is possible, though). The expression in the if, while and the repeat statements must be a Boolean expression.
Parameters may be passed both by reference and by value.
4. Examples
{ a sample program 1 }
program sample1;
var i, j: integer;
begin
readln(i); { get i }
j := 9 + i * 8; { evaluate j }
write( 'Result is ' );
write( j ); { print j }
writeln
end.
{ a sample program 1R }
program sample1R;
var i: integer;
j: real;
begin
readln(i); { get i }
j := 9 + i * 8.3e3; { evaluate j }
write( 'Result is ' );
write( j ); { print j }
writeln
end.
{ a sample program 2 }
program sample2;
var i, sum: integer;
procedure count( n: integer; var sum: integer );
var i: integer; { local variable }
begin
i := 1;
sum := 0;
while ( i <= n ) do { set-up the loop }
begin
sum := sum + i;
i := i + 1;
end
end;
begin
readln(i); { get i }
count(i, sum); { call count }
write(sum); { write result }
writeln;
end. |