-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.l
196 lines (170 loc) · 5.82 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
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
/* %option noyywrap */
D [0-9]
L [a-zA-Z_]
E [Ee][+-]?{D}+
%{
#include <stdio.h>
#include "grammar.tab.h"
static void count();
static void comment();
void yyerror(const char *);
void check_integer_overflow();
// extern void lyyerror(YYLTYPE t,char *s,...);
char linebuf[500];
char filename[50];
void warning(const char*);
int yycolumn =1;
#define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno;\
yylloc.first_column = yycolumn; yylloc.last_column = yycolumn+yyleng-1;\
yycolumn += yyleng;
%}
%option lex-compat
%%
"/*" { comment(); } /* defined as comment */
"//"[^\n]* { /* consume //-comment */ } /* comment */
"cns" { count(); return CNS; } /* cns is constant */
"elif" { count(); return ELIF; } /* elif is just like else if in cpp */
"else" { count(); return ELSE; } /* it's else statement */
"endif" { count(); return ENDIF;} /* endif directive used to define "#if" */
"float" { count(); return FLOAT; } /* float datatype */
"if" { count(); return IF; } /* if */
"int" { count(); return INT; } /* int datatype */
"Tensor" { count(); return TENSOR; } /* tensor datatype */
"var" { count(); return VAR; } /* var is for variable */
"bool" { count(); return BOOL;} /* it is boolean */
"sizeof" { count(); return SIZEOF;} /* sizeof function */
"grad" { count(); return GRAD;} /* gradiance */
"backward" { count(); return BACKWARD;}
"cos" { count(); return COS;} /* cos function as keyword*/
"sin" { count(); return SIN;} /* sin function as keyword */
"exp" { count(); return EXP;} /* exponential function as keyword */
"log" { count(); return LOG;} /* logarithmic function as keyword */
"transpose" { count(); return TRANSPOSE;} /* transpose function as keyword */
"print" { count(); return PRINT;} /* print is just like print in python */
"declare" { count(); return DECLARE;} /* declare is just like declare in cpp */
"operations" { count(); return OPERATIONS;} /* operations is just like operations in cpp */
"gradient" { count(); return GRADIENT;} /* gradient is just like gradient in cpp */
{L}({L}|{D})* { count();/*printf("IDENTIFIER: %s\n", yytext);*/ return IDENTIFIER;} /* D for digit H for hexadecimal E for exponential L for letters*/
{D}+ { yylval.ival = atoi(yytext) ; check_integer_overflow(); return INT_CONST; }
{D}+{E} { yylval.fval = atof(yytext) ; return FLOAT_CONST; }
{D}*"."{D}+({E})? { yylval.fval = atof(yytext) ;return FLOAT_CONST; }
{D}+"."{D}*({E})? { yylval.fval = atof(yytext) ; return FLOAT_CONST; }
L?\"(\\.|[^\\"])*\" { count(); return STRING_LITERAL; }
\"[^\n\"]*[;,]? { count(); warning("unterminated string, misssing \" character"); return STRING_LITERAL; }
/* Tensor multiply operator */
"+=" { count(); return ADD_ASSIGN; }
"-=" { count(); return SUB_ASSIGN; }
"*=" { count(); return MUL_ASSIGN; }
"/=" { count(); return DIV_ASSIGN; }
"%=" { count(); return MOD_ASSIGN; }
"@=" { count(); return AT_ASSIGN;}
"&&" { count(); return AND_OP; }
"||" { count(); return OR_OP; }
"<=" { count(); return LE_OP; }
">=" { count(); return GE_OP; }
"==" { count(); return EQ_OP; }
"!=" { count(); return NE_OP; }
";" { count(); return ';'; } /* some other operators from 74 to 96 */
"{" { count(); return '{'; }
"}" { count(); return '}'; }
"," { count(); return ','; }
":" { count(); return ':'; }
"=" { count(); return '='; }
"(" { count(); return '('; }
")" { count(); return ')'; }
"[" { count(); return '['; }
"]" { count(); return ']'; }
"-" { count(); return '-'; }
"+" { count(); return '+'; }
"*" { count(); return '*'; }
"@" { count(); return '@'; }
"/" { count(); return '/'; }
"%" { count(); return '%'; }
"<" { count(); return '<'; }
">" { count(); return '>'; }
\n.* { strcpy(linebuf,yytext+1); yyless(1); yycolumn=1;}
[ \t\v\f] { /*count(); eat these chars*/ }
. { /* ignore bad characters */ }
%%
int yywrap()
{
return(1);
}
static void
comment() {
char c, c1 = 0;
while ( (c = yyinput()) != 0 ) { /* (EOF maps to 0) */
if ( c == '/' && c1 == '*' )
return;
c1 = c;
}
printf("Error::Unclosed comment of type */ \n");
}
int column = 0;
void count()
{
yylval.string = strdup(yytext);
int i;
for (i = 0; yytext[i] != '\0'; i++)
if (yytext[i] == '\n')
column = 0;
else if (yytext[i] == '\t')
column += 8 - (column % 8);
else
column++;
// ECHO;
}
void check_integer_overflow()
{
int is_neg = 0,l1,l2, overflow_detected = 0,n=0;
char int_max[] = "2147483647", int_min[] = "-2147483648";
if (yytext[0] == '-')
is_neg =1;
l1 = strlen(yytext);
if(is_neg)
l2 = strlen(int_min);
else
l2 = strlen(int_max);
// If number is negative
if(is_neg)
{
if (l2 < l1)
overflow_detected = 1;
else if(l2 == l1)
{
for (n = 1; n < l1; n++) {
if (int_min[n] < yytext[n])
overflow_detected =1;
}
}
}
if (l1 > l2)
overflow_detected = 1;
else if (l2 == l1)
{
for (n = 0; n < l1; n++)
{
if (yytext[n] > int_max[n])
overflow_detected =1;
}
}
if(overflow_detected){
warning("Integer Overflow, number will not fit into 32-bit integer constant");
}
}
/* int main(int argc,char** argv)
{
if( argc >=2 ){
yyin = fopen(argv[1] , "r");
if(yyin ==NULL){
printf("File not found\n");
return 1;
}
}
else{
printf("Use case : ./p1.out \"filename.txt\" \n");
return 1;
}
yylex();
return 0;
} */