syntax error using enum{}

When I compile my code, the compiler says “syntax error” for one of my header files and points to the line shown below:

enum
{
  START,     <==  this line causes syntax error
  IN_PROGRESS,
  DRIVING,
  TURNING,
  COMPLETE
};

When I rename the variable START to something else like FOOSTART, the error moves down to the next line:

enum
{
  FOOSTART,
  IN_PROGRESS,  <===  now the error is here
  DRIVING,
  TURNING,
  COMPLETE
};

I figured there must be a name conflict (since I’m using bits of Kevin’s code), so I Searched in Project Files… for “IN_PROGRESS”, but it is only defined in this one place. No conflict. As I modify each variable in the list, the error moves down the list.

I did a Build All (CTRL-F10) to make sure there wasn’t a leftover symbol table. No difference.

As a matter of practicality I renamed all the variables to start with an _underscore, and now it builds just fine. But it bugs me that I can’t explain this error.

Any ideas???

Here’s the entire header file:

#ifndef SCRIPTING_H
#define SCRIPTING_H


#define MAX_SCRIPTS 32 // more than this is an error from parseScript()


struct scriptCommand
{
  int command;
  long int parm_1;
  int parm_2;
  int parm_3;
};

// List of commands
//
enum
{
  S_NAME,    // marks the start of a script
  S_END,     // marks the end of a script
  S_DRIVE,   // <distance>, <maxSpeed>, <maxAccel>
  S_TURN,    // <heading>, <maxSpeed>, <maxAccel>
  S_STOP,
  S_WAIT,    // <counts>
  S_SET_ARM_POSITION,  // <angle>, <speed>, <tolerance>
  S_LIST_END
};


// List of all possible command states 
// Not all are used in every command.
enum
{
  START,
  IN_PROGRESS,
  DRIVING,
  TURNING,
  COMPLETE
};



int parseScript();
int startScript( int scriptCode );
int executingScript( );  // returns 1 if a script is executing.

void backAndForth();
void driveFromStartToLoading();
void knockOffHangingTetra();
void setArmPosition();

unsigned char sDrive();
unsigned char sTurn();


#endif

For one, enums aren’t part of the C standard (unless they were add in ISO C99). Second, you have to give the enum a name. I believe the syntax is:
enum <name> {
<enum values>
};

Hope this is helpful.

Matt

According to my copy of K & R Second Edition (ANSI C Edition) enumerations are defined in Appendix A (Reference Manual) section A8.4. The identifier (what you show as <name>) is indicated as optional.

Besides, if enum{} wasn’t allowed, just changing the names of the identifiers would not have eliminated the syntax error. But it did.

I’m still confused.

All of those identifiers are #defined in robot.h:

/* Command States */
/* These states are used by the commands to keep track of the robot's 
   progress in completing each command. */
#define START              1
#define IN_PROGRESS        2
#define COMPLETE           3
#define TURNING            4
#define DRIVING            5
#define STOPPED            6

I think this explains the mystery.

I figured there must be a name conflict (since I’m using bits of Kevin’s code), so I Searched in Project Files… for “IN_PROGRESS”, but it is only defined in this one place. No conflict. As I modify each variable in the list, the error moves down the list.

“Find in Project Files” only searches files it knows about in the project. (i.e. listed in the project window), whereas, the compiler couldn’t care less about the files in the IDE’s list. It cares about #include statements only.

I think that’s it. I had re-written robot.[h,c] but I think I have a residual #include “robot.h” in user_routines.c. I’ll have to double check tonight, but I’m pretty sure that’s it.

You’d think the compiler could have said something a little more helpful than “syntax error”…

Thanks!

-Norm

I can only agree with you about the error statement. I wasted hours trying to decipher these, while the problem was mainly as simple as giving a name to a parameter similiar to an existing macro, or ; being where it’s not needed and vice versa :smiley:

That was indeed the error.

Yeah, the compiler is low on helpfulness. I accidentally created a local variable that shadowed a global variable but it gave no warning.

Is there a way to increase the number of warnings for the compiler? Like gcc -Wall?

Agreed, but keep in mind what was happening: your “enum” values were being replaced with the #defines from robot.h, so to the compiler your enum ended up looking like this:


enum
{
  1,
  2,
  3,
  4,
  5
};

While that doesn’t excuse the cryptic error generated, it might at least make sense why the compiler reported a syntax error (because it is!).

You can set the message level: Project | Build Options | Project

Then on the MPLAB C18 tab, set the Diagnostic Level=“Errors, Warnings, and Messages”

I make no guarantee, however, that it will catch these problems.