-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.lex
46 lines (42 loc) · 1.03 KB
/
tokenizer.lex
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
%option yylineno
%{
#include <y.tab.h> /* tokens' file */
#include <string.h>
#include <stdlib.h>
%}
NUMBER (0x)?[0-9]+
IDENTIFIER [a-zA-Z_][a-zA-Z0-9_]*
%%
char {return TOK_CHAR;}
int {return TOK_INT;}
long {return TOK_LONG;}
signed {return TOK_SIGNED;}
unsigend {return TOK_UNSIGNED;}
const {return TOK_CONST;}
volatile {return TOK_VOLATILE;}
register {return TOK_REGISTER;}
struct {return TOK_STRUCT;}
if {return TOK_IF;}
else {return TOK_ELSE;}
for {return TOK_FOR;}
while {return TOK_WHILE;}
break {return TOK_BREAK;}
typedef {return TOK_TYPEDEF;}
{IDENTIFIER} {
yylval.identifier_name = strdup(yytext);
return TOK_IDENTIFIER;}
{NUMBER} {
yylval.long_value = strtol(yytext, NULL, 0);
return TOK_NUMBER;}
[ \t\r]+
\n {yylineno++;}
== { return TOK_EQUAL; }
!= { return TOK_NEQUAL; }
\|\| { return TOK_OP_OR; }
&& { return TOK_OP_AND; }
\>\> { return TOK_SHIFT_RIGHT; }
\<\< { return TOK_SHIFT_LEFT; }
\<= { return TOK_LESS_EQUAL; }
\>= { return TOK_GREATER_EQUAL; }
. {return yytext[0];}
%%