In this task you will write an interpreter for a simple programming language; this will involve writing a lexer, a parser (or their combination, if suitable), and doing a bit of semantic analysis. We will use the parser generator ANTLR as our main tool to reduce the amount of hand-written code required.
Please also carefully read the submission guidelines.
In our simple programming language,
the main function is a function int main(...), which may occur anywhere in the function declarations.
The return value of a function is the value of the final expression in its body.
All arguments to functions are passed by value, and the only identifiers defined in any function are as follows:
In other words, all functions are side-effect-free and of 'global scope', while parameters and local variables are of 'function scope'. There is one special case: when a local variable is being initialised, only the first two items above and local variables defined before it are available. We summarise the rules about arguments, scoping, and return values as follows:
fnA may involve the invocation other functions
that are defined before or after the function fnA;
fnA may have the same name;
fnA are only in-scope in the body of fnA;
int or bool types;
main function must return int.
The following are the rules which specify how expressions of various kinds are to be typed.
| Numerical expressions | |||
| Integer constants |
Γ ⊢ INTLIT : int
| ||
| Addition |
Γ ⊢ P : int
Γ ⊢ Q : int
Γ ⊢ (P + Q) : int
| ||
| Subtraction |
Γ ⊢ P : int
Γ ⊢ Q : int
Γ ⊢ (P - Q) : int
| ||
| Multiplication |
Γ ⊢ P : int
Γ ⊢ Q : int
Γ ⊢ (P * Q) : int
| ||
| Division |
Γ ⊢ P : int
Γ ⊢ Q : int
Γ ⊢ (P / Q) : int
| ||
These are expressions which take values of type int, in particular arising from arithmetic operations
on two values of type int.
Any expression involving an arithmetic operator as below, where one of the sub-expressions involved
is not of type int, should result in an error.
| Comparison expressions | |
| Equality |
Γ ⊢ P : int
Γ ⊢ Q : int
Γ ⊢ (P == Q) : bool
|
| Less than |
Γ ⊢ P : int
Γ ⊢ Q : int
Γ ⊢ (P < Q) : bool
|
| Greater than |
Γ ⊢ P : int
Γ ⊢ Q : int
Γ ⊢ (P > Q) : bool
|
| Less than or equal |
Γ ⊢ P : int
Γ ⊢ Q : int
Γ ⊢ (P <= Q) : bool
|
| Greater than or equal |
Γ ⊢ P : int
Γ ⊢ Q : int
Γ ⊢ (P >= Q) : bool
|
Comparison expressions are ones which relate two expressions of type int, to produce an expression of type bool.
Any expression involving a comparison operator as below, where one of the sub-expressions involved is not of type int, should result in an error.
| Boolean operators | |
| Boolean constants |
Γ ⊢ BOOLLIT : bool
|
| Logical AND |
Γ ⊢ P : bool
Γ ⊢ Q : bool
Γ ⊢ (P & Q) : bool
|
| Logical OR |
Γ ⊢ P : bool
Γ ⊢ Q : bool
Γ ⊢ (P | Q) : bool
|
| Logical XOR |
Γ ⊢ P : bool
Γ ⊢ Q : bool
Γ ⊢ (P ^ Q) : bool
|
These are expressions which combine two values of type bool using a logical operation, to produce another value of type bool.
Any expression involving a logical operator as below, where one of the sub-expressions involved is not of type bool, should result in an error.
| Statements | |||||
| Print statements |
Γ ⊢ E : int
Γ ⊢ print E : unit
E = space
Γ ⊢ print E : unit
E = newline
Γ ⊢ print E : unit
| ||||
| Space statements |
Γ ⊢ space : unit
| ||||
| Newline statements |
Γ ⊢ newline : unit
| ||||
| Skip statements |
Γ ⊢ skip : unit
| ||||
| Assignments |
Γ ⊢ x : α
Γ ⊢ P : α
Γ ⊢ x := P : unit
| ||||
| While loops |
Γ ⊢ C : bool
Γ ⊢ B : unit
Γ ⊢ while C do {B} : unit
| ||||
| Repeat loops |
Γ ⊢ B : unit
Γ ⊢ C : bool
Γ ⊢ repeat {B} until C : unit
| ||||
These are 'expressions' which represent statements — not having an interesting value
(represented by assigning them the type unit).
Any statement such as those below, which involve a mismatch of types where type-agreement is
required, or which involve an expression other than type bool where a Boolean expression is expected,
should result in an error. In particular, print must be followed by either space or
newline, or an expression of type int.
| Generic expressions | |||||
| Function invocations |
Γ ⊢ subr : (α1 , ... , αn) → β
Γ ⊢ E1 : α1
...
Γ ⊢ En : αn
| ||||
| Block expressions |
Γ ⊢ E1 : α1
...
Γ ⊢ En : αn
Γ ⊢ E1 ; ... ; En : αn
| ||||
| If statements |
Γ ⊢ C : bool
Γ ⊢ B1 : α
Γ ⊢ B2 : α
Γ ⊢ if C then {B1} else {B2} : α
| ||||
These are expressions, whose types depend entirely on the types of other identifiers or expressions. Any expression involving a mismatch of types should result in an error.
Your interpreter will read a string (the input program in our simple programming language) from System.in.
It also reads the command-line arguments, where each argument (as specified on the command line) must be either an INTLIT or a BOOLLIT.
The interpreter will conduct the following checks (you do not necessarily need to do the checks in the order below):
main function, which has return type int; main matches the types of the command-line arguments passed to the interpreter — we assume that this is always the case.
If any one of these conditions fail, your program will throw an instance of
a custom RuntimeException class that we provide.
The exception should be caught by the main method in your Java code,
which should then invoke the report method of the exception like below:
try {
...(run your type checker on the parse tree)...
} catch (TypeException ex) {
System.out.println(ex.report());
}
If the input program passes all these checks (i.e. it is syntactically and semantically valid), then your interpreter
can start simulating the program. The semantics of the language constructs should be self-explanatory; in particular,
your interpreter will print to System.out as requested by print statements in the input program,
followed by a line separator (System.out.println()), and then two separate lines: the first contains the string NORMAL_TERMINATION, and the second contains the return value of main.
Some input / output examples can be found in this file.
Ready for code generation? Please head to Task 2 for more challenges.