我首先定义了一个包含所有可能状态的enum
:
typedef enum {
Start = 0,
Build_Id = 1,
Identifier = 2,
Build_Num = 3,
Number = 4,
Error = 5
} State_type;
这是我目前的代码:
State_type analyzeData(State_type* currState, char c);
int main(int argc, char** argv) {
State_type currState = 0;
for (int i = 1; i < strlen(*argv); ++i) {
analyzeData(&currState, *argv[i]);
}
}
State_type analyzeData(State_type* currState, char c) {
if (currState == 0) {
if (isblank(c)) {
*currState = (State_type) 0;
return *currState;
}
else if (isdigit(c)) {
*currState = (State_type) 3;
return *currState;
}
else if (isalpha(c)) {
*currState = (State_type) 1;
return *currState;
}
}
}
我的计划是基本上为所有其他可能状态使用一系列if-else语句。我想我是否正确地接近这一点有点困惑。我一直试图阅读其他FSM问题的答案,但没有任何意义。有人能指出我正确的方向吗?
答案 0 :(得分:3)
你定义一个列出你的状态的枚举 - 好!
typedef enum {
Start_state = 0,
Build_Id_state = 1,
Identifier_state = 2,
Build_Num_state = 3,
Number_state = 4,
Error_state = 5
} State_type;
稍微更改您的州过渡代码
int
main(int argc, char** argv) {
State_type currState = 0;
Action_t action;
char* p = *argv; char symbol;
int len = strlen(p);
//C-strings are zero-indexed
for (int i=0; i < len; ++i) {
action = analyzeData(&currState, classify(symbol=*p++));
switch(action) {
case None_act: break;
case Gather_act: //appropriate symbol gathering
case Emit_act: //handle ident/number print/save
case Stop_act: //appropriate behavior, e.g. i=len
...
}
}
}
构建一个包含这些条目的状态转换表:
typedef struct state_table_entry_s {
State_type state;
Transition_t trans; //could define as bit-field
State_type nextstate;
Action_t action; //semantic action
} state_table_entry_t;
定义状态转换表,清楚表明您尚未定义某些转换的行为。 (使表格成为二维的,您可以更快地处理状态和转换)
state_table_entry_t states[] = {
{Start_state, Letter_class, None_act, Build_Id}
,{Start_state, Digit_class, None_act, Build_Num}
,{Start_state, Blank_class, None_act, Start_state}
,{Start_state, Semicolon_class, Stop_act, Start_state}
,{Build_Id_state, Letter_class, Gather_act, Build_Id_state}
,{Build_Id_state, Digit_class, Gather_act, Build_Id_state}
,{Build_Id_state, Underscore_class, Gather_act, Build_Id_state}
,{Build_Id_state, Blank_class, None_act, Identifier_state}
,{Identifier_state, Blank_class, Emit_act, Start_state}
,{Build_Num_state, Digit_class, Gather_act, Build_Num_state}
,{Build_Num_state, Blank_class, None_act, Number_state}
,{Number_state, Blank_class, Emit_act, Start_state}
,{Stop_state, <any>, Error_act, Stop_state}
,{Error_state, <any>, None_act, Stop_state}
};
注意上面的“状态转换表”如何清楚地记录您的状态机?您可以(轻松地)从配置文件中加载此表吗?
停止。您是否为每个(状态X转换)对定义了(适当的)操作?
//States:
Start_state
Build_Id_state
Identifier_state
Build_Num_state
Number_state
Error_state
//Transitions:
Letter_class
Digit_class
Underscore_class
Blank_class
Semicolon_class
Other_class
对于上述内容,您需要定义状态转换类:
typedef enum {
Letter_class
,Digit_class
,Underscore_class
,Blank_class
,Semicolon_class
,Other_class
} Transition_t;
你需要定义你的行动:
typedef enum {
None_act
,Gather_act
,Emit_act
,Stop_act
,Error_act
} Action_t;
将您的字符/符号转换为其转换类(您可以使用ctype.h和isalpha(),isdigit()宏),
Transition_t classify(char symbol) {
Transition_t class = Other_class;
if (isblank(c)) {
return(class = Blank_class); break;
}
else if(isdigit(symbol)) {
return(class = Digit_class);
}
else if (isalpha(symbol)) {
return(class = Letter_class); break;
}
else {
switch(tolower(symbol)) {
case ' ':
return(class = Blank_class); break;
case '_':
return(class = Underscore_class); break;
case ';':
return(class = Semicolon_class); break;
default :
return(class = Other_class); break;
}
}
return(class = Other_class); break;
}
在状态表中找到匹配状态(可以使效率更高),并在转换表中进行匹配转换,然后采取语义操作,
Action_t
analyzeData(State_type& currState, Transition_t class) {
for( int ndx=0; ndx<sizeof(states)/sizeof(states[0]); ++ndx ) {
if( (states[ndx].state == currState)
&&. (states[ndx].trans == class) ) { //state match
semantic_action(states[ndx].action);
currState = states[ndx].nextState;
return(states[ndx].action);
}
}
}
您需要定义'semantic_action'功能,当然您需要“收集”您的输入,以便您可以在适当的操作时间执行输出。你的'emit_act'需要清理。
答案 1 :(得分:1)
使用switch语句(甚至转换表)可能会更好,但基本结构是相同的。
如果您使用枚举,请使用它。不要使用魔术数字。定义枚举的关键是能够使用有意义的名称而不是数字。
如果要返回新状态,使用输入输出参数绝对没有意义。使用原型
State_type analyzeData(State_type currState, char c);
/* Better would be int c. See below. */
然后典型的州案例可能是:
case Start:
if (isblank(c)) return Start;
else (isdigit(c)) return Build_Num;
else (isalpha(c)) return Build_Id;
else return Error;
另请注意,isalpha
和朋友可以使用int
,而不是char
。如果char
已签名(这是常见的)且值恰好为负数,则会导致未定义的行为。