C212 Syntax

Selected Java syntax rules follow. 

Notation:

These rules that follow do not express whitespace (spaces, newlines, tabs, and comments), which is all the same as far as the language is concerned..

Program

  1.  program  =>  source_file { source_file }
    A program consists of one or more source files. (Officially, Java calls them "compilation units", but in practice they are files.)

  2. source_file  =>   { import_declaration } class_or_interface_declaration { class_or_interface_declaration }
    A source_file consists of zero or more import declarations followed by one or more class or interface declarations. If there are more than one in a file, only the first may be public and if there are any compilation errors with multiple classes file the messages can be very confusing.

  3. import_declaration  =>   import package_name . { class_ nameinterface_name }
    An import declarationconsists of the keyword import followed by a package name (such as objectdraw or java.awt), and then a period followed by a either an asterisk (star), a class name, or an interface name .

  4. class_or_interface_declaration  =>   class_declaration
    For now we don't need to know about interface declarations.

    Class

  5. class_declaration  =>  [ public ] class  class_name [ extends FrameWindowController ] class_body
    For now we only need to extend FrameWindowController, and then only for the main class of an objectdraw program.

  6. class_body  =>   { { class_member_declaration } }
    A class body consists of a left brace, followed by zero or more class member declarations, and finally a right brace.

  7. class_member_declaration  =>  method_declaration  |  constructor_declaration  |  field_declaration
    A class member declaration is either a method declaration , a constructor declaration, or a field declaration.

  8. field_declaration  =>  { modifier } type variable_name [ initializer ] ;

  9. initializer  =>  = expression

  10. method_declaration  =>  { modifier } return_type method_name parameter_list method_body

  11. modifier  =>  public  |  private  |  static  |  final

  12. return_type  =>  void type

  13. type  =>  class_or_interface_name primitive_type

  14. parameter_list  =>  ( [ parameter_declaration { parameter_declaration } ]  )
    A parameter list consists of a left parenthesis, followed by zero or more parameter_declarations , separated by commas, followed by a right parenthesis.

  15. parameter_declaration  =>  type  variable_name

  16. method_body  =>  {statement } }

  17. constructor_declaration  =>   [ public ] class_name parameter_list constructor_body

  18. constructor_body  =>  method_body


    Statement

  19. statement  =>  method_call  ;  assignment ;  |  new_expression ;  |  return [ expression ] ;  |  if_statement
  20. if_statement  =>  if ( expression ) statement [ else statement ]
    An else clause is associated with the nearest enclosing if statement.
  21. assignment  =>  variable_name = expression

    Expression

  22. expression  =>  literal  |  variable_name  |  method_call binary_operation prefix_operation  |  class_variable_reference  |  this

  23. method_call  =>  [ expression .class_name ] method_name argument_list

  24. argument_list  =>  ( [ expression { , expression } ] )
    An argument list is a list of zero or more expressions, separated by commas, and enclosed by parenthesis.

  25. binary_operation  =>  expression binary_operator expression

  26. prefix_operation  =>  prefix_operator expression

  27. class_variable_reference  =>  class_name . variable_name

  28. new_expression  =>  new class_name argument_list

    Tokens

  29. binary_operator  =>  +  |  -  |  *  |  /  |  %  |  <  |  <=  |  >  |  >=  |  ==  |  !=

  30. prefix_operator  =>  +  |  -

  31. primitive_type  =>  int  |  boolean  |  double  |  char  |  byte  |  short  |  long  |  float

  32. literal  =>  integer_literal string_literal

  33. string_literal  =>  "string_character  |  escape_sequence } "
    A string literal is any sequence of zero or more string characters or escape sequences (which will be introduced later), surrounded by double-quote characters.

  34. string_character  =>  any ASCII character other than a newline, backslash, or doublequote

  35. integer_literal  =>  digit { digit }

  36. name  =>  name_letter  |  { name_letter  |  digit }

  37. digit  =>  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8   |  9

  38. name_letter  =>  a  |  ... |  z  |  A  | ... |  Z  |  _  |  $
    A name letter is a lower or uppercase letter, or a dollar sign or underscore. Dollar signs should only be used in compiler-generated names.

  39. class_name, interface_name, method_name, and variable_name may be any name that is not a keyword , but the standard Java style convensions should be followed (such as starting class and interface names with upper-case letters and method and variable names with lower-case letters)

  40. keyword  => abstract | default | if | private | this | boolean | do | implements | protected | throw | break | double | import | public | throws | byte | else | instanceof | return | transient | case | extends | int | short | try | catch | final | interface | static | void | char | finally | long | strictfp | volatile | class | float | native | super | while | const | for | new | switch | continue | goto | package | synchronized