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:
Functions, Classes, and Methods | |||
program | -> | function* | There must be a main function (see below) |
function | -> | functionHeader functionBody | |
functionHeader | -> | ( type | void ) name ( [ parameters ] ) | |
functionBody | -> | { variableDeclaration* } { statement* } |
Variable declarations come before statements; both are surrounded by braces |
type | -> | ( int | boolean) [ [ ] ] | Integer, boolean, integer array, or boolean array |
parameters | -> | type name (, type name )* | Parameters are separated by commas |
variableDeclaration | -> | type name ( , name )* ; | |
Statements | |||
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 |
statement | -> | target = expression ; | |
target | -> | name [ subscript ] | Can assign to a variable or an array element |
Expressions | |||
expression | -> | expPart [ binaryOp expPart ] | Single operator (no precedence necessary) |
expPart | -> | unaryOp expPart | |
expPart | -> | literal | |
expPart | -> | ( expression ) | Parenthesized expression |
expPart | -> | name [ functionArgs | subscript ] | Variable, function, or subscripted variable |
functionArgs | -> | ( [ expressionList ] ) | Function arguments |
expressionList | -> | expression ( , expression )* | |
subscript | -> | [ expression ] | Array subscript |
literal | -> | integer | true | false | null | The various kinds of literals |
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 | |
Comments |
|||
|
program |
|
mainFunction |
|
function |
|
parameters |
|
variableDeclaration |
|
statement |
|
expression |
|
arrays |
|
predefined functions |
|