| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 818 人关注过本帖
标题:[原创]RegExp DEMO version -- [2]最核心函数代码(下) -- int dealState( ...
只看楼主 加入收藏
rabbit5455
Rank: 2
等 级:论坛游民
帖 子:123
专家分:25
注 册:2004-4-14
收藏
 问题点数:0 回复次数:0 
[原创]RegExp DEMO version -- [2]最核心函数代码(下) -- int dealState(SymbolT
===============================================
新版本所有链接如下:
RegExp DEMO version -- [1]正则表达式 TO NFA表格 -- 词法分析代码
RegExp DEMO version -- [2]最核心函数代码(上) -- int dealState(SymbolType symboltype)
RegExp DEMO version -- [2]最核心函数代码(中) -- int dealState(SymbolType symboltype)
RegExp DEMO version -- [2]最核心函数代码(下) -- int dealState(SymbolType symboltype)
RegExp DEMO version -- [3][regexp.h]最核心数据类型
===============================================

case END_REGEXP:
while_notfinish = 0; /*一会儿退出循环*/
printf("debug: end scan the regexp:\n\t\"%s\"\n", g_strRegExp);
/*做一次状态机结束操作*/
case OR_MACHINE_END:
if (symboltype == OR_MACHINE_END) {
/*将g_ele_ary[]连接到OR_MACHINE中*/
for (OR_MACHINE_i=0; OR_MACHINE_i<sizeof(g_ele_ary); OR_MACHINE_i++) {
if (g_mstk->topM->not_mode == '^') {
/*否定模式*/
if (g_ele_ary[OR_MACHINE_i] == 0) {
setdestState(g_mstk->topM->state_start, OR_MACHINE_i, g_mstk->topM->state_end);
}
} else {
if (g_ele_ary[OR_MACHINE_i] == 1) {
setdestState(g_mstk->topM->state_start, OR_MACHINE_i, g_mstk->topM->state_end);
}
}
}
g_st->curState = g_mstk->topM->state_end;
}
case AND_MACHINE_END:
/*关闭一个子状态机,右括号: ')'*/
/*得到当前状态机参数*/
pmnew = g_mstk->topM;
/*将状态机的结束状态加入状态表*/
appendStateTable(g_st, pmnew->state_end);
/*结束当前状态机: 最后一个状态连到状态机的结束状态*/
setdestState(g_st->curState, g_st->eleCount, pmnew->state_end);
g_st->curState = pmnew->state_end; /*移动到新的状态*/
if (symboltype != END_REGEXP) {
/*如果表达式没有结束,向后看一个词,判断是不是重复运算,
如果是则执行相应的重复运算,否则继续解析表达式*/
getSymbol();
}
switch (g_symbol_type) {
case REPEAT_ZERO_MORE:
/*(A*)*/
/*将状态机开始和结束的状态做epsilon连接*/
setdestState(pmnew->state_start, g_st->eleCount, pmnew->state_end);
setdestState(pmnew->state_end, g_st->eleCount, pmnew->state_start);
printf("debug: (A*)ZEOR_MORE\tstart: %x | end: %x\n", pmnew->state_start, pmnew->state_end);
break;
case REPEAT_ZERO_ONCE:
/*(A?)*/
setdestState(pmnew->state_start, g_st->eleCount, pmnew->state_end);
printf("debug: (A?)ZEOR_ONCE\n");
break;
case REPEAT_ONCE_MORE:
/*(A+)*/
setdestState(pmnew->state_end, g_st->eleCount, pmnew->state_start);
printf("debug: (A+)ONCE_MORE\n");
break;
case REPEAT_RANGE_MN:
/*(A{m,n})*/
if (g_repeat_m <= 0) {
/*处理{0,n}的情况,把刚刚的A变成(A?)*/
setdestState(pmnew->state_start, g_st->eleCount, pmnew->state_end);
/*重复n-1次(A?)*/
pst_optional = repeatMachine_optional(g_repeat_n-1, pmnew);
if (pst_optional != NULL) {
g_st = joinStateTable(g_st, pst_optional);
pst_optional->head = NULL;
pst_optional->tail = NULL;
pst_optional->curState = NULL;
destroyStateTable(pst_optional);
}
} else {
pst_needed = repeatMachine_needed(g_repeat_m-1 , pmnew);
pst_optional = repeatMachine_optional(g_repeat_n-g_repeat_m, pmnew);
if (pst_needed != NULL) {
g_st = joinStateTable(g_st, pst_needed);

pst_needed->head = NULL;
pst_needed->tail = NULL;
pst_needed->curState = NULL;
destroyStateTable(pst_needed);
}
if (pst_optional != NULL) {
g_st = joinStateTable(g_st, pst_optional);
pst_optional->head = NULL;
pst_optional->tail = NULL;
pst_optional->curState = NULL;
destroyStateTable(pst_optional);
}
}
printf("debug: type -> %d\t| m -> %d | n -> %d\n", symboltype, g_repeat_m, g_repeat_n);
break;
case REPEAT_RANGE_M_MORE:
/*(A{m,})*/
pst_needed = repeatMachine_needed(g_repeat_m-1, pmnew);
if (pst_needed != NULL) {
g_st = joinStateTable(g_st, pst_needed);
pst_needed->head = NULL;
pst_needed->tail = NULL;
pst_needed->curState = NULL;
destroyStateTable(pst_needed);
}
/*做一个(A*)*/
psnew = g_st->curState; /*保存当前状态*/
pst_optional = repeatMachine_optional(1, pmnew); /*做一个(A?)*/
if (pst_optional != NULL) {
g_st = joinStateTable(g_st, pst_optional);
pst_optional->head = NULL;
pst_optional->curState = NULL;
pst_optional->tail = NULL;
destroyStateTable(pst_optional);
}
psnew = psnew->next; /*最后一次重复运算的开始状态*/
setdestState(psnew, g_st->eleCount, g_st->curState); /*修改成(A*)*/
printf("debug: type -> %d\t| m -> %d | n -> max_int\n", symboltype, g_repeat_m);
break;
case REPEAT_RANGE_M:
/*(A{m})*/
pst_needed = repeatMachine_needed(g_repeat_m-1, pmnew);
if (pst_needed != NULL) {
g_st = joinStateTable(g_st, pst_needed);
pst_needed->head = NULL;
pst_needed->tail = NULL;
pst_needed->curState = NULL;
destroyStateTable(pst_needed);
}
printf("debug: type -> %d\t| m -> %d\n", symboltype, g_repeat_m);
break;
default:
/*不是重复运算符,从堆栈中弹出当前状态机,然后重新循环*/
pmnew = popMachine(g_mstk);
destroyMachine(pmnew);
return 0;
}/*重复运算符 -- 结束*/

pmnew = popMachine(g_mstk);
destroyMachine(pmnew);
break;
case UNKNOWN:
while_notfinish = 0;
printf("debug: UNKNOWN -- ######################");
break;
default:
printf("debug: type -> %d\t| char -> %c\n", symboltype, g_symbol_charvalue);
break;
}

return 1; /*通知调用者传递下一个符号,即:调用get_symbol()*/
}
========================================================

[此贴子已经被作者于2007-6-3 0:17:13编辑过]

搜索更多相关主题的帖子: int DEMO RegExp SymbolT dealState 
2007-06-02 23:06
快速回复:[原创]RegExp DEMO version -- [2]最核心函数代码(下) -- int dealS ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025248 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved