-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.l
192 lines (174 loc) · 4.06 KB
/
main.l
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
%option noyywrap
%{
#include"common.h"
#include"main.tab.hh"
int firstword = 0;
extern vector<scope> scopes;//作用域向量
extern vector<variable> work_scope;//当前作用域的变量表
extern int scopeid;
%}
INTEGER 0|[1-9][0-9]*
CHARACTER \'(.*)\'
STRING \"(.*)\"
ID [[:alpha:]_][[:alpha:][:digit:]_]*
andID \&[[:alpha:]_][[:alpha:][:digit:]_]*
starID \*[[:alpha:]_][[:alpha:][:digit:]_]*
EOL (\r\n|\n)
WHITE [\t ]
commentblock "/*"
commentspace [ \t]
commentelement [^ \t\n]
commentnextline \n
commentend "*/"
commentline "//"
%x CBLOCK
%x CLINE
%%
"true" {
TreeNode *node = new TreeNode(NODE_BOOL);
node->bool_val = true;
yylval = node;
return TRUE;
}
"false" {
TreeNode *node = new TreeNode(NODE_BOOL);
node->bool_val = false;
yylval = node;
return FALSE;
}
"int" return INT;
"void" return VOID;
"char" return CHAR;
"string" return STR;
"for" return FOR;
"if" return IF;
"while" return WHILE;
"else" return ELSE;
"return" return RETURN;
"printf" return PRINTF;
"scanf" return SCANF;
"const" return CONST;
"." return dot;
"=" return ASSIGN;
"+=" return ADE;
"-=" return SUE;
"*=" return MUE;
"/=" return DIE;
"%=" return MOE;
"++" return ADO;
"--" return SUO;
"+" return ADD;
"-" return SUB;
"*" return MUL;
"/" return DIV;
"%" return MOD;
"!" return NOT;
"&&" return AND;
"||" return OR;
"==" return EQUAL;
"!=" return NEQUAL;
">" return GT;
">=" return GE;
"<" return LT;
"<=" return LE;
"," return COMMA;
";" return SEMICOLON;
"(" return SLB;
")" return SRB;
"[" return MLB;
"]" return MRB;
"{" {
scopes.emplace_back(scope(work_scope, scopeid++));
return LLB;
}
"}" return LRB;
{INTEGER} {
TreeNode *node = new TreeNode(NODE_CONINT);
node->int_val = atoi(yytext);
node->varType = VAR_INTEGER;
yylval = node;
return INTEGER;
}
{CHARACTER} {
TreeNode *node = new TreeNode(NODE_CONCHAR);
node->int_val = int(string(yytext)[1]);
node->varType = VAR_CHAR;
yylval = node;
return CHARACTER;
}
{STRING} {
TreeNode *node = new TreeNode(NODE_CONSTR);
string str = string(yytext);
str.erase(0,str.find_first_not_of("\""));
str.erase(str.find_last_not_of("\"") + 1);
node->str_val = str;
node->varType = VAR_STR;
yylval = node;
return STRING;
}
{ID} {
TreeNode *node = new TreeNode(NODE_VAR);
node->varName = string(yytext);
vector<variable>::reverse_iterator it = work_scope.rbegin();
while(it != work_scope.rend())
{
if((*it).name == node->varName)
{
node->varType = (*it).type;
break;
}
it++;
}
yylval = node;
return ID;
}
{andID} {
TreeNode *node = new TreeNode(NODE_VAR);
string str = string(yytext);
str.erase(str.begin());
node->varName = str;
node->varFlag = VAR_ADDRESS;
vector<variable>::reverse_iterator it = work_scope.rbegin();
while(it != work_scope.rend())
{
if((*it).name == node->varName)
{
node->varType = (*it).type;
break;
}
it++;
}
yylval = node;
return ID;
}
{starID} {
TreeNode *node = new TreeNode(NODE_VAR);
string str = string(yytext);
str.erase(str.begin());
node->varName = str;
node->varFlag = VAR_POINTER;
vector<variable>::reverse_iterator it = work_scope.rbegin();
while(it != work_scope.rend())
{
if((*it).name == node->varName)
{
node->varType = (*it).type;
break;
}
it++;
}
yylval = node;
return ID;
}
{EOL}
{WHITE}
{commentblock} { BEGIN CBLOCK; printf("# CBLOCK BEGIN\t"); }
<CBLOCK>{commentelement} { if(!firstword) printf("#\t"); printf("%s", yytext); firstword = 1; }
<CBLOCK>{commentspace} { if(firstword) printf("%s", yytext); }
<CBLOCK>{commentnextline} { printf("\n"); firstword = 0; }
<CBLOCK>{commentend} { printf("# CBLOCK END\n"); BEGIN INITIAL; }
{commentline} { BEGIN CLINE; printf("# CLINE START:"); }
<CLINE>{commentelement} { printf("%s", yytext); }
<CLINE>{commentspace} { printf("%s", yytext); }
<CLINE>{commentnextline} { printf("\n"); BEGIN INITIAL; }
%%