-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.l
63 lines (49 loc) · 1.64 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
%option yylineno
%pointer
%{
#include <cstdlib>
#include <cerrno>
#include <climits>
#include <limits>
#include "ast.hpp"
#include "parser.hpp"
void yyerror(const char *);
%}
ID [a-zA-Z][a-zA-Z0-9]*
%x COMMENT
%%
"print" { return T_PRINT; }
"return" { return T_RETURN; }
"if" { return T_IF; }
"else" { return T_ELSE; }
"while" { return T_WHILE; }
"new" { return T_NEW; }
"extends" { return T_EXTENDS; }
"do" { return T_DO; }
"integer" { return T_INTEGER; }
"boolean" { return T_BOOLEAN; }
"none" { return T_NONE; }
[{}\(\),=:\.\;] { return *yytext; }
"->" { return T_ARROW; }
[\+\-\*\/>] { return *yytext; }
">=" { return T_GEQ; }
"equals" { return T_EQUALS; }
"and" { return T_AND; }
"or" { return T_OR; }
"not" { return T_NOT; }
0|([1-9][0-9]*) { long int value = strtol(yytext, NULL, 0); if (errno != 0 || value > INT_MAX) { yyerror("integer out of range"); } yylval.integer_ptr = new IntegerNode((int)value); return T_NUMBER; }
"true" { return T_TRUE; }
"false" { return T_FALSE; }
{ID} { yylval.identifier_ptr = new IdentifierNode(std::string(strdup(yytext))); return T_IDENTIFIER; }
"/*" { BEGIN(COMMENT); }
<COMMENT>\n ;
<COMMENT>. ;
<COMMENT><<EOF>> { yyerror("dangling comment"); }
<COMMENT>"*/" { BEGIN(INITIAL); }
\n ;
[[:space:]] ;
. { yyerror("invalid character"); }
%%
int yywrap(void) {
return 1;
}