-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.l
205 lines (175 loc) · 4.58 KB
/
scanner.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
203
204
205
%option noyywrap
%option never-interactive
%option header-file="scanner.h"
%option yylineno
%{
#include <stdio.h>
#include <math.h>
#include "parser.h"
int line = 1;
int yycolumn = 1;
char *filename;
#define YY_USER_ACTION \
yylloc.filename = filename; \
yylloc.first_line = yylloc.last_line = yylineno; \
yylloc.first_column = yycolumn; \
yylloc.last_column = yycolumn + yyleng - 1; \
yycolumn += yyleng;
void yyerror(const char *msg)
{
printf("%s:%d.%d: %s\n", yylloc.filename, yylloc.first_line, yylloc.first_column, msg);
}
%}
FLOAT [0-9]*\.[0-9]+[eE]*[-+]*[0-9]*
DECIMAL [0-9]+
%x MULTILINE_COMMENT
%x MULTILINE_STRING_LITERAL
%%
\/\/.* {}
\/\* {BEGIN(MULTILINE_COMMENT);}
<MULTILINE_COMMENT>\*\/ {BEGIN(INITIAL);}
<MULTILINE_COMMENT>\n {++line; }
<MULTILINE_COMMENT>. {}
"break" {return BREAK;}
"default" {return DEFAULT;}
"func" {return FUNC;}
"case" {return CASE;}
"struct" {return STRUCT;}
"else" {return ELSE;}
"switch" {return SWITCH;}
"const" {return CONST;}
"if" {return IF;}
"continue" {return CONTINUE;}
"for" {return FOR;}
"return" {return RETURN;}
"var" {return VAR;}
"float32" {return FLOAT32_TYPE;}
"int" {return INT_TYPE;}
"string" {return STRING_TYPE;}
"bool" {return BOOL_TYPE;}
"true" {
yylval.boolValue = 1;
return TRUE;
}
"false" {
yylval.boolValue = 0;
return FALSE;
}
"nil" {return NIL;}
"scan" {return SCANLN; }
"print" {return PRINTLN; }
\"(\\.|[^\\"])*\" {
int textLength = strlen(yytext);
yylval.string = (char*)malloc(textLength);
strncpy(yylval.string, yytext+1, textLength - 2);
yylval.string[textLength-2] = 0;
return STRING_LITERAL;
}
\'(\\.|[^\\"])*\' {
int textLength = strlen(yytext);
yylval.string = (char*)malloc(textLength);
strncpy(yylval.string, yytext+1, textLength - 2);
yylval.string[textLength-2] = 0;
return STRING_LITERAL;
}
"+=" {return PLUS_ASSIGN_OP;}
"-=" {return MINUS_ASSIGN_OP;}
"*=" {return MUL_ASSIGN_OP;}
"/=" {return DIV_ASSIGN_OP;}
"=" {return ASSIGN_OP;}
"," {return ',';}
"(" {return '(';}
")" {return ')';}
";" {return ';';}
"{" {return '{';}
"}" {return '}';}
"[" {return '[';}
"]" {return ']';}
"&&" {return AND;}
"||" {return OR;}
">" {return GT;}
"<" {return LT;}
">=" {return GTE;}
"<=" {return LTE;}
"==" {return EQU;}
"!=" {return NE;}
"!" {return '!';}
"+" {return '+';}
"-" {return '-';}
"*" {return '*';}
"/" {return '/';}
"." {return '.';}
"%" {return '%';}
":" {return ':';}
"++" {return PLUS_PLUS;}
"--" {return MINUS_MINUS;}
"&" {return '&';}
[a-zA-Z_][a-zA-Z0-9_]* {
yylval.string = (char*)malloc(strlen(yytext) + 1);
strcpy(yylval.string, yytext);
return IDENTIFIER;
}
{FLOAT} {
yylval.floatValue = atof(yytext);
return FLOAT_NUMBER;
}
0[xX][0-9a-fA-F]+ {
yylval.decValue = hex_decimal(yytext);
return DECIMAL_NUMBER;
}
0[1-7][0-7]* {
return OCTAL_NUMBER;
}
{DECIMAL} {
yylval.decValue = atoi(yytext);
return DECIMAL_NUMBER;
}
"\n" {line++;}
"\t" {}
[ \a\b\f\r\v\\]+ {}
. {printf(".unknown<-: \"%s\", line: %i\n", yytext, line);}
%%
int octal_decimal(int n)
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(8,i);
++i;
}
return decimal;
}
long hex_decimal(char hex[])
{
char *hexstr;
int length = 0;
const int base = 16; // Base of Hexadecimal Number
unsigned long decnum = 0;
int i;
// Find length of Hexadecimal Number
for (hexstr = hex; *hexstr != '\0'; hexstr++)
{
length++;
}
// Find Hexadecimal Number
hexstr = hex;
for (i = 0; *hexstr != '\0' && i < length; i++, hexstr++)
{
// Compare *hexstr with ASCII values
if (*hexstr >= 48 && *hexstr <= 57) // is *hexstr Between 0-9
{
decnum += (((int)(*hexstr)) - 48) * pow(base, length - i - 1);
}
else if ((*hexstr >= 65 && *hexstr <= 70)) // is *hexstr Between A-F
{
decnum += (((int)(*hexstr)) - 55) * pow(base, length - i - 1);
}
else if (*hexstr >= 97 && *hexstr <= 102) // is *hexstr Between a-f
{
decnum += (((int)(*hexstr)) - 87) * pow(base, length - i - 1);
}
}
return decnum;
}