home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13114 < prev    next >
Encoding:
Text File  |  1992-08-31  |  4.8 KB  |  229 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!gatech!concert!sas!mozart.unx.sas.com!sasghm
  3. From: sasghm@theseus.unx.sas.com (Gary Merrill)
  4. Subject: Syntactic Ambiguity Resolution in C++
  5. Originator: sasghm@theseus.unx.sas.com
  6. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  7. Message-ID: <Btv8Gq.B3@unx.sas.com>
  8. Date: Mon, 31 Aug 1992 20:48:26 GMT
  9. Nntp-Posting-Host: theseus.unx.sas.com
  10. Organization: SAS Institute Inc.
  11. Lines: 216
  12.  
  13.  
  14. Since we are about to make a beta release of our MVS/CMS mainframe
  15. C++ compiler I took a few moments to run a small set of my regression
  16. tests for syntactic ambiguity resolution through a number of other
  17. C++ products.  Syntactic ambiguity resolution in C++ is a subject
  18. near and dear to my heart since I have expended no small amount of
  19. effort in that direction.  I thought others might be interested in
  20. the results.
  21.  
  22. The compilers I tested are:
  23.  
  24.     HP C++  (I don't know what the version number of this
  25.                 is, but it is running on an HP 700 and 'what'
  26.                 yields the result:  "HP C++ B2402 A.02.34".)
  27.  
  28.     g++     Version 2.2.2
  29.  
  30.     Borland C++ Version 2.0  (I don't have 3.? handy.)
  31.  
  32.     Microsoft C++ V7
  33.  
  34.     Zortech C++ V3.0r1
  35.  
  36. The little tests below are just a few simple ones I have used to
  37. test our parser.  They are meant to expose only very fundamental
  38. problems in parsing declaration statements and expression statements.
  39.  
  40. To summarize these very informal tests, one might say that that
  41. Microsoft and Zortech seem to do things pretty well.  Borland is
  42. not far behind.  HP has some real problems.  And g++ doesn't seem
  43. to be trying.  Newer versions of any of these compilers not immediately
  44. accessible to me may have improvements.
  45.  
  46. Note that no one (except SAS/C C++!) handles Test 9 (which is
  47. admittedly somewhat perverse).
  48.  
  49. The examples below assume the following definition of 'X'.  Some
  50. compilers (HP and Borland) do not permit the second form of
  51. the operator definition, and hence the '#if'.
  52.  
  53. class X {
  54.    public:
  55.    X(int);
  56.    X(void);
  57. #if HP
  58.    operator++();
  59. #else
  60.    operator++(int);
  61. #endif
  62.    };
  63.  
  64.  
  65. Failure for these tests (except where otherwise noted) means that
  66. a syntax error is diagnosed by the compiler.  This is all acceptable
  67. C++ code.
  68.  
  69. /* --------------------------------------------------------------*/
  70.  
  71. // Test 1:  failed by g++.
  72.  
  73. typedef int T;
  74. T(a) = 0;
  75.  
  76. /* --------------------------------------------------------------*/
  77.  
  78. // Test 2:  failed by HP and g++.
  79.  
  80. void f(int a, int b)
  81. {
  82.  
  83.    X(a)++;
  84. }
  85.  
  86. /* --------------------------------------------------------------*/
  87.  
  88. // Test 3:  failed by HP and g++.
  89.  
  90. X foo(int);
  91.  
  92. int b;
  93. X x(1);
  94. X(c);       // HP & g++ error on this line
  95.  
  96. void f(int a)
  97. {
  98.    X(a)++;  // and this one.  It appears to be a cascade for HP, but
  99.             // not for g++.
  100.  
  101. }
  102.  
  103. /* --------------------------------------------------------------*/
  104.  
  105. // Test 4:  failed by g++ and Borland
  106.  
  107. typedef int T;
  108.  
  109. int foo(void)
  110. {
  111.  
  112. for ( T(a) = 0; a < 10; a++)  // T(a) not handled correctly
  113.     return a;
  114. }
  115.  
  116.  
  117. /* --------------------------------------------------------------*/
  118.  
  119. // Test 5:  failed by HP and g++.
  120.  
  121. int a;
  122.  
  123. int foo(void)
  124. {
  125.  
  126. for ( X(a)++; a < 10; a++)  // X(a) not handled correctly
  127.     return a;
  128. }
  129.  
  130. /* --------------------------------------------------------------*/
  131.  
  132. // Test 6:  failed by g++, borland, and Zortech
  133.  
  134. typedef int T;
  135.  
  136. X f(T(a));  /* declaration of fcn f -- NOT of a class */
  137.  
  138. /* --------------------------------------------------------------*/
  139.  
  140. // Test 7:  failed by HP and g++.
  141.  
  142. int a;
  143.  
  144. void foo(void)
  145. {
  146. X(X(a)++)++;
  147. }
  148.  
  149. /* --------------------------------------------------------------*/
  150.  
  151. // Test 8:  failed by g++.
  152.  
  153. typedef void T;
  154.  
  155. static void foo()
  156. {
  157. T(*e)(int);        // declaration
  158. }
  159.  
  160. /* --------------------------------------------------------------*/
  161.  
  162. // Test 9:  failed by HP, g++, Borland, MS, and Zortech.
  163.  
  164. int a;
  165.  
  166. void foo(void)
  167. {
  168. X(X[2]); // decl of X as an array of two X's
  169. }
  170.  
  171. /* --------------------------------------------------------------*/
  172.  
  173. // Test 10:  failed by HP and g++.
  174.  
  175. typedef int T;
  176. double i;
  177. int j = (T(i));
  178.  
  179.  
  180. /* --------------------------------------------------------------*/
  181.  
  182. // Test 11:  failed by g++.
  183.  
  184. int foo( int(a), int(b) );
  185.  
  186. void goo(void)
  187. {
  188. short x, y;
  189.  
  190. foo( int(x), int(y) );
  191.  
  192. }
  193.  
  194.  
  195. /* --------------------------------------------------------------*/
  196.  
  197. // Test 12:  failed by
  198.  
  199. typedef int T;
  200. double i;
  201.  
  202. int k = (T(i));
  203.  
  204. /* --------------------------------------------------------------*/
  205.  
  206.  
  207. // Test 13:  failed by HP, g++, Borland, and Zortech.
  208.  
  209. // In such cases, the failure is that the function declaration
  210. // is instead interpreted as an object declaration.
  211.  
  212. class S {
  213.  
  214. public:
  215.  
  216.     S(int);
  217. };
  218. short a;
  219.  
  220. void foo(void)
  221. {
  222. S x(int(a));  // function declaration
  223. }
  224.  
  225. -- 
  226. Gary H. Merrill  [Principal Systems Developer, C Compiler Development]
  227. SAS Institute Inc. / SAS Campus Dr. / Cary, NC  27513 / (919) 677-8000
  228. sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm
  229.