Another item from the day of the July meeting that I happened to miss.

Allen Wirfs-Brock wrote:
However, at the meeting, we did not discussion the fact that in ES3 NoIn was also used in:

IterationStatement : 'for' '(' [ExpressionNoIn] ';' [Expression] ';' [Expression] ')' Statement

This makes statements like this:

for (a in b ;;) ;

illegal in ES3.  This was presumably done for nanny reasons

No, not for nanny reasons.

as using the 'in' operator in this position isn't ambiguous. The availability of the NoIn productions also made it easy to express such a restriction.

Consider this toy grammar:

%token FOR
%token IDENT
%token IN
%token NUMBER

%%

Program:
        Statement
|       Program Statement
;

Statement:
        FOR '(' Expression ';' Expression ';' Expression ')' Statement
|       FOR '(' Expression IN Expression ')' Statement
|       Expression ';'
;

Expression:
        Expression IN Primary
|       Primary
;

Primary:
        NUMBER
|       IDENT
;

This grammar is ambiguous: as bison(1) says,

state 18

    6 Expression: Expression IN Primary .
    7           | Primary .

    IN        reduce using rule 6 (Expression)
    IN        [reduce using rule 7 (Expression)]
    ')'       reduce using rule 7 (Expression)
    $default  reduce using rule 6 (Expression)

See attached file for full output.


But if we eliminate the NoIn productions it's no longer so easy to impose that restriction. I may be able to come up with some other static semantic mechanism to express that restriction but it will have complexity similar to the NoIn productions.

You can'd do this via static semantics, as I said in my last message. We need an LR(1) grammar, that was always a consensus requirement.

My preference is to simply allow the use of the 'in' operator in the first expression of a for(;;) statement.

Ambiguity is not a matter of preference. We need to validate the ES6 grammar. Until then, please put back the NoIn productions. They were not there only because of the silly and unwanted initialiser option for 'for (var x = y of z)'. They were there because for-in and for(;;) have prefixes in common up to arbitrary lookahead.

/be


This is what the rev17 grammer does. As it is currently illegal in ES<=5.1, allowing 'in' use in that context is an extension rather than a breaking change. 'a in 'b may not be very useful in that position but neither is 'a + b'. The simplification of the expression grammar is a pretty big win both now and for future extensions.

Allen
State 18 conflicts: 1 reduce/reduce


Grammar

    0 $accept: Program $end

    1 Program: Statement
    2        | Program Statement

    3 Statement: FOR '(' Expression ';' Expression ';' Expression ')' Statement
    4          | FOR '(' Expression IN Expression ')' Statement
    5          | Expression ';'

    6 Expression: Expression IN Primary
    7           | Primary

    8 Primary: NUMBER
    9        | IDENT


Terminals, with rules where they appear

$end (0) 0
'(' (40) 3 4
')' (41) 3 4
';' (59) 3 5
error (256)
FOR (258) 3 4
IDENT (259) 9
IN (260) 4 6
NUMBER (261) 8


Nonterminals, with rules where they appear

$accept (10)
    on left: 0
Program (11)
    on left: 1 2, on right: 0 2
Statement (12)
    on left: 3 4 5, on right: 1 2 3 4
Expression (13)
    on left: 6 7, on right: 3 4 5 6
Primary (14)
    on left: 8 9, on right: 6 7


state 0

    0 $accept: . Program $end

    FOR     shift, and go to state 1
    IDENT   shift, and go to state 2
    NUMBER  shift, and go to state 3

    Program     go to state 4
    Statement   go to state 5
    Expression  go to state 6
    Primary     go to state 7


state 1

    3 Statement: FOR . '(' Expression ';' Expression ';' Expression ')' 
Statement
    4          | FOR . '(' Expression IN Expression ')' Statement

    '('  shift, and go to state 8


state 2

    9 Primary: IDENT .

    $default  reduce using rule 9 (Primary)


state 3

    8 Primary: NUMBER .

    $default  reduce using rule 8 (Primary)


state 4

    0 $accept: Program . $end
    2 Program: Program . Statement

    $end    shift, and go to state 9
    FOR     shift, and go to state 1
    IDENT   shift, and go to state 2
    NUMBER  shift, and go to state 3

    Statement   go to state 10
    Expression  go to state 6
    Primary     go to state 7


