-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexical.l
202 lines (202 loc) · 4.36 KB
/
lexical.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
193
194
195
196
197
198
199
200
201
202
%{
#include "syntax.tab.h"
#include "tree.h"
int yycolumn = 1;
extern int error;
#define YY_USER_ACTION \
yylloc.first_line = yylloc.last_line = yylineno; \
yylloc.first_column = yycolumn; \
yylloc.last_column = yycolumn + yyleng - 1; \
yycolumn += yyleng;
%}
%option yylineno
digit [0-9]
letter [a-zA-Z]
INT 0|[1-9]+[0-9]*
FLOAT {digit}+"."{digit}+
hexletter [0-9a-fA-F]
hexhead 0x|0X
ID (_|{letter})(_|{letter}|{digit}){0,31}
SEMI ;
COMMA ","
ASSIGNOP =
RELOP >|<|>=|<=|==|!=
PLUS "+"
MINUS "-"
STAR "*"
DIV "/"
AND &&
OR "||"
DOT "."
NOT !
TYPE int|float
LP "("
RP ")"
LB "["
RB "]"
LC "{"
RC "}"
STRUCT struct
RETURN return
WHILE while
IF if
%%
{INT} {
int val = atoi(yytext);
yylval.node = malloc_node("INT",INT,&val,yylineno);
return INT;
}
0[0-7]+ {
int val = 0;
sscanf(yytext, "%o", &val);
yylval.node = malloc_node("INT",INT,&val,yylineno);
return INT;
}
0{digit}*[8-9]+{digit}* {
error = 1;
int val = 0;
yylval.node = malloc_node( "INT", INT, &val,yylineno);
printf("Error type A at Line %d: Illegal octal number '%s'.\n", yylineno, yytext);
return INT;
}
{hexhead}{hexletter} {
int val = 0;
sscanf(yytext, "%x", &val);
yylval.node = malloc_node("INT",INT,&val,yylineno);
return INT;
}
{hexhead}{hexletter}*[g-zG-Z]+{hexletter}* {
error = 1;
int val = 0;
yylval.node = malloc_node( "INT", INT, &val,yylineno);
printf("Error type A at Line %d: Illegal octal number '%s'.\n", yylineno, yytext);
return INT;
}
{FLOAT}|{digit}*\.{digit}*[eE][\+\-]?{digit}+ {
float val = strtof(yytext, NULL);
yylval.node = malloc_node("FLOAT",FLOAT,&val,yylineno);
return FLOAT;
}
{TYPE} {
char *s = malloc(strlen(yytext) + 1);
strcpy(s, yytext);
yylval.node = malloc_node( "TYPE", Type, s,yylineno);
return Type;
}
"//"[^\n]* {}
"/*" {
char a = input(), b = input();
while(a != '*' || b != '/'){
a = b;
b = input();
}
//printf("COMMENT ");
}
{SEMI} {
yylval.node = malloc_node("SEMI",SEMI,NULL,yylineno);
return SEMI;
}
{COMMA} {
yylval.node = malloc_node("COMMA",COMMA,NULL,yylineno);
return COMMA;
}
{ASSIGNOP} {
yylval.node = malloc_node("ASSIGNOP",ASSIGNOP,NULL,yylineno);
return ASSIGNOP;
}
{RELOP} {
char *s = malloc(strlen(yytext) + 1);
strcpy(s, yytext);
yylval.node = malloc_node("RELOP",RELOP,s,yylineno);
return RELOP;
}
{PLUS} {
yylval.node = malloc_node("PLUS",PLUS,NULL,yylineno);
return PLUS;
}
{MINUS} {
yylval.node = malloc_node("MINUS",MINUS,NULL,yylineno);
return MINUS;
}
{STAR} {
yylval.node = malloc_node("STAR",STAR,NULL,yylineno);
return STAR;
}
{DIV} {
yylval.node = malloc_node("DIV",DIV,NULL,yylineno);
return DIV;
}
{AND} {
yylval.node = malloc_node("AND",AND,NULL,yylineno);
return AND;
}
{OR} {
yylval.node = malloc_node("OR",OR,NULL,yylineno);
return OR;
}
{DOT} {
yylval.node = malloc_node("DOT",DOT,NULL,yylineno);
return DOT;
}
{NOT} {
yylval.node = malloc_node("NOT",NOT,NULL,yylineno);
return NOT;
}
{LP} {
yylval.node = malloc_node("LP",LP,NULL,yylineno);
return LP;
}
{RP} {
yylval.node = malloc_node("RP",RP,NULL,yylineno);
return RP;
}
{LB} {
yylval.node = malloc_node("LB",LB,NULL,yylineno);
return LB;
}
{RB} {
yylval.node = malloc_node("RB",RB,NULL,yylineno);
return RB;
}
{LC} {
yylval.node = malloc_node("LC",LC,NULL,yylineno);
return LC;
}
{RC} {
yylval.node = malloc_node("RC",RC,NULL,yylineno);
return RC;
}
{STRUCT} {
yylval.node = malloc_node("STRUCT",STRUCT,NULL,yylineno);
return STRUCT;
}
{RETURN} {
yylval.node = malloc_node("RETURN",RETURN,NULL,yylineno);
return RETURN;
}
{IF} {
yylval.node = malloc_node("IF",IF,NULL,yylineno);
return IF;
}
{WHILE} {
yylval.node = malloc_node("WHILE",WHILE,NULL,yylineno);
return WHILE;
}
"else" {
yylval.node = malloc_node("ELSE",ELSE,NULL,yylineno);
return ELSE;
}
{ID} {
char *s = malloc(strlen(yytext) + 1);
strcpy(s, yytext);
yylval.node = malloc_node( "ID", ID, s,yylineno);
return ID;
}
\n {yycolumn = 1;}
[" "|\r|\t] {}
. {
error = 1;
printf("Error type A at Line %d: Mysterious characters \'%s\'\n",
yylineno, yytext);
}
%%