From: sasghm@theseus.unx.sas.com (Gary Merrill)
Subject: Syntactic Ambiguity Resolution in C++
Originator: sasghm@theseus.unx.sas.com
Date: Mon, 31 Aug 1992 20:48:26 GMT
Organization: SAS Institute Inc.


Since we are about to make a beta release of our MVS/CMS mainframe
C++ compiler I took a few moments to run a small set of my regression
tests for syntactic ambiguity resolution through a number of other
C++ products.  Syntactic ambiguity resolution in C++ is a subject
near and dear to my heart since I have expended no small amount of
effort in that direction.  I thought others might be interested in
the results.

The compilers I tested are:

	HP C++  (I don't know what the version number of this
                is, but it is running on an HP 700 and 'what'
                yields the result:  "HP C++ B2402 A.02.34".)

	g++     Version 2.2.2

	Borland C++ Version 2.0  (I don't have 3.? handy.)

	Microsoft C++ V7

	Zortech C++ V3.0r1

The little tests below are just a few simple ones I have used to
test our parser.  They are meant to expose only very fundamental
problems in parsing declaration statements and expression statements.

To summarize these very informal tests, one might say that that
Microsoft and Zortech seem to do things pretty well.  Borland is
not far behind.  HP has some real problems.  And g++ doesn't seem
to be trying.  Newer versions of any of these compilers not immediately
accessible to me may have improvements.

Note that no one (except SAS/C C++!) handles Test 9 (which is
admittedly somewhat perverse).

The examples below assume the following definition of 'X'.  Some
compilers (HP and Borland) do not permit the second form of
the operator definition, and hence the '#if'.

class X {
   public:
   X(int);
   X(void);
#if HP
   operator++();
#else
   operator++(int);
#endif
   };


Failure for these tests (except where otherwise noted) means that
a syntax error is diagnosed by the compiler.  This is all acceptable
C++ code.

/* --------------------------------------------------------------*/

// Test 1:  failed by g++.

typedef int T;
T(a) = 0;

/* --------------------------------------------------------------*/

// Test 2:  failed by HP and g++.

void f(int a, int b)
{

   X(a)++;
}

/* --------------------------------------------------------------*/

// Test 3:  failed by HP and g++.

X foo(int);

int b;
X x(1);
X(c);       // HP & g++ error on this line

void f(int a)
{
   X(a)++;  // and this one.  It appears to be a cascade for HP, but
            // not for g++.

}

/* --------------------------------------------------------------*/

// Test 4:  failed by g++ and Borland

typedef int T;

int foo(void)
{

for ( T(a) = 0; a < 10; a++)  // T(a) not handled correctly
    return a;
}


/* --------------------------------------------------------------*/

// Test 5:  failed by HP and g++.

int a;

int foo(void)
{

for ( X(a)++; a < 10; a++)  // X(a) not handled correctly
    return a;
}

/* --------------------------------------------------------------*/

// Test 6:  failed by g++, borland, and Zortech

typedef int T;

X f(T(a));  /* declaration of fcn f -- NOT of a class */

/* --------------------------------------------------------------*/

// Test 7:  failed by HP and g++.

int a;

void foo(void)
{
X(X(a)++)++;
}

/* --------------------------------------------------------------*/

// Test 8:  failed by g++.

typedef void T;

static void foo()
{
T(*e)(int);        // declaration
}

/* --------------------------------------------------------------*/

// Test 9:  failed by HP, g++, Borland, MS, and Zortech.

int a;

void foo(void)
{
X(X[2]); // decl of X as an array of two X's
}

/* --------------------------------------------------------------*/

// Test 10:  failed by HP and g++.

typedef int T;
double i;
int j = (T(i));


/* --------------------------------------------------------------*/

// Test 11:  failed by g++.

int foo( int(a), int(b) );

void goo(void)
{
short x, y;

foo( int(x), int(y) );

}


/* --------------------------------------------------------------*/

// Test 12:  failed by

typedef int T;
double i;

int k = (T(i));

/* --------------------------------------------------------------*/


// Test 13:  failed by HP, g++, Borland, and Zortech.

// In such cases, the failure is that the function declaration
// is instead interpreted as an object declaration.

class S {

public:

	S(int);
};
short a;

void foo(void)
{
S x(int(a));  // function declaration
}

-- 
Gary H. Merrill  [Principal Systems Developer, C Compiler Development]
SAS Institute Inc. / SAS Campus Dr. / Cary, NC  27513 / (919) 677-8000
sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm