[antlr-interest] MSVC 6.0 Compiler accuracy?

Andy Tripp antlr at jazillian.com
Mon May 22 08:23:30 PDT 2006


According to the ISO/IEC 14882:1998 C++ standard, all those
declarations inside conditions are, in fact, legal. So if MSVC++ doesn't 
accept them,
it's a problem with MSVC++ - perhaps there's a "standards compatible 
mode" option?
Yes, each of these statements opens a new scope, the variable is 
declared once, and
its scope is what is should be :)

Andy

Wigg, J D wrote:

> I am the latest author of the C++ parser available from the Antlr website.
>  
> It has been brought to my attention that it does not parse the 
> following properly because of declarations in conditions,
>  
> bool fun()
>  {
>  int i, j, k, l = 0;
>  
>  if(int i = 1)   // MSVC++ 6.0 accepts declaration and redefinition of i.
>   {
>   printf("%d ",i);
>   }
>  
>  switch(int j = 1)   // MSVC++ 6.0 accepts declaration and 
> redefinition of j.   
>   {
>  case 1:
>   return true;
>  default:
>   return false;
>   }
>  
>  while(int k = 2)   // MSVC++ 6.0 accepts declaration and redefinition 
> of k.
>   {
>   k--;
>   printf("%d ",k);
>   }
>  
>  for(int i = 5; int l = i; i--) // MSVC++ 6.0 rejects redefinition of 
> i and
>   {                                // declaration in condition  
>   printf("%d ",l);
>   }
>  
> I have not made allowance for the apparently legal possibility 
> according to reliable grammar definitions of declaring a variable in 
> the condition statement of these four statements.
>  
> I am told that gcc 3.3.3 accepts the declaration in the above "for" 
> statement.
>  
> It therefore looks as though MSVC++ 6.0 is incorrect in this instance. 
> I would be grateful if someone would confirm this. Perhaps there is a 
> later version of MSVC which parses this "for" statement correctly?
>  
> I presume that each of these four statements opens a new scope.
>  
> I have been parsing various C++ programs for some years now and never 
> come across the declaration of variables in conditions. I suppose this 
> is not surprising. I can't think why one would want to do it. It seems 
> illogical to allow this in iteration statements since the condition 
> would appear to redeclare the variable on each iteration!
>  
> It would be interesting to hear of any use for this feature.
>  
> David
> Centre for Systems and Software Engineering
> London South Bank University
> UK.
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  
>  




More information about the antlr-interest mailing list