linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* parse error
@ 2004-09-26 14:32 Ankit Jain
  2004-09-25  6:29 ` Elias Athanasopoulos
  0 siblings, 1 reply; 5+ messages in thread
From: Ankit Jain @ 2004-09-26 14:32 UTC (permalink / raw)
  To: linux prg

hi

i could not understand why compiler has put semi colon
before else due t owhich this parse error comes....

thanks

  
#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }

However that wouldn't work in some cases. The
following code is meant to be an if-statement with two
branches:

  if(x>y)
    exch(x,y);          // Branch 1
  else  
    do_something();     // Branch 2

But it would be interpreted as an if-statement with
only one branch:

  if(x>y) {                     // Single-branch
if-statement!!!
    int tmp;            // The one and only branch
consists
    tmp = x;            // of the block.
    x = y;
    y = tmp;
  }
  ;                             // empty statement
  else                  // ERROR!!! "parse error
before else"
    do_something();


________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping" 
your friends today! Download Messenger Now 
http://uk.messenger.yahoo.com/download/index.html

^ permalink raw reply	[flat|nested] 5+ messages in thread
* RE: parse error
@ 2004-09-27 15:58 Huber, George K RDECOM CERDEC STCD SRI
  0 siblings, 0 replies; 5+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-27 15:58 UTC (permalink / raw)
  To: 'Ankit Jain', linux prg

Ankit wrote:

>i could not understand why compiler has put semi colon
>before else due t owhich this parse error comes....

>#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }

Remember that macro expansion occures early in the compilation process 
(it occures in step 4 if I remember correctly).  In this case a simple
text replacement is performed.

>However that wouldn't work in some cases. The following code is 
>meant to be an if-statement with two branches:

  if(x>y)
    exch(x,y);          // Branch 1
  else  
    do_something();     // Branch 2

As written, assuming exch() is a function you would have a two branch
if statement.  However, because exch is a macro, this is what is passed
from the c preprocessor to the compiler (because exch(..) is replaced with
the body of the defination:

if(x>y)
  { int tmp; tmp=x; x=y; y=tmp;};
else
  do_something();

and you have a one-brance if statement along with a parse error of an 
unassociated elst statement.  There are two fixes to this problem.  The 
first is to remove the semi-colon after the if statement:

if(x>y)
  exch(x,y)
else
  do_something();

This will expand to what you expect.  This is probably the least 
satisfactory fix.  The better fix would be to wrap the entire macro in a 
do-while loop, leaving the semicolon in place (i.e. this construct allows 
a macro call to be treated like a function).

#define exch(x,y)  do{int tmp; tmp=x; x=y; y=tmp;}while(0)

if(x>y)
  do{int tmp; tmp=x; x=y; y=tmp;}while(0);
else
 ...


You should be aware that while macros are powerful, they can be tricky to 
use.  As an example, consifer what would happen with this statement

exch(a, b+4)

George

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

end of thread, other threads:[~2004-09-28  6:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-26 14:32 parse error Ankit Jain
2004-09-25  6:29 ` Elias Athanasopoulos
2004-09-28  6:14   ` Jan-Benedict Glaw
2004-09-26 22:51     ` Elias Athanasopoulos
  -- strict thread matches above, loose matches on Subject: below --
2004-09-27 15:58 Huber, George K RDECOM CERDEC STCD SRI

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 NNTP newsgroup(s).