public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Parsing of switch statement
@ 1997-12-10 12:02 Craig J Copi
  1997-12-10 15:53 ` Joe Buck
  1997-12-11  5:32 ` Oleg Krivosheev
  0 siblings, 2 replies; 3+ messages in thread
From: Craig J Copi @ 1997-12-10 12:02 UTC (permalink / raw)
  To: egcs

	Recently I have encountered some code that contains an empty case
statement at the end of a switch (see example below).  The code compiles under
gcc but not under g++.  I get the error
test.c: In function `int main (int, char **)':
test.c:9: parse error before `}' 
If I put a ';' after the case 0 : it will also compile under g++.  I've tried
this on sparc-sun-solaris2.5.1 with egcs-2.90.21 971202 (egcs-1.00 release)
and egcs-2.91.02 971206 (gcc-2.8.0); both give the same results.  Is this the
correct behavior?  Does the C++ spec require this?

	Craig

Here is the simple test

#include <stdio.h>

int main (int argc, char *argv[])
{
  int i=1;
  switch (i) {
  case 1 : printf ("1 ");
  case 0 : /* a ';' is needed here to compile with g++ */
  }
  printf ("\n");
  return 0;
}


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Parsing of switch statement
  1997-12-10 12:02 Parsing of switch statement Craig J Copi
@ 1997-12-10 15:53 ` Joe Buck
  1997-12-11  5:32 ` Oleg Krivosheev
  1 sibling, 0 replies; 3+ messages in thread
From: Joe Buck @ 1997-12-10 15:53 UTC (permalink / raw)
  To: egcs

> 	Recently I have encountered some code that contains an empty case
> statement at the end of a switch (see example below). 

Such code is neither legal C nor legal C++.  I think it's an extension
to gcc; you shouldn't assume that all GNU C extensions will be available
in g++ (some clash with other C++ syntax, not sure about this one).

> 
> Here is the simple test
> 
> #include <stdio.h>
> 
> int main (int argc, char *argv[])
> {
>   int i=1;
>   switch (i) {
>   case 1 : printf ("1 ");
>   case 0 : /* a ';' is needed here to compile with g++ */
>   }
>   printf ("\n");
>   return 0;
> }

The ';' is needed with any C or C++ compiler other than gcc.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Parsing of switch statement
  1997-12-10 12:02 Parsing of switch statement Craig J Copi
  1997-12-10 15:53 ` Joe Buck
@ 1997-12-11  5:32 ` Oleg Krivosheev
  1 sibling, 0 replies; 3+ messages in thread
From: Oleg Krivosheev @ 1997-12-11  5:32 UTC (permalink / raw)
  To: Craig J Copi; +Cc: egcs

Hi,

On Wed, 10 Dec 1997, Craig J Copi wrote:

> Date: Wed, 10 Dec 1997 15:01:50 -0500
> From: Craig J Copi <copi@erebus.phys.cwru.edu>
> Reply-To: egcs@cygnus.com
> To: egcs@cygnus.com
> Subject: Parsing of switch statement
> 
> 	Recently I have encountered some code that contains an empty case
> statement at the end of a switch (see example below).  The code compiles under
> gcc but not under g++.  I get the error
> test.c: In function `int main (int, char **)':
> test.c:9: parse error before `}' 
> If I put a ';' after the case 0 : it will also compile under g++.  I've tried
> this on sparc-sun-solaris2.5.1 with egcs-2.90.21 971202 (egcs-1.00 release)
> and egcs-2.91.02 971206 (gcc-2.8.0); both give the same results.  Is this the
> correct behavior?  

No, it's not.

> Does the C++ spec require this?

Nor C neither C++ require this. This is GNU C extension.

Just in case, some times ago i patched 
C++ parser to allow such a code. It was 
not accepted by maintainer, i believe.

Just in case i enclose patch below.

OK

*** parse.y.orig	Tue Jun 17 21:25:08 1997
--- parse.y	Mon Jul  7 00:58:28 1997
***************
*** 3583,3595 ****
  	  .poplevel
  		{ finish_stmt (); }
  	| CASE expr_no_commas ':'
! 		{ do_case ($2, NULL_TREE); }
  	  stmt
  	| CASE expr_no_commas ELLIPSIS expr_no_commas ':'
! 		{ do_case ($2, $4); }
  	  stmt
  	| DEFAULT ':'
! 		{ do_case (NULL_TREE, NULL_TREE); }
  	  stmt
  	| BREAK ';'
  		{ emit_line_note (input_filename, lineno);
--- 3583,3616 ----
  	  .poplevel
  		{ finish_stmt (); }
  	| CASE expr_no_commas ':'
! 		{ do_case ($2, NULL_TREE);
!                   if (yychar == YYEMPTY) /* read ahead */
! 		    yychar = YYLEX;
!                   if ( yychar == '}' ) { /* no stmt, just closing } */
!                     yyungetc ('}', 0 ); /* put it back */
!                     yychar=';' ; /* fool parser with fake empty stmt */
!                   }
!                 }
  	  stmt
  	| CASE expr_no_commas ELLIPSIS expr_no_commas ':'
! 		{ do_case ($2, $4);
!                   if (yychar == YYEMPTY) /* read ahead */
! 		    yychar = YYLEX;
!                   if ( yychar == '}' ) { /* no stmt, just closing } */
!                     yyungetc ('}', 0 ); /* put it back */
!                     yychar=';' ; /* fool parser with fake empty stmt */
!                   }
!                 }
  	  stmt
  	| DEFAULT ':'
! 		{ do_case (NULL_TREE, NULL_TREE);
!                   if (yychar == YYEMPTY) /* read ahead */
! 		    yychar = YYLEX;
!                   if ( yychar == '}' ) { /* no stmt, just closing } */
!                     yyungetc ('}', 0 ); /* put it back */
!                     yychar=';' ; /* fool parser with fake empty stmt */
!                   }
!                 }
  	  stmt
  	| BREAK ';'
  		{ emit_line_note (input_filename, lineno);



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1997-12-11  5:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-12-10 12:02 Parsing of switch statement Craig J Copi
1997-12-10 15:53 ` Joe Buck
1997-12-11  5:32 ` Oleg Krivosheev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).