1 #include<stdio.h>
2 #include <ctype.h>
3 #define ok 1
4 #define error 0
5 #define MAXREGLUARLONG 40
6 #define MAXSTATELONG 40
7 #define MAXCAHRSLONG 40
8 typedef int state;
9 int iCurrentState=0; //初态以1开始
10 int iPreState=0;
11 int iLastForkState=0;
12 int iForkState=0;
13 int iMaxState=0;
14 char cRegluarSting[MAXREGLUARLONG]; //输入的正规式字符串
15 char cCharSet[MAXCAHRSLONG]; //字符集
16 int iStateMatrix[MAXSTATELONG][MAXCAHRSLONG]; //状态转换矩阵
17 state vStoreRegluarSting()//把字符串读入一个缓冲区中
18 {
19 scanf("%s",cRegluarSting);
20 return ok;
21 }
22 state vPreProcessRegluarSting()
23 //对字符串进行预处理,去掉字符串里面的对分析不产生影响
24 {
25 int i=0;
26 while(cRegluarSting[i]!='\0')
27 {
28 if(cRegluarSting[i]=='*')
29 {
30 int j=i+1;
31 while(cRegluarSting[j-1]!='\0')
32 {
33 cRegluarSting[j-1]=cRegluarSting[j++];
34 }
35 }
36 i++;
37 }
38 return ok;
39 }
40 void vConstructStateMatrix(char cChar,int istate)//构造状态转换矩阵
41 {
42 int i;
43 for(i=0;cCharSet[i]!='\0';i++)
44 if(cChar==cCharSet[i])
45 break;
46 cCharSet[i]=cChar;
47 iStateMatrix[iPreState][i]=istate;
48 }
49 void vAanalyseRegluarSting()//对字符串进行从左到右的分析与处理
50 {
51 int i=0;
52 for(i=0;cRegluarSting[i]!=0;i++)
53 {
54 if(cRegluarSting[i]=='(') //NFA出现开始分叉情况
55 {
56 int iTheFirstl=0;
57 int iCharNumBeforl=0;
58 iForkState=iCurrentState;
59 while(cRegluarSting[i]!=')')
60 {
61 i++;
62 if(isalpha(cRegluarSting[i]))
63 {
64 if(cRegluarSting[i+1]==')')
65 iCurrentState=iLastForkState;
66 else
67 iCurrentState++;
68 iCharNumBeforl++; vConstructStateMatrix(cRegluarSting[i],iCurrentState);
69 iPreState=iCurrentState;
70 if(iCurrentState>iMaxState)
71 iMaxState=iCurrentState;
72 }
73 if(cRegluarSting[i]=='|')
74 {
75 iPreState=iForkState;
76 if(iTheFirstl==0)
77 {
78 iLastForkState=iCurrentState;
79 iTheFirstl++;
80 }
81 if(iCharNumBeforl==1&&cRegluarSting[i+2]=='|')
82 iCurrentState=iForkState;
83 iCharNumBeforl=0;
84 }
85 if(cRegluarSting[i]==')')
86 {
87 iPreState=iForkState=iLastForkState;
88 iCurrentState=iMaxState;
89 }
90 }
91 }
92 else
93 {
94 if(isalpha(cRegluarSting[i]))
95 {
96 iCurrentState++; vConstructStateMatrix(cRegluarSting[i],iCurrentState);
97 iPreState=iCurrentState;
98 if(iCurrentState>iMaxState)
99 iMaxState=iCurrentState;
100 }
101 }
102 }
103 }
104 void vPrintfStateProjectFunction()
105 {
106 int icCharSetPointer;
107 int iPreStatePointer;
108 for
109 (iPreStatePointer=0;iPreStatePointer<MAXSTATELONG;iPreStatePointer++)
110 for(icCharSetPointer=0;icCharSetPointer<MAXSTATELONG;icCharSetPointer++)
111 if(iStateMatrix[iPreStatePointer][icCharSetPointer]>0) printf("&(%d,%c)=%d\n",iPreStatePointer,cCharSet[icCharSetPointer],iStateMatrix[iPreStatePointer][icCharSetPointer]);
112 }
113 void vPrintfNfa()//输出NFA
114 {
115 int iStateNumble;
116 int i=0;
117 printf("NFA的形式为:(S,$,&,S0,F)\n\n以下为NFA的具体集合内容:\n\n");
118 printf("字符集$为:{");
119 while(cCharSet[i]!=0)
120 if(cCharSet[i+1]==0)
121 printf("%c",cCharSet[i++]);
122 else
123 printf("%c,",cCharSet[i++]);
124 printf("}\n");
125 printf("\n状态集S为:{");
126 for (i=0;i<=iMaxState;i++) {
127 if(i==iMaxState)
128 printf("%d",i);
129 else
130 printf("%d,",i);
131 }
132 printf("}\n\n");
133 vPrintfStateProjectFunction();
134 printf("\n初态集S0为:{0}\n\n");
135 printf("终态集F为:{%d}",iMaxState);
136 }
137 void main()
138 {
139 vStoreRegluarSting();
140 vPreProcessRegluarSting();
141 vAanalyseRegluarSting();
142 vPrintfNfa();
143 }
![](https://images2015.cnblogs.com/blog/809047/201512/809047-20151203192358049-1137617704.png)