-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.l
163 lines (135 loc) · 3.54 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
%{
#include "ast.hh"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "parser.tab.hh"
extern "C" int yylex();
%}
delim [ \t\n]
ws {delim}+
alpha [A-Za-z]
digit [0-9]
comments "//".*
id {alpha}({alpha}|{digit})*
number {digit}+(\.{digit}+)?(E[+-]?{digit}+)?
hexit 0x({digit}|[a-fA-F])+
character (\\n|\\t|\\'|\\\\|\\\"|[^\\"'])
type int|boolean
%%
"class" { return CLASS;}
"{" { return LC;}
"}" { return RC;}
"(" { return LP;}
")" {return RP;}
"[" {return LS;}
"]" {return RS;}
";" { return SEMICOLON;}
"!" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return NOT;
}
"return" { return RETURN;}
"+" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return ADD;
}
"-" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return MINUS;
}
"*" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return MULTIPLY;
}
"/" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return DIVIDE;
}
"%" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return REMAINDER;
}
"=" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return EQUAL;
}
"+=" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return PLUSEQUAL;}
"-=" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return MINUSEQUAL;}
"continue" {return CONTINUE;}
"void" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return TYPE;
}
{type} {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return TYPE;
}
"while" {return WHILE;}
"for" {return FOR;}
"if" {return IF;}
"else" {return ELSE;}
"break" return BREAK;
"callout" return CALLOUT;
">"|"<"|">="|"<=" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return ROP;
}
"&&" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return ANDAND;}
"||" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return OROR;}
"=="|"!=" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return EOP;
}
true|false {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return BOOLIT;}
{delim} {;}
"," return COMMA;
{comments} {;}
{id} {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext, yyleng + 1);
return ID;
}
{number} {
yylval.integer = atoi(yytext);
return NUMBER;}
{hexit} { return HEXIT; }
"'"{character}"'" {
yylval.string=(char *)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string, yytext+1, yyleng -2);
yylval.string[yyleng-2]='\0';
return CHARACTER;
}
\"{character}*\" {
yylval.string=(char*)malloc((yyleng+1)*(sizeof(char)));
strncpy(yylval.string,yytext+1,yyleng-2);
yylval.string[yyleng-2]='\0';
return STRINGIT;
}
%%