-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.l
70 lines (60 loc) · 2.2 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
%option yylineno
%pointer
%{
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include "ast.hpp"
#include "parser.hpp"
void yyerror(const char *);
%}
%x c_comments
number [1-9][0-9]*|0
name [a-zA-Z][a-zA-Z0-9]*
%%
"/*" { BEGIN(c_comments); }
"boolean" { return t_bool_type; }
"integer" { return t_int_type; }
"none" { return t_none_type; }
"print" { return t_print; }
"return" { return t_return; }
"if" { return t_if; }
"else" { return t_else; }
"while" { return t_while; }
"new" { return t_new; }
"equals" { return t_equals; }
"and" { return t_and; }
"or" { return t_or; }
"not" { return t_not; }
"true" { return t_true; }
"false" { return t_false; }
"extends" { return t_extends; }
"do" { return t_do; }
">=" { return t_geq; }
"->" { return t_arrow; }
";" { return ';'; }
"," { return ','; }
"(" { return '('; }
")" { return ')'; }
"}" { return '}'; }
"{" { return '{'; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"/" { return '/'; }
">" { return '>'; }
"=" { return '='; }
{name} { yylval.base_char_ptr = strdup(yytext); return t_id; }
{number}"."[0-9]+ { yylval.base_int = atoi(yytext); return t_number; }
{number} { yylval.base_int = atoi(yytext); return t_number; }
"." { return '.'; }
<c_comments>"*/" { BEGIN(INITIAL); }
<c_comments><<EOF>> { yyerror("dangling comment"); }
<c_comments>[ \t\n]+ { /* do nothing */ }
<c_comments>. { /* do nothing */ }
[ \t\n]+ { /* do nothing */ }
. { yyerror("invalid character"); }
%%
int yywrap(void) {
return 1;
}