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 | class)* | 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 | name) [ [ ] ] | Integer, boolean, class-type, integer array, boolean array, or class-type array |
parameters | -> | type name (, type name )* | Parameters are separated by commas |
variableDeclaration | -> | type name ( , name )* ; | |
class | -> | class name
[ ( name ) ] { fieldDeclaration* } { constructor* } { method* } |
Single-inheritance; field declarations come before constructors which come before methods |
fieldDeclaration | -> | modifier variableDeclaration | |
constructor | -> | modifier name ( [ parameters ] ) functionBody | Constructor name must match the class name |
method | -> | modifier function | |
modifier | -> | public | private | |
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 | . name ] | Can assign to a variable or an array element or a field |
Expressions | |||
expression | -> | expPart [ binaryOp expPart ] | Single operator (no precedence necessary) |
expPart | -> | unaryOp expPart | |
expPart | -> | literal | |
expPart | -> | ( expression ) | Parenthesized expression |
expPart | -> | name [ functionArgs | subscript | attributeRef ] | Variable, function, subscripted variable, or object use (field or method-call) |
functionArgs | -> | ( [ expressionList ] ) | Function arguments |
expressionList | -> | expression ( , expression )* | |
subscript | -> | [ expression ] | Array subscript |
attributeRef | -> | . name [ functionArgs ] | Field or method-call |
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 |
|
class |
|
fieldDeclaration |
|
modifier |
|
constructor |
|
method |
|
this |
|
super |
|
statement |
|
expression |
|
arrays |
|
predefined functions |
|