state 5

    1 Program: Statement .

    $default  reduce using rule 1 (Program)


state 6

    5 Statement: Expression . ';'
    6 Expression: Expression . IN Primary

    IN   shift, and go to state 11
    ';'  shift, and go to state 12


state 7

    7 Expression: Primary .

    $default  reduce using rule 7 (Expression)


state 8

    3 Statement: FOR '(' . Expression ';' Expression ';' Expression ')' 
Statement
    4          | FOR '(' . Expression IN Expression ')' Statement

    IDENT   shift, and go to state 2
    NUMBER  shift, and go to state 3

    Expression  go to state 13
    Primary     go to state 7


state 9

    0 $accept: Program $end .

    $default  accept


state 10

    2 Program: Program Statement .

    $default  reduce using rule 2 (Program)


state 11

    6 Expression: Expression IN . Primary

    IDENT   shift, and go to state 2
    NUMBER  shift, and go to state 3

    Primary  go to state 14


state 12

    5 Statement: Expression ';' .

    $default  reduce using rule 5 (Statement)


state 13

    3 Statement: FOR '(' Expression . ';' Expression ';' Expression ')' 
Statement
    4          | FOR '(' Expression . IN Expression ')' Statement
    6 Expression: Expression . IN Primary

    IN   shift, and go to state 15
    ';'  shift, and go to state 16


state 14

    6 Expression: Expression IN Primary .

    $default  reduce using rule 6 (Expression)


state 15

    4 Statement: FOR '(' Expression IN . Expression ')' Statement
    6 Expression: Expression IN . Primary

    IDENT   shift, and go to state 2
    NUMBER  shift, and go to state 3

    Expression  go to state 17
    Primary     go to state 18


state 16

    3 Statement: FOR '(' Expression ';' . Expression ';' Expression ')' 
Statement

    IDENT   shift, and go to state 2
    NUMBER  shift, and go to state 3

    Expression  go to state 19
    Primary     go to state 7


state 17

    4 Statement: FOR '(' Expression IN Expression . ')' Statement
    6 Expression: Expression . IN Primary

    IN   shift, and go to state 11
    ')'  shift, and go to state 20


state 18

    6 Expression: Expression IN Primary .
    7           | Primary .

    IN        reduce using rule 6 (Expression)
    IN        [reduce using rule 7 (Expression)]
    ')'       reduce using rule 7 (Expression)
    $default  reduce using rule 6 (Expression)


state 19

    3 Statement: FOR '(' Expression ';' Expression . ';' Expression ')' 
Statement
    6 Expression: Expression . IN Primary

    IN   shift, and go to state 11
    ';'  shift, and go to state 21


state 20

    4 Statement: FOR '(' Expression IN Expression ')' . Statement

    FOR     shift, and go to state 1
    IDENT   shift, and go to state 2
    NUMBER  shift, and go to state 3

    Statement   go to state 22
    Expression  go to state 6
    Primary     go to state 7


state 21

    3 Statement: FOR '(' Expression ';' Expression ';' . Expression ')' 
Statement

    IDENT   shift, and go to state 2
    NUMBER  shift, and go to state 3

    Expression  go to state 23
    Primary     go to state 7


state 22

    4 Statement: FOR '(' Expression IN Expression ')' Statement .

    $default  reduce using rule 4 (Statement)


state 23

    3 Statement: FOR '(' Expression ';' Expression ';' Expression . ')' 
Statement
    6 Expression: Expression . IN Primary

    IN   shift, and go to state 11
    ')'  shift, and go to state 24


state 24

    3 Statement: FOR '(' Expression ';' Expression ';' Expression ')' . 
Statement

    FOR     shift, and go to state 1
    IDENT   shift, and go to state 2
    NUMBER  shift, and go to state 3

    Statement   go to state 25
    Expression  go to state 6
    Primary     go to state 7


state 25

    3 Statement: FOR '(' Expression ';' Expression ';' Expression ')' Statement 
.

    $default  reduce using rule 3 (Statement)
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to