错误#29期待一个表达式

时间:2015-02-02 04:46:13

标签: c greenhills microc

以下是2维数组的代码抛出错误#29期望表达式。

typedef  enum 
{
    BATTERY_POW = 0,
    USB_POW = 1,
    END_STATE = 2
} BMTState_e;   

typedef enum //event enums
{  
    NO_EVENT = 0,
    BOOT_EVENT =1,
    //I/O events
    POW_GOOD_LOW =2,
    POW_GOOD_HIGH = 3,
    VBUS_POW_LOW = 4,
    VBUS_POW_HIGH =5
}BMTEvent_e;

结构定义为

typedef struct  //state machine definition
{
    void (*funcPtr)();
    BMTState_e nextState;
}BMTAction_t;
BMTState_e BMTGlobal_State ; //global state

该功能定义为

void BMTTest()
{
 //do nothing for time being
}
BMTAction_t  action[END_STATE][END_EVENT]={
   [BATTERY_POW][NO_EVENT]    = {BMTTest,BATTERY_POW}
   [BATTERY_POW][BOOT_EVENT]    = {BMTTest,BATTERY_POW},
   [BATTERY_POW][POW_GOOD_LOW]  = {BMTTest,USB_POW},
   [BATTERY_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW}
   [BATTERY_POW][VBUS_POW_LOW]  = {BMTTest,BATTERY_POW},
   [BATTERY_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW},
   [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW_}
   [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW},
   [USB_POW][POW_GOOD_LOW]  = {BMTTest,USB_POW},
   [USB_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW}
   [USB_POW][VBUS_POW_LOW]  = {BMTTest,BATTERY_POW},
   [USB_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW}
}; 

void BMT_HandleEvent(BMTEvent_e event)
{
  BMTAction_t stAction;


   if(event != NO_EVENT)
   {
      stAction.funcPtr = action[BMTGlobal_State][event].funcPtr;
      stAction.nextState = action[BMTGlobal_State][event].nextState;
      printf("current State =%d, event = %d, nextState = %d",BMTGlobal_State, event,stAction.nextState );

      if(NULL!= stAction.funcPtr)
        stAction.funcPtr();
        BMTGlobal_State = stAction.nextState;
    }

}

int main()
{
BMTEvent_e = BOOT_EVENT;
if(retVal)
    {
    BMTGlobal_State = BATTERY_POW;
    }
else // PG is low so check VBUS signal 
    {
retVal = GPIO_Read_Pin(USB_VBUS_PWR_PIN);
if(retVal)
        {
        BMTGlobal_State = USB_POW;
        }
else
        {
        BMTGlobal_State = CRADLE_POW;
        }
    }
event = BOOT_EVENT;
while (1)
    {
    BMT_HandleEvent(event);
    } 
}

想法是根据收到的事件执行状态机。 2D数组列出当前状态和状态的所有可能事件,一旦接收到事件,将调用函数指针并且状态将转换到下一状态。

我正在使用MicroC / OS2和GreenHills编译器/工具。 非常感谢您的回复。

我遵循了基思的建议

BMTAction_t  action[END_STATE][END_EVENT] = {
                  {{NULL,BATTERY_POW}}, //0
                  {{BMTProcess_BatteryPowBoot,BATTERY_POW}},//   1
                  {{BMTProcess_PowGoodLow,BATTERY_POW}},//2
                  {{BMTProcess_PowGoodHigh,BATTERY_POW}}, //3
                  {{BMTProcess_VBUSPowerLow,BATTERY_POW}}, //4
                  {{BMTProcess_VBUSPowerHigh,BATTERY_POW}},//5 
};

编译器在行

处给出错误
"{{BMTProcess_PowGoodHigh,BATTERY_POW}}, //3" 
"error # 146 too many initializer values"

4 个答案:

答案 0 :(得分:4)

这不是指定初始值设定项的正确语法。您一次只能指定一个索引。

我错了;允许多个指示符 。实际上,在我添加一些声明后,您的代码使用gcc -std=c99 -pedantic -Wall -Wextra编译时没有错误。

typedef enum { BATTERY_POW, END_STATE } BMTState_e;
typedef enum { BOOT_EVENT, POW_GOOD_LOW, POW_GOOD_HIGH, END_EVENT } BMTEvent_e;

void BMTTest(void);

typedef struct  //state machine definition
{
    void (*funcPtr)();
    BMTState_e nextState;
}BMTAction_t;

BMTAction_t  action[END_STATE][END_EVENT]={
    [BATTERY_POW][BOOT_EVENT]    = {BMTTest,BATTERY_POW},
    [BATTERY_POW][POW_GOOD_LOW]  = {BMTTest,BATTERY_POW},
    [BATTERY_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW}
}; 

您的编译器很可能不了解指定的初始值设定项。 (它们被1999年标准添加到语言中,即使在今天,并非所有编译器都支持它们。)

如果没有,则需要删除括号中的表达式(并确保按正确的顺序排列元素)。可能类似于:

BMTAction_t  action0[END_STATE][END_EVENT] = {
    {{ BMTTest, BATTERY_POW }},
    {{ BMTTest, BATTERY_POW }},
    {{ BMTTest, BATTERY_POW }}
};

