-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.ll
executable file
·205 lines (200 loc) · 4.04 KB
/
scanner.ll
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
%{
#include <string.h>
#include <stdlib.h>
#include <cstdio>
#include <iostream>
#include <vector>
#include "visitor.hh"
#include "ast.hh"
#include "parser.tab.hh"
int linenumm = 0;
extern int yylex();
%}
%%
[ \t] { /* eat white spaces */ }
"/""/"[^"\n"]*"\n" {
//consume the comment
}
[,] {
//std::cout<<linenumm<<" found a LIST_SEP;"<<std::endl;
return LIST_SEP;
}
[;] {
//std::cout<<linenumm<<" found a STMT_SEP;"<<std::endl;
return STMT_SEP;
}
[!] {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a UNARY_OP;"<<std::endl;
return UNARY_OP;
}
[+-] {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a ADD;"<<std::endl;
return ADD;
}
[*/%] {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a MUL;"<<std::endl;
return MUL;
}
[<>]=? {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a REL;"<<std::endl;
return REL;
}
[=!]= {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a EQ;"<<std::endl;
return EQ;
}
[&] {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a AND;"<<std::endl;
return AND;
}
[|] {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a OR;"<<std::endl;
return OR;
}
"void" {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a VOIDV;"<<std::endl;
return VOIDV;
}
("uint"|"int"|"float"|"char"|"file"|"bool") {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a TYPE;"<<std::endl;
return TYPE;
}
print {
return PRINT;
}
read {
return READ;
}
import {
return IMPORT;
}
return {
//std::cout<<linenumm<<" found a RETURN;"<<std::endl;
return RETURN;
}
break {
//std::cout<<linenumm<<" found a BREAK;"<<std::endl;
return BREAK;
}
continue {
//std::cout<<linenumm<<" found a CONTINUE;"<<std::endl;
return CONTINUE;
}
if {
//std::cout<<linenumm<<" found a IF;"<<std::endl;
return IF;
}
else {
//std::cout<<linenumm<<" found a ELSE;"<<std::endl;
return ELSE;
}
for {
//std::cout<<linenumm<<" found a FOR;"<<std::endl;
return FOR;
}
while {
//std::cout<<linenumm<<" found a WHILE;"<<std::endl;
return WHILE;
}
"true"|"false" {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a BOOLLIT;"<<std::endl;
return BOOLLIT;
}
[0-9]+ {
yylval.ival = atoi(yytext);
//std::cout<<linenumm<<" found a INTV; "<<yytext<<std::endl;
return INTV;
}
[0-9]*[.][0-9]+ {
yylval.ival = atof(yytext);
//std::cout<<linenumm<<" found a FLOATV;"<<std::endl;
return FLOATV;
}
("\'""\\""n""\'") {
yylval.sval[0] = yytext[1];
yylval.sval[1] = yytext[2];
yylval.sval[2] = 0;
return CHARV;
}
("\'""\\""0""\'") {
yylval.sval[0] = yytext[1];
yylval.sval[1] = yytext[2];
yylval.sval[2] = 0;
return CHARV;
}
("\'""\\""t""\'") {
yylval.sval[0] = yytext[1];
yylval.sval[1] = yytext[2];
yylval.sval[2] = 0;
return CHARV;
}
("\'"[^"\'"]"\'") {
yylval.sval[0] = yytext[1];
yylval.sval[1] = 0;
//std::cout<<linenumm<<" found a CHARV;"<<std::endl;
return CHARV;
}
[a-zA-Z_][a-zA-Z0-9_]* {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a NAME; "<< yytext<<std::endl;
return NAME;
}
("+"|"-"|"*"|"%")?= {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a ASSIGN;"<<std::endl;
return ASSIGN;
}
"?" {
//std::cout<<linenumm<<" found a TQ;"<<std::endl;
return TQ;
}
":" {
//std::cout<<linenumm<<" found a TE;"<<std::endl;
return TE;
}
"(" {
//std::cout<<linenumm<<" found a OPENPAREN;"<<std::endl;
return OPENPAREN;
}
")" {
//std::cout<<linenumm<<" found a CLOSEPAREN;"<<std::endl;
return CLOSEPAREN;
}
"{" {
//std::cout<<linenumm<<" found a OPENBRACE;"<<std::endl;
return OPENBRACE;
}
"}" {
//std::cout<<linenumm<<" found a CLOSEBRACE;"<<std::endl;
return CLOSEBRACE;
}
"[" {
//std::cout<<linenumm<<" found a OPENSQUARE;"<<std::endl;
return OPENSQUARE;
}
"]" {
//std::cout<<linenumm<<" found a CLOSESQUARE;"<<std::endl;
return CLOSESQUARE;
}
"\""[^"\""]+"\"" {
strcpy(yylval.sval,yytext);
//std::cout<<linenumm<<" found a STRINGLIT; "<<yytext<<std::endl;
return STRINGLIT;
}
\n { ++linenumm;}
. { printf("Line No %d: Unrecognized character '%c'\n", linenumm, yytext[0]);}
%%
// int main(){
// while(yylex());
// }