-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner.l
90 lines (66 loc) · 1.86 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
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
86
87
88
89
90
%{
#include "AST.h"
#include "parser.tab.h"
extern FILE * flex_output;
extern union _NODE_ yylval;
%}
%%
"declblock" {
fprintf(flex_output, "declblock keyword from scanner\n");
return DECLBLOCK;
}
"codeblock" {
fprintf(flex_output, "codeblock keyword from scanner\n");
return CODEBLOCK;
}
"int" return INT;
"for" return FOR;
"while" return WHILE;
"if" return IF;
"else" return ELSE;
"goto" return GOTO;
"print" return PRINT;
"read" return READ;
"+" return '+';
"-" return '-';
"*" return '*';
"/" return '/';
"%" return '%';
";" return ';';
"," return ',';
":" return ':';
"{" return '{';
"}" return '}';
"=" return '=';
"[" return '[';
"]" return ']';
"(" return '(';
")" return ')';
"<" return LESS;
">" return GREATER;
"<=" return LESS_OR_EQUAL;
">=" return GREATER_OR_EQUAL;
"==" return EQUAL;
"!=" return NOT_EQUAL;
"||" return OR;
"&&" return AND;
[0-9][0-9]* {
yylval.int_val = atoi(yytext);
fprintf(flex_output, "integer literal: %s\n", yytext);
return INT_LITERAL;
}
[a-zA-Z][a-zA-Z0-9]* {
yylval.string_val = strdup(yytext);
fprintf(flex_output, "identifier: %s\n", yytext);
return IDENTIFIER;
}
\"(\.|[^\"])*\" {
yylval.string_val = strdup(yytext);
fprintf(flex_output, "string literal: %s\n", yytext);
return STRING_LITERAL;
}
[ \t\n] { /* Do nothing */ }
. {
fprintf(flex_output, "Unexpected token encountered: %s\n", yytext);
return ETOK;
}