答案 1 :(得分:0)

基思的回答帮助我弄清楚了正确的初始化。我必须列出{{}}内给定状态的所有可能事件和相关函数指针。

这就像初始化row [0] [0]和row [0] [1]的元素一样,我必须列出顶级双花括号内的所有元素,如

 { **{{**values/function/events associated with row [0][0],{values/function/events associated with row [0][1] }}, 
//now do the same for row 1. 
**{{**values/function/events associated with row [1][0],{values/function/events associated with row [1][1] **}}** }; //end of the array

BMTAction_t  action[END_STATE][END_EVENT] = {
                 //STATE = BATTERY_POW
                  {{NULL,BATTERY_POW}, //0
                  {BMTProcess_BatteryPowBoot,BATTERY_POW},//   1
                  {BMTProcess_PowGoodLow,BATTERY_POW},//2
                  {BMTProcess_PowGoodHigh,BATTERY_POW}, //3
                  {BMTProcess_VBUSPowerLow,BATTERY_POW}, //4
                  {BMTProcess_VBUSPowerHigh,BATTERY_POW}},//5 
                  //STATE = USB_POW
                  {{NULL,USB_POW}, //0
                  {BMTProcess_BatteryPowBoot,USB_POW},//   1
                  {BMTProcess_PowGoodLow,USB_POW},//2
                  {BMTProcess_PowGoodHigh,USB_POW}, //3
                  {BMTProcess_VBUSPowerLow,USB_POW}, //4
                  {BMTProcess_VBUSPowerHigh,USB_POW}}//5 
                };

答案 2 :(得分:0)

如果使用-c99选项,那么您正在使用的初始化将与greenhills编译器一起使用。

我必须对您的代码进行一些小的更改才能使其编译:

  • 添加END_EVENT枚举
  • action声明的项目之间添加一些逗号。
  • 修正了错误USB_POW_USB_POW

以下是修改过的代码:

#include <stdio.h>
typedef enum {
    BATTERY_POW = 0,
    USB_POW = 1,
    END_STATE = 2
} BMTState_e;

typedef enum {
    NO_EVENT = 0,
    BOOT_EVENT =1,
    //I/O events
    POW_GOOD_LOW =2,
    POW_GOOD_HIGH = 3,
    VBUS_POW_LOW = 4,
    VBUS_POW_HIGH =5,
    END_EVENT = 6

} BMTEvent_e;

typedef struct {
    void (*funcPtr)();
    BMTState_e nextState;
} BMTAction_t;

void BMTTest()
{
 //do nothing for time being
}
BMTAction_t  action[END_STATE][END_EVENT]={
[BATTERY_POW][NO_EVENT]    = {BMTTest,BATTERY_POW},
[BATTERY_POW][BOOT_EVENT]    = {BMTTest,BATTERY_POW},
[BATTERY_POW][POW_GOOD_LOW]  = {BMTTest,USB_POW},
[BATTERY_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW},
[BATTERY_POW][VBUS_POW_LOW]  = {BMTTest,BATTERY_POW},
[BATTERY_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW},
[USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW},
[USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW},
[USB_POW][POW_GOOD_LOW]  = {BMTTest,USB_POW},
[USB_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW},
[USB_POW][VBUS_POW_LOW]  = {BMTTest,BATTERY_POW},
[USB_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW}
};
int main()
{
    return 1;
}

这里是没有-c99的编译器输出:

$ ccppc test.c
"test.c", line 30: error #29: expected an expression
  [BATTERY_POW][NO_EVENT]    = {BMTTest,BATTERY_POW},
  ^

"test.c", line 31: error #29: expected an expression
  [BATTERY_POW][BOOT_EVENT]    = {BMTTest,BATTERY_POW},
  ^

"test.c", line 32: error #29: expected an expression
  [BATTERY_POW][POW_GOOD_LOW]  = {BMTTest,USB_POW},
  ^

"test.c", line 33: error #29: expected an expression
  [BATTERY_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW},
  ^

"test.c", line 34: error #29: expected an expression
  [BATTERY_POW][VBUS_POW_LOW]  = {BMTTest,BATTERY_POW},
  ^

"test.c", line 35: error #29: expected an expression
  [BATTERY_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW},
  ^

"test.c", line 36: error #29: expected an expression
   [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW},
   ^

"test.c", line 37: error #29: expected an expression
  [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW},
  ^

"test.c", line 38: error #29: expected an expression
  [USB_POW][POW_GOOD_LOW]  = {BMTTest,USB_POW},
  ^

"test.c", line 39: error #29: expected an expression
  [USB_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW},
  ^

"test.c", line 40: error #29: expected an expression
  [USB_POW][VBUS_POW_LOW]  = {BMTTest,BATTERY_POW},
  ^

"test.c", line 41: error #29: expected an expression
  [USB_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW}
  ^

使用-c99

$ ccppc -c99 test.c

答案 3 :(得分:0)

您确实意识到您的代码包含以下内容:

   [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW_}
   [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW},

即使我对此感到困惑。你的意思是第一个是NO_EVENT吗?