-
Notifications
You must be signed in to change notification settings - Fork 0
/
boris.l
83 lines (77 loc) · 2.27 KB
/
boris.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
/* recognize the tokens for the boris compiler*/
%{
# include "boris.h"
# include "boris.tab.h"
int yycolumn = 1;
#define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno; \
if (yytext[0] == '\n') {\
yycolumn = 0;\
} \
yylloc.first_column = yycolumn; \
yylloc.last_column = yycolumn + yyleng - 1; \
yycolumn += yyleng;
%}
%option noyywrap nodefault yylineno
%option nounput
%option noinput
%%
array { return KW_ARRAY;}
tuple { return KW_TUPLE;}
local { return KW_LOCAL;}
global { return KW_GLOBAL;}
defun { return KW_DEFUN;}
end { return KW_END;}
while { return KW_WHILE;}
do { return KW_DO;}
if { return KW_IF;}
then { return KW_THEN;}
elsif { return KW_ELSIF;}
else { return KW_ELSE;}
foreach { return KW_FOREACH;}
for { return KW_FOR;}
in { return KW_IN;}
return { return KW_RETURN;}
print { return KW_PRINT;}
\.\. { return OP_DOTDOT;}
, { return OP_COMMA;}
\. { return OP_DOT;}
\[ { return OP_LBRAK;}
\] { return OP_RBRAK;}
; { return OP_SEMI;}
\( { return OP_LPAR;}
\) {return OP_RPAR;}
= { return OP_ASSIGN;}
\<-\> { return OP_EXCHANGE;}
\< { return OP_LESS;}
\> { return OP_GREATER;}
\<= { return OP_LESSEQUAL;}
\>= {return OP_GREATEREQUAL;}
== { return OP_EQUAL;}
!= { return OP_NOTEQUA;}
\+ { return OP_PLUS;}
- { return OP_MINUS;}
\* { return OP_MULT;}
\/ { return OP_DIV;}
[a-zA-Z_]+ {
if(yyleng > MAX_ID_LENGTH){
yyerror("[Lexer Error]ID length is too long. Will be truncted.");
}
yylval.sval = strndup(yytext, MAX_ID_LENGTH);
return ID;}
[0-9]+ {
long int val = atol(yytext);
if (yyleng > MAX_INT_LIT_LENGTH) {
yyerror("[Lexer Error]The length of INT_LIT exceeds max length, No guarantee what will happen.");
} else if (yyleng > 1 && yytext[0] == '0') {
yyerror("[Lexer Error]INT_LIT in a invalid format. Non-zero integer can't start with 0. No guarantee what will happen.");
} else if (val > MAX_INT_LIT){
yyerror("[Lexer Error]INT_LIT exceeds max value permitted. No guarantee what will happen.");
}
int ival = (int) val;
yylval.ival = ival;
return INT_LIT;}
\*\*\*.* { /* return COMMENT; */}
\n { /* return END_OF_LINE; */}
[ \t] { /* return WS; */}
. { yyerror("[Lexer Error]Mystery character %c, ignored.", *yytext); /* return UNKNOWN; */}
%%