資源簡介
代碼相對簡單;
c語言實現;
正則表達式轉換為nfa;
代碼片段和文件信息
#include
#include?
#define??ok???1
#define??error?0
#define??MAXREGLUARLONG?40
#define??MAXSTATELONG??40
#define??MAXCAHRSLONG???40??
typedef??int?state;
int?iCurrentState=0;???//初態以1開始
int?iPreState=0;
int?iLastForkState=0;
int?iForkState=0;
int?iMaxState=0;
char?cRegluarSting[MAXREGLUARLONG];???????//輸入的正規式字符串
char?cCharSet[MAXCAHRSLONG]; ??//字符集
int??iStateMatrix[MAXSTATELONG][MAXCAHRSLONG];??//狀態轉換矩陣
state?vStoreRegluarSting()//把字符串讀入一個緩沖區中
{
scanf(“%s“cRegluarSting);
return?ok;
}
state?vPreProcessRegluarSting()//對字符串進行預處理去掉字符串里面的對分析不產生影響
{
int?i=0;
while(cRegluarSting[i]!=‘\0‘)
{
if(cRegluarSting[i]==‘*‘)
{
int?j=i+1;
while(cRegluarSting[j-1]!=‘\0‘)
{?
cRegluarSting[j-1]=cRegluarSting[j++];
}
}
????i++;
}
return?ok;
}
void?vConstructStateMatrix(char?cCharint?istate)//構造狀態轉換矩陣
{
for(int?i=0;cCharSet[i]!=‘\0‘;i++)
if(cChar==cCharSet[i])
break;
cCharSet[i]=cChar;
iStateMatrix[iPreState][i]=istate;
}
void?vAanalyseRegluarSting()//對字符串進行從左到右的分析與處理
{
int?i=0;
for(i=0;cRegluarSting[i]!=0;i++)
{
if(cRegluarSting[i]==‘(‘)??//NFA出現開始分叉情況
{
iForkState=iCurrentState;
int?iTheFirstl=0;
int?iCharNumBeforl=0;
while(cRegluarSting[i]!=‘)‘)
{
i++;
if(isalpha(cRegluarSting[i]))
{
if(cRegluarSting[i+1]==‘)‘)
iCurrentState=iLastForkState;
else
iCurrentState++;
iCharNumBeforl++;
vConstructStateMatrix(cRegluarSting[i]iCurrentState);
iPreState=iCurrentState;
if(iCurrentState>iMaxState)
iMaxState=iCurrentState;
}
if(cRegluarSting[i]==‘|‘)
{
iPreState=iForkState;
if(iTheFirstl==0)
{
iLastForkState=iCurrentState;
iTheFirstl++;
}
- 上一篇:簡易C語言詞法分析程序
- 下一篇:Linux 用C語言實現shell
評論
共有 條評論