Jump to content

Ambiguous grammar rules of foreach

Recommended Posts

All foreach grammar rules (activity_foreach_stmt, foreach_constraint_item, and procedural_foreach_stmt) are ambiguous. Consider the grammar rule below (terminal symbols are enclosed in double quotes).

procedural_foreach_stmt ::= foreach "(" [ iterator_identifier ":" ] expression [ "[" index_identifier "]" ] ")" procedural_stmt

Assume there is an integer k and a map m.

int k = 0;
map< int, map<int, int> > m = { 0: {0: 0, 1: 1} };

Then the following statement is ambiguous.

foreach (v : m[k]) ...

There are two interpretations.

  1. for each value v with index k in m (index_identifier = k in this case)
  2. for each value v in m[k] (i.e., m[0]) (no index_identifier in this case)


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...