谁能说下把正则式转换成NFA应该怎么建立数据结构,就是实现Thomson算法的数据结构

weiyiabout 2008-04-15 10:09:25
如题
谢谢
...全文
163 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyt31 2008-08-03
  • 打赏
  • 举报
回复
这种方法不可能构造正则表达式引擎。。
只能判断某个字符串是否匹配某个【预先给定】的正则表达式
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
有点难度哦
weiyiabout 2008-04-16
  • 打赏
  • 举报
回复
到NFA的各个结点,得用某个数据结构来表示吧?
C1053710211 2008-04-16
  • 打赏
  • 举报
回复
似乎不用数据结构,switch-case语句就够了
C1053710211 2008-04-16
  • 打赏
  • 举报
回复
节点就是case,每个case后的statement 就是一个状态,case之间的转化模拟有穷自动机
举个例子,这个是以前做过的题
给定一文本文件,存储内容如下:
ID,TYPE
a*bc,001
a*v,002
a?cd,003
a*bbc,004
其中,ID列为正则表达式。另外,给定一字符串s="abc",现在需要求出s所对应的TYPE,

#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#define LEN 1<<16
using namespace std;
char s[LEN],c,*lexeme_beginning;
char *token_beginning = s,*forward = s ;
string token;
int start,state,step,symbol_lex;
char nextchar()
{
return *forward++;
}
void retract(int pos)
{
forward--;
}
string token_nexttoken()
{
while(1)
{
switch(state)
{
case 0:
c=nextchar();
if(c=='a') state = 8;
else if(c=='b') state = 1;
else if(c=='c') state = 2;
else if(c=='v') state = 3;
else state =30;
break;
case 1:
c=nextchar();
if(c=='c') state = 4;
else if(c=='b') state = 5;
else state = 30;
break;
case 2:
c=nextchar();
if(c=='d') state = 7;
else state = 30;
break;
case 3:
if(*forward=='\0') return "002";
else state = 30;
break;
case 4:
if(*forward=='\0') return "001";
else state = 30;
break;
case 5:
c=nextchar();
if(c=='c') state = 6;
else state = 30;
break;
case 6:
if(*forward=='\0') return "004";
else state = 30;
break;
case 7:
if(*forward=='\0') return "003";
else state = 30;
break;
case 8:
c=nextchar();
if(c=='a') state = 9;
else if(c=='b') state = 1;
else if(c=='c') state = 2;
else if(c=='v') state = 3;
else state = 30;
break;
case 9:
c=nextchar();
if(c=='a') state = 9;
else if(c=='b') state = 1;
else if(c=='v') state = 3;
else state = 30;
break;
case 30:
return "[false]";
}
}
}
void Init()
{
}
int main()
{
int len;
string tmp;
while(scanf("%s",s)!=EOF)
{
state = 0;
lexeme_beginning = s;
forward = s;
len = strlen(s);
while(forward!=s+len)
{
start = 0;
state = 0;
token_beginning = lexeme_beginning;
tmp = token_nexttoken();
cout<<tmp.c_str()<<endl;
if(state == 30 ) break;
}
}
system("pause");
return 0;
}

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