daniberg.com

posts github chip8 rtc guitar

Java 7 grammar fixes

Here are a few fixes in the grammar specification for Java7 that I've introduced while developing c4.

1. Expression2Rest

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 }

2. Expression3

Expression3:
  PrefixOp Expression3
  '(' Type ')' Expression3
  '(' Expression ')' Expression3
  Primary { Selector } { PostfixOp }

The italicized parentheses should be ignored. We treat them as terminals.

3. CatchType

CatchType:
  Identifier { '|' Identifier }

Modified version

bnf
CatchType:
  QualifiedIdentifier { '|' QualifiedIdentifier }

4. Creator

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

5. NonWildcardTypeArguments

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.

6. TypeArgument

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 ]

7. ForControl

ForControl:
  ForVarControl
  ForInit ; [Expression] ; [ForUpdate]

We have to make ForInit optional to handle this case

for (;;) { ... }

Modified version

ForControl:
  ForVarControl
  [ForInit] ; [Expression] ; [ForUpdate]

8. Expression

Expression: Expression1 [ AssignmentOperator Expression1 ]

This prevents multiple assignments like

a = b = 1

Modified version

Expression: Expression1 [ AssignmentOperator Expression ]

9. IdentifierSuffix

IdentifierSuffix:
  '[' ( {'[]'} . class | Expression ) ']'

Modified version

IdentifierSuffix:
  '[' ( ']' {'[]'} . class | Expression ']' )

10. AnnotationTypeBody

Modified version

AnnotationTypeBody:
  [AnnotationTypeElementDeclarations]

©2023 daniberg.com