Bali Specifications for Part 2

CS 212 - Fall 2007

This is a simplified Bali specification for the Part 2 assignment.  Bali is designed to be reasonably simple to compile.  The Bali language changes every semester.

We use the following notation throughout this document:

Bali Syntax

program int main ( ) : [ declarations ] : statement* end A program consists of  a single function.  This function must (1) have return type int (this returned value remains on the stack when the program halts), (2) be called main, and (3) have no parameters.  The function body consists of optional declarations (local variables) followed by zero or more statements.
declarations type name ( , type name )* Each declaration is a type followed by a name.
type ( int | boolean) There are predefined types (int, boolean)
statement reference = expression ; An assignment statement.  
  if expression then statement* [ else statement*] endif An if-statement has an optional else part.
  loop statement* ( while | until ) expression ; statement* endloop Looping.  Note that there are two blocks of statements.  The loop works by executing the first block, then checking the condition and possibly exiting the loop, then executing the second block.  If the condition causes a loop-exit then the second block is not executed.  After executing the second block the loop starts over again with the first block.  Either statement-block in a loop can be empty or both blocks be nonempty.  (It's also legal for both blocks to be empty, but then it's not clear that the loop would be useful.)
  return expression ; Return statement.
  print expression ( , expression )* ; Print statement.  Multiple expressions can be printed.  Ideally, all are printed on a single line with a space between adjacent items, but this isn't currently possible with SaM-code.
reference name  A reference to a local variable.  
expression [ + | - | not ] term ( binaryOp term )* An expression is a a sequence of terms separated by binary operators.  There can be an optional sign or boolean-negation in front of the first term of an expression; it is applied to the first term.  Operators are evaluated left-to-right (i.e., no precedence rules).
binaryOp arithmeticOp | comparisonOp | booleanOp The three kinds of binary operators.
arithmeticOp + | - | * | / | %  Operators for arithmetic.
comparisonOp < | <= | == | != | > | >= Comparison operators.
booleanOp and | or Boolean  operators (both are short-circuiting).
term literal | ( expression ) | inputValue | reference A term is a literal, a parenthesized expression, an input value, or a reference.
literal integer | true | false Unsigned integers (e.g., 123, 6, 44) and boolean constants.
inputValue readInt This "value" is read from standard input; readInt expects an integer (initial blanks are ignored, an initial + or - is OK).


Bali Semantics

  • Keywords (those shown in bold-blue above) cannot be used as names.
  • The complete list of keywords:
    • end, void
    • class, extends, endclass, this, super, null
    • if, then, else, endif
    • loop, while, until, endloop
    • return, print
    • and, or, not, true, false
  • The following words have special meaning, but they are not keywords (i.e., they are defined in the global namespace---see below):
    • int, boolean, char, float
    • readInt, readChar, readLine, readFloat


  • Running  a program means the program's main function is executed.
main function
  • The return type for the main function must be int; this integer value appears as the exit code when the SaM Simulator halts.
  • Variables have default values : 0 for int and false for boolean.
  • Variables must be declared before use.
  • Expressions in if and loop statements must be of type boolean.
  • For assignment statements, the types of the left-hand and right-hand sides must match.
  • Rudimentary output is available via the print statement.
  • For all binary operators, the operands must be of the same type.
  • For logical operators (and, or, not), operands must be of type boolean.
  • For arithmetic operators (+, -, *, /), operands must be of type int.  Division (/) is integer division.
  • For the mod operator (%), operands must be of type int.
  • For the relational operators (<, <=, >, >=), operands must be of type int.
  • The equality (==) and inequality (!=) operators both work either with both operands int or both operands boolean.
  • Bali does not support division by zero, NaN, or infinity.
  • Rudimentary input is available using the predefined "variable" readInt.
  • It is a semantic error to readInt on the left side of an assignment statement.
predefined names
  • The names int and boolean are predefined global variables.  You can have local variables that use these names if you want; it's legal even though it's bad programming style.
  • The name readInt is also a predefined global variable.