Re: [Java Spec Report] JSR 201 enhanced-for comments
Neal Gafter <[email protected]> Wed, 18 Feb 2004 14:19:35 -0800
| Newsgroups | gmane.comp.java.spec-report |
|---|---|
| Message-ID | <[email protected]> |
Eric is certainly correct with regards to the short if grammar changes. Those
changes are needed to make the language unambiguous in dealing with the dangling
else problem for constructs such as
if (expr) for (Type v : expr) if (expr) stmt1; else stmt2;
About the FormalParameter issue, it isn't clear to me to see why there is a
problem. If you have a choice between FormalParameter and
LocalVariableDeclaration, it can only be due to being inside a for loop. A
single-character lookahead should resolve the ambiguity: if you see a ',' or '='
then it is a VariableDeclarator; if you see a ':' it is a FormalParameter. I'd
like to see the itemset of the problem LALR(1) parser state to better understand
the issue; can you make a yacc grammar available to me? There was a genuine
language ambiguity when we changed the assert statement, but I believe there is
no underlying language or grammar ambiguity in the for-each loop.
I suggest you try the following grammar factoring. Transform a copy of the
grammar rule LocalVariableDeclaration by inlining VariableDeclarators, changing
it from
LocalVariableDeclaration:
finalopt Type VariableDeclarators
to:
ForLocalVariableDeclaration:
finalopt Type VariableDeclarator
finalopt Type VariableDeclarators, VariableDeclarator
and then inlining VariableDeclarator in the first line resulting in
ForLocalVariableDeclaration:
finalopt Type VariableDeclaratorId
finalopt Type VariableDeclaratorId = VariableInitializer
finalopt Type VariableDeclarators, VariableDeclarator
and finally, substituting the nonterminal FormalParameter for the first of these:
ForLocalVariableDeclaration:
FormalParameter
finalopt Type VariableDeclaratorId = VariableInitializer
finalopt Type VariableDeclarators, VariableDeclarator
[Warning: I haven't tested these musings with a parser generator.]
Regards,
Neal
Eric Blake wrote:
> In trying to implement this, I found a HUGE problem - the proposed grammar is
> not LALR(1). Remember how JSR 41 (assert statement) changed its grammar long
> after the public draft closed, because the proposed grammar was not LALR(1)?
>
>
>> /EnhancedForStatement:/
>> for ( /FormalParameter/ : /Expression/ )
>> /Statement/
>
>
> When the parser sees "for (@A final int i", your proposal makes it impossible
> to decide whether you are starting a FormalParameter or
> LocalVariableDeclaration (as part of ForInitopt). You are going to have to
> factor this statement out; I haven't yet had time to tackle the problem, but
> if I can find a factorization that works, I will post it.
>
> You also need to add:
> EnhancedForStatementNoShortIf:
> 'for' '(' <whatever factorization works> ':' Expression ')'
> StatementNoShortIf
>
> And you should explicitly list:
> Statement:
> <all existing productions>
> EnhancedForStatement
> StatementNoShortIf:
> <all existing productions>
> EnhancedForStatementNoShortIf:
>
To unsubscribe from this mailing list, send an email to:
[email protected]
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/java-spec-report/
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/