This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.l
159 lines (136 loc) · 3.45 KB
/
lexer.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
%{
#include"common.h"
#include"parser.hpp"
extern TreeNode *nodePrint;
extern void InitIOFunctionNode();
extern void init_scanner();
extern void verbose(char* token, char* text);
extern void lex_error(char* msg);
%}
%option yylineno
/* 空白符 */
space [ \t\r\a]+
/* 关键字 */
int int
return return
void void
if if
else else
while while
continue continue
break break
/* 标识符 */
id [A-Za-z_][0-9A-Za-z_]*
/* 常量 */
integer 0|[1-9][0-9]*
/* 赋值运算 */
assign =
/* 一元运算 */
not \!
compl \~
/* 二元运算 */
add \+
sub \-
mul \*
div \/
mod \%
lt \<
le \<=
gt \>
ge \>=
eq ==
ne !=
band \&
bor \|
xor \^
and \&\&
or \|\|
/* 逗号分号括号 */
semi ;
comma ,
lb \{
rb \}
lp \(
rp \)
%%
[\n] {PR("\n");}
{space} {/* ignore all spaces */}
{int} {verbose("INT", yytext); return INT;}
{return} {verbose("RETURN", yytext); return RETURN;}
{void} {verbose("VOID", yytext); return VOID;}
{if} {verbose("IF", yytext); return IF;}
{else} {verbose("ELSE", yytext); return ELSE;}
{while} {verbose("WHILE", yytext); return WHILE;}
{continue} {verbose("CONTINUE", yytext); return CONTINUE;}
{break} {verbose("BREAK", yytext); return BREAK;}
"println_int" {
verbose("ID", yytext);
TreeNode *node = new TreeNode(-1, NODE_ID);
node->var_name = string(yytext);
yylval = node;
return ID;
}
{id} {
verbose("ID", yytext);
TreeNode* node = new TreeNode(yylineno, NODE_ID);
node->var_name = string(yytext);
yylval = node;
#ifdef ID_TOKEN_DEBUG
PR("get IDENTIFIER: %s", yytext);
#endif
return ID;
}
{integer} {
verbose("INTEGER", yytext);
TreeNode* node = new TreeNode(yylineno, NODE_CONST);
node->type = TYPE_INT;
node->int_val = atoi(yytext);
yylval = node;
return INTEGER;
}
{assign} {verbose("ASSIGN", yytext); return ASSIGN;}
{not} {verbose("NOT", yytext); return NOT;}
{compl} {verbose("COMPL", yytext); return COMPL;}
{add} {verbose("ADD", yytext); return ADD;}
{sub} {verbose("SUB", yytext); return SUB;}
{mul} {verbose("MUL", yytext); return MUL;}
{div} {verbose("DIV", yytext); return DIV;}
{mod} {verbose("MOD", yytext); return MOD;}
{lt} {verbose("LT", yytext); return LT;}
{le} {verbose("LE", yytext); return LE;}
{gt} {verbose("GT", yytext); return GT;}
{ge} {verbose("GE", yytext); return GE;}
{eq} {verbose("EQ", yytext); return EQ;}
{ne} {verbose("NE", yytext); return NE;}
{band} {verbose("BAND", yytext); return BAND;}
{bor} {verbose("BOR", yytext); return BOR;}
{xor} {verbose("XOR", yytext); return XOR;}
{and} {verbose("AND", yytext); return AND;}
{or} {verbose("OR", yytext); return OR;}
{semi} {verbose("SEMI", yytext); return SEMI;}
{comma} {verbose("COMMA", yytext); return COMMA;}
{lb} {verbose("LBRACE", yytext); return LBRACE;}
{rb} {verbose("RBRACE", yytext); return RBRACE;}
{lp} {verbose("LPAREN", yytext); return LPAREN;}
{rp} {verbose("RPAREN", yytext); return RPAREN;}
. {lex_error("Unrecognized character");}
%%
void init_scanner(){
PR("%-20s%s\n", "TOKEN-TYPE", "TOKEN-VALUE");
PR("-----------------------------------\n");
}
#ifdef VERBOSE
void verbose(char* token, char* text){
PR("%-20s%s\n", token, text);
}
#else
void verbose(char* token, char* text){
return;
}
#endif
void lex_error(char *msg){
PR("\nError at line %-3d: %s\n\n", yylineno, msg);
}
int yywrap(void){
return 1;
}