The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Grammar ambiguities in C# 2.0

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Adrian Florea

Posts: 206
Nickname: adrian11
Registered: Jul, 2004

Adrian Florea is a .NET developer from Italy
Grammar ambiguities in C# 2.0 Posted: Oct 2, 2005 10:13 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Adrian Florea.
Original Post: Grammar ambiguities in C# 2.0
Feed Title: Web Log di Adrian Florea
Feed URL: /error.aspx?aspxerrorpath=/adrian/Rss.aspx
Feed Description: "You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Adrian Florea
Latest Posts From Web Log di Adrian Florea

Advertisement

Secondo voi, se questo snippet compila:

class Test
{
      static void Foo(bool b1, bool b2)
      {
            System.Console.WriteLine("{0}, {1}", b1, b2);
      }
 
      static void Main()
      {
            bool b1 = expr1;
            bool b2 = expr2;
            Foo(b1, b2);
      }
}

e lasciando identiche le espressioni expr1 e expr2, dovrebbe per forza compilare senza errori anche quest'altro snippet?:

class Test
{
      static void Foo(bool b1, bool b2)
      {
            System.Console.WriteLine("{0}, {1}", b1, b2);
      }
 
      static void Main()
      {
           Foo(expr1, expr2);
      }
}

Non sempre! Ed ecco un esempio, suggerito dalle specifiche (ECMA-334, 3rd Ed., 9.2.3 "Grammar ambiguities" - dove troverete anche altre situazioni!):

// compila senza errori e stampa True, False
class Test
{
      static void Foo(bool b1, bool b2)
      {
            System.Console.WriteLine("{0}, {1}", b1, b2);
      }
 
      static void Main()
      {
            int a = 1;
            int b = 2;
            int c = 3;
 
            bool b1 = a<b;
            bool b2 = c>(4+5);
            Foo(b1, b2);
      }
}

// errori di compilazione: 2 errori CS0246 e 1 errore CS0471
class Test
{
      static void Foo(bool b1, bool b2)
      {
            System.Console.WriteLine("{0}, {1}", b1, b2);
      }
 
      static void Main()
      {
            int a = 1;
            int b = 2;
            int c = 3;
 
            Foo(a<b, c>(4+5));
      }
}

L'ambiguità è risolta in modo un po' radicale (molto probabilmente per motivi di performance del compilatore):

"If a sequence of tokens can be parsed (in context) as a simple-name, member-access, or pointer-member-access ending with a type-argument-list, the token immediately following the closing > token is examined. If it is one of

( ) ] : ; , . ? == !=

then the type-argument-list is retained as part of the simple-name, member-access or pointer-member-access and any other possible parse of the sequence of tokens is discarded. Otherwise, the type-argument-list is not considered part of the simple-name, member-access or pointer-member-access, even if there is no other possible parse of the sequence of tokens."

Questo tipo di regola si chiama, nella teoria dei compilatori, predicato sintattico (syntactic predicate).

Read: Grammar ambiguities in C# 2.0

Topic: [MapPoint] Neuerungen in der Version 4.0 Previous Topic   Next Topic Topic: Java Leads Again; VB in Massive Decline

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use