Nasty IDE bug?

There seems to be nasty bug in the IDE that keeps a sketch from compiling even though the code in question is commented out.

The sketch at the bottom of this page http://moderndevice.com/AM/Hmwrk1_BlinkLEDs.html won't compile when pasted into either Mac or Windows Arduino IDE. It throws this error

error: expected unqualified-id before 'else' which looks like a bracket, semicolon or parenthesis is out of place. I tried commenting out sections with no success.

Then I pasted the whole thing in below the standard blink sketch - just to get the required setup and loop functions in there, and commented out the entirety of my sketch. Nada - same error. This is really puzzling - commented out code keeps sketch from compiling !?!

Another thing I tried is to comment out the else if statements - no compile. But if I cut out the statements - compiles fine. Whats the story here? Is the compiler confused about something in the comments?

Eventually, by pasting in sections a little at a time, I did get the sketch to compile (without ostensibly changing any code). I'm still looking for specific problem however.

Is this a known bug? Or some evil non-printing character that is new? Or maybe, embarrassingly - I am missing something simple? But why is commented-out code having any effect at all?

The problem with the sketch seems to be below the second else if statement.

I duplicated my results with both the Mac and Windows IDE and with pastes from Firefox and IE.

Paul

As a work-around, if you indent the else statments the sketch will compile. (I have no idea why)

Another odd thing is that in the code you first posted, the error was reported even though the problem code was commented out.

This skeletal code has the error even though the problem area is in a comment :-?.

void setup() // run once, when the sketch starts 
{ 
    ;
} 

void loop() // run over and over again 
{ 
  ;
} 

/*
void loop(){ 
   if (1){ 
       ;
   }
else if(0){ 
    ;
} 
} */

The indent does fix the problem. Weird huh? The sketch won’t autoformat though and throws a string of Java errors.

Maybe this is unrelated. I’m wondering about new IDE improvements with auto-format. Maybe a new bug was inadvertently introduced with the autoformat fixes in Arduino 0010

Thanks for the quick workaround mem! At least it proves I’m not losing my mind (although I’m sure some would debate that)

Paul

There seems to be nasty bug in the IDE that keeps a sketch from compiling even though the code in question is commented out.

Does it have anything to do with the "pre-processing" the IDE does when you compile? I know the regular-expression based parsing has caused issues before. Can you see what the actual source to be compiled looks like? (I assume it's in the temporary directory somewhere.)

--Phil.

Phil, the problem appears in the arduino build transformation , prior to it being sent to the compiler.

Below is the source from my post above after transforming and saved in the temp directory.

#include "WProgram.h"
else if(0);
void setup() // run once, when the sketch starts 
{ 
    ;
} 

void loop() // run over and over again 
{ 
  ;
} 

/*
void loop(){ 
   if (1){ 
       ;
   }
else if(0){ 
    ;
} 
} */ 


int main(void)
{
      init();
      setup();    
      for (;;)
            loop();        
      return 0;
}

The error is the else fragment placed on the second line of the file. This statement was extracted from the comment at the bottom of the source!

Ugh. It looks like it might be time for me to sit down with ANTLR (a parser) and get it do some proper generation of function prototypes instead of the regex hack we have now. Unless anyone has a better idea? Or there's a volunteer?

I have a suggestion that may help as a workaround for this, and perhaps also helps with problem of #includes ordered incorrectly (mentioned here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1198025665)

Why not put the auto generated prototypes in a separate file that is included at the top of the sketch. This enables experienced users to more easily see and control what is happening. And although the details of the file would be more or less invisible to those new to programming, more experienced programmers could (I hope) have an option to suppress the automatic creation of the prototypes to enable explicit management of the scope of function declarations.

The only obvious downside is the need to have a line like:

include “sketch.def”

in every sketch.

What I'd like to do is just be smart about where I insert the prototypes: e.g. after any #includes and type definitions, but before functions. Putting them in a separate file doesn't really help, since we still need to pick the right place to stick that #include.

I’ve mentioned the existing http://cproto.sourceforge.net/ before…

Putting them in a separate file doesn't really help, since we still need to pick the right place to stick that #include.

although it doesn't solve the problem, it does give the programmer much greater visibility over what is happening and preferably an opportunity to control what is in that file if desired.

westfw: thanks for the pointer. I'll try it out - I hope it doesn't get confused by the small bits of C++ in Arduino.

mellis, FYI here are two recent threads that reported problems with apparently valid sketches that couldn't compile. It appears both were caused by comments confusing the parser.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1200368717 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1200346291