今天继续研究代码解析的算法
这个是算法流程图
有图解可能更直观一点;
以下是c#源码:
1
using System;
2
using System.IO;
3
using System.Text;
4
using System.Windows.Forms;
5
using System.Collections;
6
7
namespace CodeFormatter {
8
/// <summary>
9
/// CodeFormatterFactory 的摘要说明。
10
/// c 代码解析,不支持中文
11
/// </summary>
12
public class CodeFormatterFactory {
13
/*源代码*/
14
private string sourceCode = "";
15
16
/*C语言所有关键字,共32个*/
17
ArrayList KeyWordList = new ArrayList();
18
19
/*运算、限界符*/
20
ArrayList LimitList = new ArrayList();
21
22
/*常量表*/
23
ArrayList ConstList = new ArrayList();
24
25
/*标识符*/
26
ArrayList IdentifierList = new ArrayList();
27
28
/*输出*/
29
ArrayList OutputList = new ArrayList();
30
31
public CodeFormatterFactory() {
32
//
33
// TODO: 在此处添加构造函数逻辑
34
//
35
init();
36
}
37
38
public string SourceCode{
39
get{return this.sourceCode;}
40
set{this.sourceCode =value;}
41
}
42
43
public string ParseMessages{
44
get{
45
string pm = "";
46
47
IEnumerator ie = this.OutputList.GetEnumerator();
48
while ( ie.MoveNext() )
49
pm += ie.Current.ToString() + "
";
50
return pm;
51
}
52
}
53
54
private void init() {
55
/*C语言所有关键字,共32个*/
56
string[] key=new string[]{" ","auto","break","case","char","const","continue","default","do","double",
57
"else","enum","extern","float","for","goto","if","int","long","register",
58
"return","short","signed","sizeof","static","struct","switch","typedef",
59
"union","unsigned","void","volatile","while"};
60
/*运算、限界符*/
61
string[] limit=new string[]{" ","(",")","[","]","->",".","!","++","--","&","~",
62
"*","/","%","+","-","<<",">>","<","<=",">",">=","==","!=","&&","||",
63
"=","+=","-=","*=","/=",",",";","{","}","#","_","'"};
64
65
this.KeyWordList.Clear();
66
this.KeyWordList.TrimToSize();
67
for(int i=1;i<key.Length;i++)
68
this.KeyWordList.Add(key[i]);
69
70
this.LimitList.Clear();
71
this.LimitList.TrimToSize();
72
for(int i=1;i<limit.Length;i++)
73
this.LimitList.Add(limit[i]);
74
75
this.ConstList.Clear();
76
this.ConstList.TrimToSize();
77
78
this.IdentifierList.Clear();
79
this.IdentifierList.TrimToSize();
80
81
this.OutputList.Clear();
82
this.OutputList.TrimToSize();
83
}
84
85
/*******************************************
86
* 十进制转二进制函数
87
*******************************************/
88
private string dtb(string buf){
89
int[] temp= new int[20];
90
string binary = "";
91
int val=0,i=0;
92
93
/*先将字符转化为十进制数*/
94
try{
95
val = Convert.ToInt32(buf);
96
}catch{
97
val = 0;
98
}
99
100
if(val==0) {
101
return(val.ToString());
102
}
103
104
i=0;
105
while(val!=0) {
106
temp[i++]=val%2;
107
val/=2;
108
}
109
110
binary = "";
111
for(int j=0;j<=i-1;j++)
112
binary += (char)(temp[i-j-1]+48);
113
114
return(binary);
115
}
116
117
/*******************************************
118
* 根据不同命令查表或造表函数
119
*******************************************/
120
private int find(string buf,int type,int command){
121
int number=0;
122
string temp;
123
124
IEnumerator ie = null;
125
ArrayList al = null;
126
switch(type){
127
case 1://关键字表
128
ie = this.KeyWordList.GetEnumerator();
129
break;
130
case 2://标识符表
131
ie = this.IdentifierList.GetEnumerator();
132
break;
133
case 3://常数表
134
ie = this.ConstList.GetEnumerator();
135
break;
136
case 4://运算、限界符表
137
ie = this.LimitList.GetEnumerator();
138
break;
139
}
140
141
if(ie!=null)
142
while (ie.MoveNext()){
143
temp = ie.Current.ToString();
144
if(temp.Trim().ToLower()==buf.Trim().ToLower()){
145
return number;
146
}
147
number ++;
148
}
149
150
if(command==1){
151
/*找不到,当只需查表,返回0,否则还需造表*/
152
return 0;
153
}
154
155
switch(type){
156
case 1: al = this.KeyWordList;break;
157
case 2: al = this.IdentifierList;break;
158
case 3: al = this.ConstList;break;
159
case 4: al = this.LimitList;break;
160
}
161
if(al!=null)
162
al.Add(buf);
163
164
return number + 1;
165
}
166
/*******************************************
167
* 数字串处理函数
168
*******************************************/
169
private void cs_manage(string buffer){
170
string binary = dtb(buffer);
171
int result = find(binary,3,2);
172
this.OutputList.Add(String.Format("{0} 3 {1}",buffer,result));
173
}
174
175
/*******************************************
176
* 字符串处理函数
177
*******************************************/
178
private void ch_manage(string buffer) {
179
int result = find(buffer,1,1);
180
if(result!=0){
181
this.OutputList.Add(String.Format("{0} 1 {1}",buffer,result));
182
}else{
183
result = find(buffer,2,2);
184
this.OutputList.Add(String.Format("{0} 2 {1}",buffer,result));
185
}
186
}
187
188
/*******************************************
189
* 出错处理函数
190
*******************************************/
191
private void er_manage(char error,int lineno) {
192
this.OutputList.Add(String.Format("错误关键字: {0} ,所在行: {1}",error,lineno));
193
}
194
195
/*******************************************
196
* 转换Char数组为string
197
******************************************/
198
private string joinString(char[] array,int Length){
199
string s = "";
200
if(array.Length>0)
201
for(int i=0;i<Length;i++){
202
if(array[i]!='

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202
