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:
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 |
|||
|
mainFunction |
|
variableDeclaration |
|
statement |
|
expression |
|