-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlexer.l
73 lines (56 loc) · 1.4 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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.tab.h"
%}
%%
/* math operators */
"+" { return OP_ADD; }
"-" { return OP_SUB; }
"*" { return OP_MUL; }
"/" { return OP_DIV; }
/* comparison operators */
">" { return OP_GT; }
"<" { return OP_LT; }
"==" { return OP_EQ; }
"!=" { return OP_NE; }
"<=" { return OP_LE; }
">=" { return OP_GE; }
/* assignment operator */
"=" { return OP_ASSIGN; }
/* statement terminator */
";" { return SEMICOLON; }
/* grouping operators */
"{" { return LBRACE; }
"}" { return RBRACE; }
"(" { return LPAREN; }
")" { return RPAREN; }
/* data type keywords */
"int" { return KW_INT; }
"float" { return KW_FLOAT; }
/* control flow keywords */
"if" { return KW_IF; }
"else" { return KW_ELSE; }
"while" { return KW_WHILE; }
/* regular expression for identifier names */
[_a-zA-Z][_a-zA-Z0-9]* {
yylval.identifier = strdup(yytext);
return IDENTIFIER;
}
/* regular expression for integer constants */
[0-9]+ {
yylval.int_const = atoi(yytext);
return INT_CONSTANT;
}
[0-9]+\.[0-9]+ {
yylval.float_const = (float) atof(yytext);
return FLOAT_CONSTANT;
}
/* ignore whitespace */
[ \r\t]+ { }
/* ignore newlines */
\n { }
/* ignore unknown characters */
. { }
%%