-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.l
87 lines (80 loc) · 1.64 KB
/
scan.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
84
85
%option noyywrap
%option nodefault
%option yylineno
%{
#include <string.h>
#include <stdlib.h>
#include "attr.h"
#include "parse.tab.h"
int fileno(FILE *stream);
%}
id [_a-zA-Z][_a-zA-Z0-9]*
%%
[0-9]+ {
yylval.token.valuetype = INTTYPE;
yylval.token.value = atoi(yytext);
yylval.token.nodekind = CONSTKIND;
return CONST;
}
"true" {
yylval.token.valuetype = BOOLTYPE;
yylval.token.value = 1;
yylval.token.nodekind = CONSTKIND;
return CONST;
}
"false" {
yylval.token.valuetype = BOOLTYPE;
yylval.token.value = 0;
yylval.token.nodekind = CONSTKIND;
return CONST;
}
"def" { return DEF; }
"main" { return MAIN; }
"end" { return END; }
"print" { return PRINT; }
"if" { return IF; }
"do" { return DO; }
"else" { return ELSE; }
"for" { return FOR; }
"while" { return WHILE; }
"return" { return RETURN; }
"int" { return INT; }
"bool" { return BOOL; }
"<=" { return LE; }
"==" { return EQ; }
"!=" { return NEQ; }
">=" { return GE; }
"&&" { return AND; }
"||" { return OR; }
"=" { return yytext[0]; }
"<" { return yytext[0]; }
">" { return yytext[0]; }
"+" { return yytext[0]; }
"-" { return yytext[0]; }
"*" { return yytext[0]; }
"/" { return yytext[0]; }
"!" { return yytext[0]; }
"(" { return yytext[0]; }
")" { return yytext[0]; }
";" { return yytext[0]; }
":" { return yytext[0]; }
"," { return yytext[0]; }
{id} {
yylval.token.nodekind = IDKIND;
strcpy(yylval.token.name, yytext);
return ID;
}
"/*" {
char in;
while (1) {
in = input();
if (in == '*') {
in = input();
if (in == '/') break;
}
}
}
[ \t\n]+ {}
"//".*\n {}
. { fprintf(stderr, "%c is an invalid character\n", yytext[0]); }
%%