[sv-bc] Ambiguity in comma-separated for loop initializer list


Subject: [sv-bc] Ambiguity in comma-separated for loop initializer list
From: Paul Graham (pgraham@cadence.com)
Date: Tue Jan 20 2004 - 09:37:03 PST


In view of the syntactic similarity between a reg declaration with an
initializer and the initializer in a for loop, I wonder if the following
loop:

1) for (int i = 1, j = 2; ...

is equivalent to:

2) for (int i = 1, int j = 2; ...

That is, should the construct

3) int i = 1, j = 2;

be interpreted as a reg declaration which declares both i and j, or should
each comma-delimited initializer be treated independently with respect to
declarations? This makes a difference, especially if, say, j is already
declared outside the loop.

The Gnu g++ compiler treats 1) as two declarations, for both i and j. In
fact, g++ disallows the following:

4) for (int i = 1, int j = 2; ...

I suggest that we do the same thing as C++ (or at least the same as g++),
and make it clear that in the case that one index variable is declared in
the scope of the for loop, then all the index variables appearing in the
initializer are declared as part of the same declaration. Right now the
grammar allows example 4), with multiple, possibly different, data types.

So I would suggest changing the grammar from:

    for_initialization ::=
        list_of_variable_assignments
        | data_type list_of_variable_assignments { , data_type list_of_variable_assignments }

to:

    for_initialization ::=
        [data_type] list_of_variable_assignments

Paul



This archive was generated by hypermail 2b28 : Tue Jan 20 2004 - 09:43:19 PST