LARA

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. See also the Tool Compiler Project page.

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

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 for instance 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, they have no meaning in Tool. 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: