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 | float | char | boolean | string | name ) ( [ ] )* | |
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 | |
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 ] | |
Expressions | |||
expression | -> | expPart [ binaryOp expPart ] | Single operator (no precedence necessary) |
expression | -> | [ [ expressionList ] ] | An entire array |
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 | character | float
| string | 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 | |
character | -> | Single quotes around any printing ASCII character (e.g., 'a', '$', ' '); there is one special character: '\n' for newline |
|
float | -> | Numbers with a single decimal point and no exponential part (e.g., 1.0, .05, 6.) |
|
string | -> | Double quotes around any sequence of characters (that does not contain double quotes); there is one special character: "\n" for newline |
|
Comments |
|||
|
program |
|
mainFunction |
|
function |
|
parameters |
|
variableDeclaration |
|
class |
|
fieldDeclaration |
|
modifier |
|
constructor |
|
method |
|
this |
|
super |
|
statement |
|
expression |
|
arrays |
|
predefined functions |
|