Skip to content

EBNF for the foreach_statement is incorrect. (draft-v8) #1602

@kaby76

Description

@kaby76

Describe the bug
I am a maintainer for https://github.com/antlr/grammars-v4 and I am in the process of bringing the grammar in grammars-v4 up to date for C#. I am also scraping and validating the grammars over here and in the published ECMA 334 .pdf.

The rule for the foreach_statement is incorrect, starting in version 7.

foreach_statement
: 'await'? 'foreach' '(' ref_kind? local_variable_type identifier
'in' expression ')' embedded_statement
;

Unless it really is not part of the spec, the rule should have an alternative for deconstruction_expression.

// Source: §13.9.5.1 General
foreach_statement
    : 'await'? 'foreach' '(' ref_kind? local_variable_type identifier
      'in' expression ')' embedded_statement
    | 'await'? 'foreach' '(' deconstruction_expression
      'in' expression ')' embedded_statement
    ;

Without the fix, the spec EBNF does not parse several input in the Roslyn v7 compiler itself. But, below is a simpler example that does not parse. (There are other places the Roslyn compiler diverges, so I don't know what is intended.)

Example

using System.Collections.Generic;

  class Program
  {
      static void Main()
      {
          var map = new Dictionary<string, int> { ["a"] = 1 };
          foreach (var (key, value) in map) { }
      }
  }

Attached is a zip file containing a full program with the example. It builds and runs with compiler version 7.

v7test.zip

Additional context
The current means of generating and testing the Antlr4 grammar is archaic. The "BuildGrammar" tool should generate a C#-target port, not a Java one, and definitely not using the ancient Antlr 4.9.2. There are better tools nowadays for analyzing the EBNF.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions