Bali Specifications for Part 2

CS 212 - Spring 2004

This document contains the Bali specifications for Part 2 of the CS 212 project. Many parts of Bali have been left out.  If you're curious, the document Bali Specs contains the full specification, but be aware that this specification may change somewhat during the semester.

As discussed in lecture, we can specify a language in terms of its syntax (structural rules) and semantics (meaning). Although formal description methods exist for both kinds of specifications, we only formalize the Bali syntax. Bali's semantics are informally defined using English-language descriptions.

We use the following notation throughout this document:

Bali Syntax

Main Function
program  ->   mainFunction Only a main function
mainFunction  -> int main (  ) functionBody
functionBody -> { variableDeclaration*
{
statement* }
Variable declarations
come before statements;
both are surrounded by
braces
type -> int | boolean
variableDeclaration -> type name ( , name )* ;

Statements
statement -> name = expression ;
statement ->   return [ expression ] ;
statement -> { statement* }
statement -> if expression then statement 
[ else statement ]
statement -> while expression do statement
statement -> do statement while expression ;
statement -> expression ;
statement -> print expression ; Output
statement ->  ; Empty statement

Expressions
expression ->   expPart [ binaryOp expPart ] Single operator (no 
precedence necessary)
expPart -> integer | true | false The various constants
expPart -> readInt ( ) Input
expPart -> name Variable
expPart -> ( expression )
expPart -> unaryOp expPart
binaryOp -> arithmeticOp | comparisionOp | booleanOp
arithmeticOp -> + | - | * /  | % % is mod (as in Java)
comparisonOp -> < | > | <= | >= | == | !=
booleanOp -> && |  | |  | ^ And (short circuiting),
or (short circuiting),
and xor
unaryOp ->  - | ! Unary minus, not

Tokens
name -> ( a-z | A-Z ) ( a-z | A-Z | _ | 0-9 )*
Names are case sensitive.
integer -> Legal Java integers
character -> Single quotes around any printing 
ASCII character (e.g., 'a', '$', ' '); 
there is one special character: 
'\n' for newline 
Not used for Part 1
float -> Numbers with a single decimal point
and no exponential part (e.g., 1.0, .05, 6.)
Not used for Part 1

Comments
  • Double slash // indicates that the remainder of the current line is a comment.
  • There are no multi-line comments.

Bali Semantics

mainFunction
  • The main function must return a single integer; this value appears as the exit code when the SaM Simulator halts.
  • If you "fall off the end" of the main function (i.e., you execute the last statement and that statement is not a return statement) then a default return (which returns zero) is automatically executed.
variableDeclaration
  • Variables have default values : 0 for int, false for boolean, ' ' (space character) for char, and 0.0 for float.
  • Variables must be declared before use.
statement
  • Expressions in if, while, and do-while statements must be of type boolean.
  • For assignment statements, the types of the left-hand and right-hand sides must match.
  • The syntax allows the following problematic construct.
    "if expression then if expression then statement else statement"
    The else should be matched with the closest if.  This rule could have been incorporated into the syntax, but it would have made the syntax more complicated.
  • Rudimentary output is available via the print statement.
expression
  • For all operators, the operands must be of the same type.
    • For logical operators (&&, | |, ^, !), 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 with all types of values.
    • Bali does not support division by zero, NaN, or infinity.
  • Rudimentary input is available via the read function readInt( ). It returns a value of type int.