Monday, April 04, 2005

The JLS2 Grammar

Introduction

The JLS2 grammar is "borrowed" from The Java Language Specification, Second Edition (JLS2 2000).

  • The JLS2 grammar (JLS2 2000) is presented piecemeal from chapter 3 to chapter 17 in the JLS.
  • The JLS2 grammar is written in ISO/IEC 14977:1996 EBNF notation.
  • The JLS2 grammar is independent of font color and type. It is possible to try out rules by substituting with copy and paste.
  • The JLS2 grammar should be a useful tool when reading for the SCJP exam.

Why another Java grammar?

I wanted the whole grammar in one HTML page, making it easier to find a rule.

I wanted all nonterminal symbols to be clickable, making the browser automatically search for the correct rule.

I also wanted an index of all terminal symbols and nonterminal symbols, with clickable references to all rules that the symbol occur within. If you click a rule name, the browser will even take you to the index of that rule.

I wanted a grammar with color coding. Terminal symbols are shown in blue, plain type. Nonterminal symbols are shown in black, italic type. Parenthesis are shown in red, italic type. Operators are shown in purple, italic type.

Errata

  • "ISO/IEC 14977 : 1996(E)" states that - except-symbol (Java: "but not") should have higher precedence than | definition-separator-symbol (Java: a separate line), but this is clearly not the case in the rules NotStar, NotStarNotSlash, and Identifier, perhaps because of readability. i.e. the rule "Identifier: IdentifierChars but not Keyword or BooleanLiteral or NullLiteral" should be written "Identifier: IdentifierChars but not (Keyword or BooleanLiteral or NullLiteral)"
  • Rules with nonmatching italic parenthesis or terminals written in italic have been corrected. Examples are the rules "IdentifierSuffix", "MoreStatementExpressions", "TypeDeclaration".
  • The "Expression" rule contains one too many ']'.
  • The "Expression3" rule references the "Expr" rule that does not exist. The reference is replaced by the "Expression" rule because of similar use in the "Primary" rule.
  • The "Statement" rule references the "ForInitOpt" rule that does not exist. The reference is replaced by "[ForInit]".
  • The "Statement" rule references the "ForUpdateOpt" rule that does not exist. The reference is replaced by "[ForUpdate]".
  • The "Statement" rule references the "ExpressionStatement" rule that does not exist. The reference is replaced by "StatementExpression".
  • The terminals ".class" and "void.class" is replaced by ". class" and "void . class", the terminal "default:" is replaced by "default :", and the terminal "[]" is replaced by "[ ]" This is done for clarity. The characters '.', ':', '[' and ']' are separators that generally allows white space.

References

  • JLS2. (2000). The Java Language Specification, Second Edition. California: Sun Microsystems, Inc. Retrieved Monday, Monday, April 04, 2005 from http://java.sun.com/docs/books/jls/.

No comments: