-
Notifications
You must be signed in to change notification settings - Fork 7
/
scanner.l
48 lines (40 loc) · 1.55 KB
/
scanner.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
%{
#include "ASTTree.hh"
#include "parser.tab.hh"
extern "C" {
int yylex();
}
%}
separator [ \t\n\r]
letter [a-z]
digit [0-9]
number 0|[1-9]{digit}*|-[1-9]{digit}*
id {letter}({letter}|{digit}|"-")*
bool-val #[t|f]
%%
<<EOF>> { return 0; }
{separator} {}
"+" { type_stk.push(AST_PLUS); return(yytext[0]); }
"-" { type_stk.push(AST_MINUS); return(yytext[0]); }
"*" { type_stk.push(AST_MULTIPLY); return(yytext[0]); }
"/" { type_stk.push(AST_DIVIDE); return(yytext[0]); }
">" { type_stk.push(AST_GREATER); return(yytext[0]); }
"<" { type_stk.push(AST_SMALLER); return(yytext[0]); }
"=" { type_stk.push(AST_EQUAL); return(yytext[0]); }
"mod" { type_stk.push(AST_MODULES); return(_mod); }
"and" { type_stk.push(AST_AND); return(_and); }
"or" { type_stk.push(AST_OR); return(_or); }
"not" { type_stk.push(AST_NOT); return(_not); }
"define" { type_stk.push(AST_DEFINE); return(_define); }
"fun"|"lambda" { type_stk.push(AST_FUN); return(_fun); }
"if" { type_stk.push(AST_IF); return(_if); }
"print-num" { type_stk.push(AST_PNUMBER); return(print_num); }
"print-bool" { type_stk.push(AST_PBOOLVAL); return(print_bool); }
{number} { sscanf(yytext, "%d", &yylval.intVal); return(_number); }
{id} { yylval.id = strdup(yytext); return(_id); }
{bool-val} { if(yytext[1] == 't') yylval.boolVal = true;
else yylval.boolVal = false;
return(_bool_val);
}
. { return(yytext[0]); }
%%