Tool Resource Page
Tool stands for Toy Object-Oriented Language, and it is the programming language for which you will write a compiler in this course.
BNF
Goal | ::= | MainObject ( ClassDeclaration )* <EOF> |
MainObject | ::= | object Identifier { def main ( ) : Unit = { Statement } } |
ClassDeclaration | ::= | class Identifier ( extends Identifier )? { ( VarDeclaration )* ( MethodDeclaration )* } |
VarDeclaration | ::= | var Identifier : Type ; |
MethodDeclaration | ::= | def Identifier ( ( Identifier : Type ( , Identifier : Type )* )? ) : Type = { ( VarDeclaration )* ( Statement )* return Expression ; } |
Type | ::= | Int [ ] |
Bool | ||
Int | ||
String | ||
Identifier | ||
Statement | ::= | { ( Statement )* } |
if ( Expression ) Statement ( else Statement )? | ||
while ( Expression ) Statement | ||
println ( Expression ) ; | ||
Identifier = Expression ; | ||
Identifier [ Expression ] = Expression ; | ||
Expression | ::= | Expression ( && | || | == | < | + | - | * | / ) Expression |
Expression [ Expression ] | ||
Expression . length | ||
Expression . Identifier ( ( Expression ( , Expression )* )? ) | ||
<INTEGER_LITERAL> | ||
" <STRING_LITERAL> " | ||
true | ||
false | ||
Identifier | ||
this | ||
new Int [ Expression ] | ||
new Identifier ( ) | ||
! Expression | ||
( Expression ) | ||
Identifier | ::= | <IDENTIFIER> |
- <IDENTIFIER> represents a sequence of letters, digits and underscores, starting with a letter and which is not a keyword. Identifiers are case-sensitive.
- <INTEGER_LITERAL> represents a sequence of digits, with no leading zeros
- <STRING_LITERAL> represents a sequence of arbitrary characters, except new lines and ". We don't ask you to support escape characters such as \n.
- <EOF> represents the special end-of-file character
Type System
The typing rules are presented on this separate page.
Miscellaneous
- Comments in Tool can be marked using the end-of-line notation (//) or blocks ( /* … */ ). Nested blocks are not allowed.
- Inheritance works as in Java, except that we don't have the notions of interfaces, abstract members or abstract classes.
- No form of overloading is permitted.
- Note that the else branch of the if construct is optional.
- The names of the classes must be distinct from the name of the program object.
Some Clarifications
The answer to (most of) these questions is in the grammar, but to avoid confusion, here are some details that may seem curious:
- Yes, if the
main
method has more than one statement, they must be in a block and thus will be surrounded by two pairs of curly braces. - No, you cannot define extra variables in the
main
method. - No, you cannot use non-default constructors (constructors that take parameters).
- Method overloading is not allowed, but method overriding is. If you are unclear about the difference between the two, see this page. The only overloading in Tool is on the
+
operator, which can be used with integers, strings, and between the two types. - Only constant strings (strings given as literals) are allowed, and although the grammar specifies that expressions such as
“my string”.method()
are legal, we exclude them. The only operations allowed are: concatenating strings with other strings or with integers, printing strings, passing strings as argument and returning strings.
Example Programs
Here are some demonstration programs:
- Factorial.tool - Computes factorials
- BinarySeach.tool - Binary search (dichotomy) in an array
- QuickSort.tool - Implementation of quick sort in Tool
- Pi.tool - Computes an approximation of pi in two different ways
- Maze.tool - Generates and prints random mazes (labyrinths)