MP Language
MP Language
MP Language
MP
Micro Pascal Language
June 2017
Contents
1 Introduction 3
2 Program Structure 3
2.1 Variable declaration: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Function declaration: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Procedure declaration: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Lexical Specification 5
3.1 Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3 Token Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.4 Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.5 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5 Expressions 10
5.1 Precedence and Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.2 Type Coercions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.3 Index Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.4 Invocation Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.5 Evaluation Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1
7 Built-in Functions 14
8 Scope Rules 15
10 Change Log 17
2
MP
version 1.2
1 Introduction
MP (Mini Pascal) is a language which consists of a subset of Pascal plus some Java language
features.
The Pascal features of this language are (details will be discussed later): a few primitive
types, one-dimensional arrays, control structures, expressions, compound statements (i.e.,
blocks), functions and procedures.
The Java features of this language are as follows:
Conventionally, the sequence ’\n’ must be used as a new line character in MP.
2 Program Structure
MP does not support separate compilation so all declarations (variable and function) must
be resided in one single file.
3
2.1 Variable declaration:
A variable declaration starts with keyword var and then a list of declarations each of
which starts with a comma-separated list of identifiers, a colon (:), a type and ends with a
semicolon.
For example,
var a , b , c : integer ;
d : array [ 1 . . 5 ] of integer ;
e , f : real ;
• function is a keyword
For example,
function foo (a , b : integer ; c : r e a l ) : array [ 1 . . 2 ] of integer ;
var x , y : r e a l ;
begin
...
end
4
2.3 Procedure declaration:
The procedure declaration is like a function one except that the keyword procedure is
used instead the keyword function and there is no colon and the return type after the
parameter declaration part.
For example,
procedure foo (a , b : i n t e g e r ; c : r e a l ) ;
var x , y : r e a l ;
begin
...
end
MP does not support function/procedure overloading. Thus, a function must be defined
exactly once.
MP does not support nested function/procedure as in Pascal. For example: the follow-
ing declaration is invalid in MP
3 Lexical Specification
This section describes the character set, comment conventions and token set in the language.
3.2 Comments
There are three kinds of comments:
5
• A traditional block comment:
(* This is
a block comment *)
All the text from (* to *) is ignored.
• A block comment:
{ This is a block comment }
All the text from { to } is ignored.
• A line comment:
//This is a line comment
All the text from // to the end of the line is ignored.
As designed in C, C++ and Java, the following rules are also enforced:
• Keywords: The following character sequences are reserved as keywords and cannot be
used as identifiers:
break continue for to downto do if then else return while begin end
function procedure var true false array of real boolean integer string
not and or div mod
Some keywords are used as operators, or literals. These keywords are also case-
insensitive.
6
Operator Meaning Operator Meaning
+ Addition - Subtraction or negation
* Multiplication / Division
not Logical NOT mod Modulus
or Logical OR and Logical AND
<> Not equal = Equal
< Less than > Greater than
<= Less than or equal >= Greater than or equal
div Integer division
3.4 Separators
The following characters are the separators: left square bracket (’[’), right square bracket
(’]’), colon (’:’), left bracket (’(’), right bracket (’)’), semicolon (’;’), double dot (’..’) and
comma (’,’).
3.5 Literals
A literal is a source representation of a value of either an integer type, real type, boolean
type or string type.
• A floating-point literal has the following parts: a whole-number part, a decimal point
(represented by an ASCII period character), a fractional part and an exponent. The
exponent, if present, is indicated by the ASCII letter e or E followed by an optionally
signed (-) integer. At least one digit, in either the whole number or the fraction part,
and either a decimal point or an exponent are required. All other parts are optional.
A floating-point literal is of type real.
For example: The following are valid floating literals:
1.2 1. .1 1e2 1.2E-2 1.2e-2 .1E2 9.0 12e8 0.33E-3 128e-42
The following are not considered as floating literals:
e-12 (no digit before ’e’) 143e (no digits after ’e’)
• The boolean literal has two values, represented by the literals true and false, formed
from ASCII letters.
• A string literal consists of zero or more characters enclosed in double quotes ’"’. The
quotes are not part of the string, but serve to delimit it.
It is a compile-time error for a backspace, newline, formfeed, carriage return, tab,
single quote, double quote or a backslash to appear after the opening ’"’ and before
7
the closing matching ’"’. The following escape sequences are used instead:
\b backspace
\f formfeed
\r carriage return
\n newline
\t horizontal tab
\’ single quote
\" double quote
\\ backslash
8
4.2 The integer Type and Values
The values of type integer are 32-bit signed integers in the following ranges:
-2147483648 . . . 2147483647
The following operators can act on integer values:
+ - * div mod < <= > >= <> = /
The first five operators always produce a value of type integer. The next six operators
always result a value of type boolean. The last operator (/) will result a value of type
real when both operands are in type integer.
Here, - represents both the binary subtraction and unary negation operators.
• The binary arithmetic operators +, -, * and /, which result in a value of type real.
• The unary negation operators -, which results in a value of type real.
• The relational operators =, <>, <, <=, > and >=, which result in a value of type
boolean.
9
5 Expressions
An expression is a finite combination of operands and operators. An operand of an expres-
sion can be a literal, an identifier, an element of an array or a function call.
10
5.4 Invocation Expression
An invocation expression is a function call which starts with an identifier followed by “(“
and “)”. A nullable comma-separated list of expressions might be appeared between “(“ and
“)” as a list of arguments.
Like C, all arguments (including arrays) in MP are passed "by value." The called func-
tion is given its value in its parameters. Thus, the called function cannot alter the variable
in the calling function in any way.
When a function is invoked, each of its parameters is initialized with the corresponding
argument’s value passed from the caller function.
When an array variable is passed (as an argument) to/from a function/procedure, the
lower bound, upper bound and the element type of the array argument and the array
formal parameter must be the same. All members of the array argument will be copied to
the corresponding members of the array formal parameter.
The type coercion rules for assignment are applied to parameter passing where LHS’s
are formal parameters and RHS’s are arguments.
For example,
procedure f o o ( a : array [ 1 . . 2 ] of r e a l ) . . .
procedure goo ( x : array [ 1 . . 2 ] of r e a l ) ;
var
y : array [ 2 . . 3 ] of real ;
z : array [ 1 . . 2 ] of integer ;
begin
f o o ( x ) ; //CORRECT
f o o ( y ) ; //WRONG
f o o ( z ) ; //WRONG
end
The type coercion rules and the exception in parameter passing are also applied to return
type where LHS is the return type and RHS is the expression in the return statement.
For example,
11
var
a : array [2 . . 3] of r e a l ;
begin
i f ( ) then return a ; //CORRECT
e l s e return b ; //WRONG
end
12
else
<statement2>
where <expression>, which must be of the type boolean, is first evaluated. If it is true,
<statement1> is executed. The <statement2> is otherwise executed.
The if-no else is like if-else but there is no else and <statement2>. In this type of if
statement, if the <expression> is false, the next statement will be executed.
Like C, C++ and Java, the MP language suffers from the so-called dangling-else prob-
lem. MP solve this by decreeing that an else must belong to the innermost if.
When while statement is executed, the <expression> is evaluated first. While its value
is true, the <statement> is executed. The loop stops when the <expression> is false.
where <expression1 > is executed first and its value is assigned to <identifier>. If the
keyword to is used and the value of the <identifier> is less than or equal to the value
of <expression2 >, the <statement> is executed and then the value of <identifier> is in-
creased by one. The comparion between <identifier> and <expression2 >, the execution of
<statement>, and the increase of <identifier> are repeated until the result of the compari-
son becomes false. If the keyword <downto> is used, the process happened the same except
that the relational operator is greater than or equal to and the increase of <identfier> is
replaced by the decrease.
The <identifie> must be a local integer variable.
13
follows:
continue ’;’
7 Built-in Functions
MP has some following built-in functions:
function getInt():integer : reads and returns an integer value from the standard input
procedure putInt(i:integer): prints the value of the integer i to the standard output
procedure putIntLn(i:integer): same as putInt except that it also prints a newline
function getFloat():real : reads and returns a floating-point value from the standard input
procedure putFloat(f:real): prints the value of the real f to the standard output
procedure putFloatLn(f:real): same as putFloat except that it also prints a newline
procedure putBool(b:boolean): prints the value of the boolean b to the standard output
procedure putBoolLn(b:boolean): same as putBoolLn except that it also prints a new line
14
procedure putString(s:string): prints the value of the string to the standard output
procedure putStringLn(s:string): same as putStringLn except that it also prints a new line
procedure putLn(): prints a newline to the standard output
8 Scope Rules
Scope rules govern declarations (defining occurrences of identifiers) and their uses (i.e.,
applied occurrences of identifiers).
The scope of a declaration is the region of the program over which the declaration can
be referred to. A declaration is said to be in scope at a point in the program if its scope
includes that point.
There are three levels of scope: global, function/procedure, and block.
A global scope is the whole program which is applied to all function/procedure dec-
larations and variable declarations outside function/procedure declarations. All built-in
function/procedures are applied to this scope
A function/procedure scope is the entire corresponding function/procedure which is
applied to its parameters and variable declarations just after the parameter part.
A block scope is the statement inside the with statement. The scope is applied to the
declarations between the keywords with and do.
There are four additional rules on the scope restrictions:
2. All declarations in local scope are effective from the place of the declaration to the
end of its scope.
3. No identifier can be defined more than once in the same scope. This implies that no
identifier represents both a global variable and a function name simultaneously.
4. Most closed nested rule: For every applied occurrence of an identifier in a block, there
must be a corresponding declaration, which is in the smallest enclosing block that
contains any declaration of that identifier.
1 var i : integer ;
2 function f ( ) : integer ;
3 begin
4 return 200;
5 end
6 procedure main ( ) ;
15
7 var
8 main : integer ;
9 begin
10 main := f ( ) ;
11 putIntLn ( main ) ;
12 with
13 i : integer ;
14 main : integer ;
15 f : integer ;
16 do begin
17 main := f := i := 1 0 0 ;
18 putIntLn ( i ) ;
19 putIntLn ( main ) ;
20 putIntLn ( f ) ;
21 end
22 putIntLn ( main ) ;
23 end
24 var g : r e a l ;
The above program will be compiled and print the following results:
200
100
100
100
200
In this program, there are three scope levels:
Declaration Level Scope
putIntLn (built-in) 1 Entire program
i (line 1) 1 Entire program
f (line 2) 1 Entire program
main (line 6) 1 Entire program
main (line 8) 2 line 9-12 and line 22-23
i (line 13) 3 line 13-21
main (line 14) 3 line 13-21
f (line 15) 3 line 13-21
g (line 24) 1 Entire program
Note that the variable g declared in line 24 has the global scope although it is declared
at the end of the program.
The variable main declared in line 8 is said to hide the procedure declaration main in
line 6. The variable main declared in line 14 hides the variable declaration main in line
8. The variable i declared in line 13 hides the global variable i in line 1. The variable f
16
declared in line 15 hides the function declaration f in line 2.
The scopes of the declarations f in line 2, i in line 1 and main in line 6 are not contiguous.
Such gaps are known as scope holes, where the corresponding declarations are hidden or
invisible. As a matter of style, it is advised not to introduce variables that conceal names
in an outer scope. This is the major reason why Java disallows a variable declaration from
hiding another variable declaration of the same name in an outer scope. Therefore, the MP
program above is a bad programming style.
10 Change Log
• Different from version 1.1
17