Java Basics

Syntax notation

There are several ways to describe syntax that have been very popular. It's essential for compiler writers to have a very accurate description of the syntax of a programming language, and there are a number of tools for reading a syntax description and turning it into executable parsers.

Following are three ways to show the syntax of a method header.

EBNF

Here are the syntax rules for EBNF.

    Non-terminals Written as italicized text.
    Terminals     Written in quotes, as in "private".
    |             The left and right sides are alternatives.
    (  )          Parentheses are used for grouping.
    [  ]          Brackets enclose optional material.
    {  }          Braces enclose material to be repeated 0 or more times.
    =             Defines symbol on left by notation on the right.
    .             Ends each definition.
    spaces        Used only for readability.

Definition of a method header

methodHeader =[visibility] ["static"] returnType methodName "(" [parameterList] ")" .
visibility ="public" | "private" | "protected" .
parameterList=parameterDeclaration {"," parameterList} .
parameterDeclaration=type ParameterName .

Railroad diagrams

An attractive, readable, syntax notation can be found in railroad/railway diagrams. Just follow the lines. The flow is left to right or top-to-bottom. Traditionally they go primarily left-to-right because that's how programming statments are written, but this results in very wide diagrams, requiring lines to snake to the beginning of the next line or some connection notation.

Advantage. You'll note in the crude ASCII railroad diagrams below that there are not as many non-terminal symbols as in EBNF.

Problems. There are two big problems with railroad digrams:

Horizontal ASCII railroad diagram


    +->- "public"  ---+     +->- "static" ->-+                                     +---<-------- "," --------<---+
    |                 |     |                |                                     |                             |
->--+->---------------+-->--+->--------------+-->- type ->- methodName ->- "(" ->--+-> type ->- parameterName ---+->- ")" 
    |                 |
    +->- "private" ---+
    |                 |
    +->- protected ---+

Vertical ASCII railroad diagram

       |
       |
       +---->---+----------+-----------+
       |        |          |           |
       V     "public"  "private"  "protected"
       |        |          |           |
       +----<---+----------+-----------+
       |
       +---->---+
       |        |
       |     "static"
       |        |
       +----<---+
       |
     type
       |
  methodName
       |
      "("
       |
       +--<---+
       |      |
     type     |
       |     ","
parameterName |
       |      |
       +-->---+
       |
      ")"