Here are a few fixes in the grammar specification for Java7 that I've introduced while developing c4.
Expression2Rest:
{ InfixOp Expression3 }
instanceof Type
This production doesn't take into account cases like
if (x == 1 && y instanceof String) { ... }
Modified version
bnf
Expression2Rest:
{ InfixOp Expression3 | instanceof Type }
Expression3:
PrefixOp Expression3
'(' Type ')' Expression3
'(' Expression ')' Expression3
Primary { Selector } { PostfixOp }
The italicized parentheses should be ignored. We treat them as terminals.
CatchType:
Identifier { '|' Identifier }
Modified version
bnf
CatchType:
QualifiedIdentifier { '|' QualifiedIdentifier }
Creator: NonWildcardTypeArguments CreatedName ClassCreatorRest CreatedName ( ClassCreatorRest | ArrayCreatorRest )
We have to take into account primitive arrays and we add one more production rule to Creator. For example:
java int[] a = new int[1];
Modified version
Creator: NonWildcardTypeArguments CreatedName ClassCreatorRest CreatedName ( ClassCreatorRest | ArrayCreatorRest ) BasicType ArrayCreatorRest
NonWildcardTypeArguments: < TypeList >
And TypeList expands to
TypeList: ReferenceType { , ReferenceType }
The problem is that it won't take into account arrays. Examples
< String[] > < int[] >
Modified version
NonWildcardTypeArguments: < TypeList2 >
TypeList2: Type { , Type }
The parser or the output code has to check for the invalid input of primitives that are not arrays.
TypeArgument: ReferenceType ? [ ( extends | super ) ReferenceType ]
Like NonWildcardTypeArguments, this production rule doesn't take into account arrays, including basic type arrays.
Modified version
bnf TypeArgument: Type ? [ ( extends | super ) Type ]
ForControl: ForVarControl ForInit ; [Expression] ; [ForUpdate]
We have to make ForInit optional to handle this case
for (;;) { ... }
Modified version
ForControl: ForVarControl [ForInit] ; [Expression] ; [ForUpdate]
Expression: Expression1 [ AssignmentOperator Expression1 ]
This prevents multiple assignments like
a = b = 1
Modified version
Expression: Expression1 [ AssignmentOperator Expression ]
IdentifierSuffix:
'[' ( {'[]'} . class | Expression ) ']'
Modified version
IdentifierSuffix:
'[' ( ']' {'[]'} . class | Expression ']' )
Modified version
AnnotationTypeBody: [AnnotationTypeElementDeclarations]
©2025 daniberg.com