Monday, March 28, 2005

The JLS3 Grammar


You may browse the JLS3 grammar at


The grammar follows The Java Language Specification, Third Edition as closely as possible. The reasons for "rewriting" the grammar are discussed here.

The JLS3 (2005) itself is probably worth a re-read if you're programming with Java 1.5.0 (White 2005). Actually, I would say that it's a must if you don't want to end up as "Tiger" food! A substantial part of the Java grammar has been affected by the proposed changes to the JLS.

The proposed changes include:

(JLS3P 2005; J2SE5 2004)


Browsing through the grammar (JLS3P 2005), I found the following problems:

Grammar Changes for JLS3 (2005)

The following changes were made for the pre-publication PDF release.

  • Many duplicates of rules are removed, for instance ArgumentList, EnumConstant, InterfaceType, Literal, OctalDigit, TypeName. Only the first instance of a rule is kept.
  • TypeName was changed to TypeDeclSpecifier in rule InterfaceType
  • 2 rules for TypeName was made into one
  • The rule TypeDeclSpecifier was created
  • The rule ClassType was fixed. TypeName replaced by TypeDeclSpecifier
  • The rule TypeParameters was replaced by TypeParameter. The rule TypeParameter is now called from the rule TypeParameters
  • The rules TypeParameters and TypeParameterList was found in 8.1.2 Generic Classes and Type Parameters
  • SimpleTypeName replaced Identifier in the rule ConstructorDeclarator. The nonterminal SimpleTypeName in rule ConstructorDeclarator is still not defined (JLS3 2005), (I renamed SimpleTypeName to Identifier, similar to the rule NormalClassDeclaration.)
  • EnumConstant was replaced by EnumConstantName in rule SwitchLabel
  • Added the rule EnumConstantName
  • All NonWildTypeArguments replaced with TypeArguments in rule ClassInstanceCreationExpression
  • TypeName replaced by ClassOrInterfaceType in rule ArrayCreationExpression

Grammar Errors

  • SimpleTypeName in ConstructorDeclarator is not defined (JLS3P 2005:245). SimpleTypeName is not defined in JLS2 either. (I renamed SimpleTypeName to Identifier, similar to the rule NormalClassDeclaration.)
  • Expression1 and Expression2 in AssertStatement is not defined (JLS3P 2005:369). (I renamed Expression1 and Expression2 to Expression).
  • TypeParameter (JLS3P 2005:65) is never called by any rules and the rule TypeParameters is missing. (I renamed the rule TypeParameter: to TypeParameters:) The rule TypeParameter is now called from the rule TypeParameterList.
  • NonWildTypeArguments should probably be optional for MethodInvocation: TypeName . NonWildTypeArguments Identifier (ArgumentList opt) (JLS3P 2005:435), especially since NonWildTypeArguments is optional everywhere else. (I made NonWildTypeArguments optional.)

Formatting Errors

  • There is no change in the rule VariableDeclaratorId: since JLS2 as indicated (JLS3P 2005:201).
  • The rule Keyword: is not sorted for the keyword goto (JLS3P 2005:37). (I sorted it.)
  • The formatting in 9.6 Annotation Types is not correct because everything is printed in italic (JLS3P 2005:271).

Grammar Clarifications

  • Input and CompilationUnit are goal symbols. It's OK that no rules are calling them.
  • MethodDeclarator is divided into two rules with the same name. This is confusing, but not an error. The division is an attempt to visualize that one of the rules (MethodDeclarator: MethodDeclarator [ ]) is deprecated, but still compatible. (I rewrote the two rules into one).
  • Many rules are identical. This is confusing, but no error. (For instance the rule TypeName is identical to the rule PackageOrTypeName.)
  • MarkerAnnotation in Annotation is defined, but well hidden on page 285.
  • SingleElementAnnotation in Annotation is defined, but well hidden on page 285.
  • CastExpression (JLS3P 2005:476) is clearly missing Dims opt (15.16 Cast Expressions) (I added Dims opt to the rule.)