Why are unmatched array dimensions valid? #3

Closed
opened 2021-06-27 15:24:51 +02:00 by ColinTimBarndt · 1 comment
ColinTimBarndt commented 2021-06-27 15:24:51 +02:00 (Migrated from github.com)

I am working on a tree traversal library for lezer/java and found this definition which was a bit strange for me:

Dimension {
  annotation* !array "[" "]"+
}

This leads me to my question: Why is int[]] a valid syntax in this grammar? Is this intended?

Here's a valid syntax tree parsed by this grammar:

class HelloWorld {
    public static void main(String[]] args) {
        System.out.println("Hello, World!");
    }
}
Program:
└╴ClassDeclaration:
  ├╴class
  ├╴Definition: HelloWorld
  └╴ClassBody:
    ├╴{
    ├╴MethodDeclaration:
    │ ├╴Modifiers:
    │ │ ├╴public
    │ │ └╴static
    │ ├╴void
    │ ├╴Definition: main
    │ ├╴FormalParameters:
    │ │ ├╴(
    │ │ ├╴FormalParameter:
    │ │ │ ├╴ArrayType:
    │ │ │ │ ├╴TypeName: String
    │ │ │ │ └╴Dimension:
    │ │ │ │   ├╴[
    │ │ │ │   ├╴]
    │ │ │ │   └╴]
    │ │ │ └╴Definition: args
    │ │ └╴)
    │ └╴Block:
    │   ├╴{
    │   ├╴ExpressionStatement:
    │   │ ├╴MethodInvocation:
    │   │ │ ├╴FieldAccess:
    │   │ │ │ ├╴Identifier: System
    │   │ │ │ ├╴.
    │   │ │ │ └╴Identifier: out
    │   │ │ ├╴.
    │   │ │ ├╴MethodName:
    │   │ │ │ └╴Identifier: println
    │   │ │ └╴ArgumentList:
    │   │ │   ├╴(
    │   │ │   ├╴StringLiteral: "Hello, World!"
    │   │ │   └╴)
    │   │ └╴;
    │   └╴}
    └╴}

(Definition of "Dims" in the official Java 16 documentation)

I am working on a tree traversal library for lezer/java and found [this definition](https://github.com/lezer-parser/java/blob/5b59e06240bed72717cfd34542e60beb76f3d44d/src/java.grammar#L166:L168) which was a bit strange for me: ```grammar Dimension { annotation* !array "[" "]"+ } ``` This leads me to my question: Why is `int[]]` a valid syntax in this grammar? Is this intended? Here's a valid syntax tree parsed by this grammar: <details> ```java class HelloWorld { public static void main(String[]] args) { System.out.println("Hello, World!"); } } ``` ```txt Program: └╴ClassDeclaration: ├╴class ├╴Definition: HelloWorld └╴ClassBody: ├╴{ ├╴MethodDeclaration: │ ├╴Modifiers: │ │ ├╴public │ │ └╴static │ ├╴void │ ├╴Definition: main │ ├╴FormalParameters: │ │ ├╴( │ │ ├╴FormalParameter: │ │ │ ├╴ArrayType: │ │ │ │ ├╴TypeName: String │ │ │ │ └╴Dimension: │ │ │ │ ├╴[ │ │ │ │ ├╴] │ │ │ │ └╴] │ │ │ └╴Definition: args │ │ └╴) │ └╴Block: │ ├╴{ │ ├╴ExpressionStatement: │ │ ├╴MethodInvocation: │ │ │ ├╴FieldAccess: │ │ │ │ ├╴Identifier: System │ │ │ │ ├╴. │ │ │ │ └╴Identifier: out │ │ │ ├╴. │ │ │ ├╴MethodName: │ │ │ │ └╴Identifier: println │ │ │ └╴ArgumentList: │ │ │ ├╴( │ │ │ ├╴StringLiteral: "Hello, World!" │ │ │ └╴) │ │ └╴; │ └╴} └╴} ``` </details> [(Definition of "Dims" in the official Java 16 documentation)](https://docs.oracle.com/javase/specs/jls/se16/html/jls-8.html#d5e13540)
marijnh commented 2021-06-27 19:11:16 +02:00 (Migrated from github.com)

The + was accidentally left when I changed that rule in d9d55157. Attached patch drops it.

The `+` was accidentally left when I changed that rule in d9d55157. Attached patch drops it.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lezer/java#3
No description provided.