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

Language Semantics

+

The + operator can be applied on Integer and String operands. Unless being applied on two integers (in which case the result is an Int), the result is the concatenation of the string representations.

"foo" + 3 => "foo3"
3 + "foo" => "3foo"
3 + 3     => 6

==

Equality works on all pairs of operands of the same type (Int, String, Boolean, Array, Objects).

The semantics of equality is

  • value-equality for ints and booleans
  • reference-equality for strings, objects and arrays
42 == 42                  => true
new A() == new A()        => false
new A() == new B()        => false
"foo" == 42               => // Type Error...
"f" + "oo" == "fo" + "o"  => false

println

println can be used on Integers, Strings and Booleans.

&&

The && (boolean AND) operator is short-circuitting!

def printFoo(): Boolean = {
  println("foo");
  return false;
}

def printBar(): Boolean = {
  println("bar");
  return true;
}
printFoo() && printBar() // "foo"

||

The || (boolean OR) operator is short-circuitting!

def printFoo(): Boolean = {
  println("foo");
  return false;
}

def printBar(): Boolean = {
  println("bar");
  return true;
}
printBar() || printFoo() // "bar"

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.
  • 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: