Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java grammar definition completeness

Tags:

java

grammar

jls

Whereas the Java grammar seems very precisely described in JLS specifications, there are some concrete cases which I fail to apply on given definitions.

For example, taking the ClassInstanceCreationExpression rule in chapter 15.9 of JLS8, non-qualified new expressions should be of the form:

new [TypeArguments] {Annotation} Identifier [TypeArgumentsOrDiamond] ( [ArgumentList] ) [ClassBody] 

Identifier being a standard Java identifier (basically Java letters / numbers, no dot).

How does this definition would apply to valid expressions like static nested classes instanciation:

new C1.C2();

or package-qualified classes instanciation:

new java.lang.String("foo");

given that dots cannot be part of an Identifier?

Note that there was a change on this definition from JLS7 to JLS8, where JLS7 was stating, for non-qualified new expressions:

new [TypeArguments] TypeDeclSpecifier [TypeArgumentsOrDiamond]( [ArgumentList] ) [ClassBody]

TypeDeclSpecifier being defined as:

TypeDeclSpecifier:
    TypeName
    ClassOrInterfaceType . Identifier 

allowing non-qualified new expressions for static nested classes and package-qualified classes.

like image 1000
lledr Avatar asked Oct 11 '14 10:10

lledr


1 Answers

It seems to be a bug in the specification.

Quoting from the bug report linked to above (third point in the description):

  1. The grammar doesn't define any production for the following expression: new java.security.Permissions()

This is a side-effect of removing TypeDeclSpecifier in 4.3, as it interacted poorly with type annotations. The JSR 308 Public Review noted: "TypeDeclSpecifier is one of the more obscure nonterminals in the The Java Language Specification. It is used in only a handful of situations: the extends and implements clauses of a class declaration (8.1.4, 8.1.5), the extends clause of an interface declaration (9.1.3), and the syntax of a class instance creation expression (15.9). The reason for its use is to prohibit wildcard type arguments (though 9.1.3 actually fails to do so), but this can be achieved equally well without a dedicated non-terminal."

like image 58
rici Avatar answered Sep 28 '22 06:09

rici