home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
syntax-week3
Java Syntax
week 3
Our Mantras
-
To call a function:
“object - dot - functionName - openParen - arguments”.
(Note that when calling a method from inside another method,
the object you are asking the question
is often yourself: “this”.)
-
To declare a function (its signature):
“returnType - functionName - openParen - parameterTypeAndNames”.
Java Syntax
Week 3's syntax includes all the syntax everything we covered in
week 2,
plus
boolean,
if-else,
if-else-if.
-
class-declaration ::=
class nameClass {
method-declaration…
}
-
type ::= int | double | boolean
| String
-
method-declaration ::=
typereturn name ( parameter-declaration-list ) {
local-variable...;
return expression;
}
-
parameter-declaration-list
::=
empty-list |
non-empty-parameter-declaration-list
-
non-empty-parameter-declaration-list
::=
parameter-declaration [, non-empty-parameter-declaration-list]
-
parameter-declaration ::= type nameparam
-
local-variable ::=
local-variable-declaration
local-variable-initialization
-
local-variable-declaration ::= type name;
-
local-variable-initialization ::= name = expression;
-
statement ::=
return-statement |
local-variable-declaration |
local-variable-initialization |
if_else-statement |
if_else_if-statement |
-
if_else-statement ::=
if (expressionboolean) {
statement...
}
else {
statement...
}
-
if_else_if-statement ::=
if_else-statement
|
if (expressionboolean) {
statement...
}
else if_else_if-statement
-
return-statement ::= return expression;
-
expression ::=
literal-value | namevariable | method-call | ( expression ) |
expression+expression | expression-expression | expression*expression | expression/expression
expressionboolean
&&
expressionboolean
|
expressionboolean
||
expressionboolean
|
!expressionboolean
-
method-call ::= expressionobject . namemethod ( argument-list )
-
argument-list ::= empty-list | non-empty-argument-list
-
empty-list ::=
-
non-empty-argument-list ::= expression [, non-empty-argument-list]
How to read Backus-Naur Form (BNF)
- colored text is replaced with
a particular instance of that type.
(Subscripts are informative only, and don't technically mean anything.)
- uncolored text is typed in literally
(not replaced with something else).
- “::=” means “consists of”.
- “|” means “or”
- “[bracket-optional-part]”
can be included, or be omitted.
- “...” means zero or more
repetitions of the preceding item.
caveats
-
This grammar is not for Java, but for the Java I teach in class.
Not only does it simplify full Java, but it also intentionally
precludes (for a while) if-without-else, block-statements,
declaring a variable without initializing it immediately afterwards, etc..
-
The above grammar is not meant to be minimal,
but rather understandable.
For instance, if-else
could be omitted (it's a special case of
if-else-if),
and
yet I include both (and I don't factor them agressively).
See also:
Lies
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs