Matrix Wall

词法分析器

一个词法分析器的demo。

源码

1
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int i, j;
int len;
int count;
int start_p;

int analyse(char *tmp, int start_p, FILE *fp_w)
{
while( isspace(tmp[start_p]) )
start_p++;

if( isalpha(tmp[start_p]) )
{
count = 0;
char tmp_str[1000] = {};

while( isalpha(tmp[start_p]) )
{
tmp_str[count] = tmp[start_p];
count++;
start_p++;
}
start_p--;

if( is_keyword(tmp_str, fp_w) ){
;
}
else
{
printf("<0, %s>\n", tmp_str);
fprintf(fp_w, "<0, %s>\n", tmp_str);
}
}
else{

if( isdigit(tmp[start_p]) )
{
count = 0;
char tmp_str[1000] = {};

while( isdigit(tmp[start_p]) )
{
tmp_str[count] = tmp[start_p];
count++;
start_p++;
}
start_p--;
printf("<1, %s>\n", tmp_str);
fprintf(fp_w, "<1, %s>\n", tmp_str);
}
else{

if( !isalpha(tmp[start_p]) && !isdigit(tmp[start_p]) )
{
count = 0;
char tmp_str[1000] = {};

if( !isalpha(tmp[start_p+1]) && !isdigit(tmp[start_p+1]) )
{
tmp_str[count] = tmp[start_p];
count++; start_p++;
tmp_str[count] = tmp[start_p];
if( is_symbol(tmp_str, fp_w) ){
;
}else{
count--; start_p--;
tmp_str[1] = '\0';
tmp_str[count] = tmp[start_p];
is_symbol(tmp_str, fp_w);
}

}else{
tmp_str[count] = tmp[start_p];
is_symbol(tmp_str, fp_w);
}
}

}
}

len = strlen(tmp);
if( start_p == len )
return 1;
else
analyse(tmp, start_p+1, fp_w);
}


int is_keyword(char *tmp, FILE *fp_w){

char *keyword[] = {
"main", "void", "int", "float", "char", "double", "for",
"if", "else", "while", "do", "break", "switch", "case",
"struct", "typedef", "char", "return", "continue", "sizeof"
};
//total number:20
for (i = 0; i < 20; ++i)
{
if( strcmp( tmp, keyword[i]) == 0 )
{
printf("<%d, %s>\n", i+2, tmp);
fprintf(fp_w, "<%d, %s>\n", i+2, tmp);
return 1;
}
}
return 0;
}

int is_symbol(char *tmp, FILE *fp_w){

char *symbol[] = {

"+", "-", "*", "/", "<", ">", "=", "!", "&", "|",
"\\", "#", "\"", "%", "^",

"++", "+=", "--", "-=", "*=", "/=", "%=", "&=", "^=", "|=",
"<=", ">=", "==", "!=", "&&", "||", ">>", "<<", "?:", "->",

"{", "}", "(", ")", "[", "]", ",", ";", ":", "."
};
//total number:45

for (i = 0; i < 45; ++i)
if( strcmp( tmp, symbol[i]) == 0 )
// printf("Yes! No.%d: %s\n", i, tmp);
{
printf("<%d, %s>\n", 22 + i, tmp);
fprintf(fp_w, "<%d, %s>\n", 22 + i, tmp);
return 1;
}

return 0;
}


int is_blankline(char *tmp){
int i;
int len = strlen(tmp);

for (i = 0; i < len; ++i)
{
if( !isspace(tmp[i]) )
return 0;
}
return 1;

}

int delete_comments(char *tmp)
{
len = strlen(tmp);
for(i = 0; i < len; i++)
{
if(tmp[i] == '/' && tmp[i+1] == '/')
{

tmp[i] = '\0';

}
}
}

int main(int argc, char const *argv[])
{
char tmp[100] = "";
char str[100] = "";
int flag = 0;

FILE *fp, *fp_tmp;
fp = fopen("test.c", "r");
fp_tmp = fopen("tmp.c", "w");
while(fgets(str, 100, fp) != NULL)
{
for(i = 0; i < 100; i++)
{
if(str[i] == '/' && str[i+1] == '/')
{
str[i] = '\n';
str[i+1] = '\0';
}

if(str[i] == '/' && str[i+1] == '*')
{
str[i] = '\0';
flag = 1;
fputs(str, fp_tmp);
}
if(str[i] == '*' && str[i+1] == '/')
{
for (j = 0; j < 100; ++j)
{
str[j] = str[j+i+2];
}
str[j] = '\0';
flag = 0;
}
}
if( flag == 0 )
fputs(str, fp_tmp);
}
fcloseall();

FILE *fp_new;
FILE *fp_w;
fp_new = fopen("tmp.c", "r");
fp_w = fopen("Lex_Result.txt", "w");

while(!feof(fp_new)){
if(fgets(tmp, 100, fp_new))
if( is_blankline(tmp) == 0)
analyse(tmp, 0, fp_w);
}

fcloseall();
remove("tmp.c");
return 0;
}

实例

目标程序

#include #include  //This is a simple target code
int main()
{
    printf("TEST BEGUN!");
    int a = 0;
    int b = 1;
    /*
        You can not see me!
    */
    if(a <= b)="" printf("Yes");

    return 0;
    //That's it!
}

分析结果

<<33, #>
<0, include>
<26, <>
<0, stdio>
<66, .>
<0, h>
<27, >>
<4, int>
<2, main>
<59, (>
<60, )>
<57, {>
<0, printf>
<59, (>
<34, ">
<0, test="">
<0, begun="">
<29, !="">
<34, "<="" span="">>
<60, )>
<64, ;>
<4, int>
<0, a>
<28, =>
<1, 0>
<64, ;>
<4, int>
<0, b>
<28, =>
<1, 1>
<64, ;>
<9, if>
<59, (>
<0, a>
<47, <=>
<0, b>
<60, )>
<0, printf>
<59, (>
<34, ">
<0, yes="">
<34, "<="" span="">>
<60, )>
<64, ;>
<19, return>
<1, 0>
<64, ;>
<58, }>