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:
- They are hard to produce. The only software I've seen to produce general images (Bungisoft Diagram Visualizer) seems to no longer be available. Perhaps this isn't surprising given the poor quality and high price. I mentioned this market opportunity to my wife and she noted that there might be 17 people who would be interested in this sort of tool. Well, it would make a good programming project for someone.
- An audience that you might think this would appeal to are compiler writers, but that isn't so. The compiler writers want a text file that can be read by parser generators, which means something closer to EBNF.
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 | | | +-->---+ | ")"