-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.l
98 lines (76 loc) · 1.84 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
91
92
93
94
95
96
97
98
/* DEFINITIONS*/
%{
#include "symbol_table.h"
#include "parser.h"
FILE *set_input_file(char* file_name);
int getLineNumber(void);
int isRunning(void);
int running = 1;
int lineCounter = 1;
int tokenCode = 0;
%}
%x comment
%%
/* RULES*/
/* KEYWORDS*/
byte {return KW_BYTE;}
short {return KW_SHORT;}
long {return KW_LONG;}
float {return KW_FLOAT;}
double {return KW_DOUBLE;}
when {return KW_WHEN;}
then {return KW_THEN;}
else {return KW_ELSE;}
while {return KW_WHILE;}
for {return KW_FOR;}
to {return KW_TO;}
read {return KW_READ;}
return {return KW_RETURN;}
print {return KW_PRINT;}
/* SPECIAL CARACTERS*/
[,;:\(\)\[\]\{\}\+\-*<>=!&$#/] {return yytext[0];}
/* COMPOSED OPERATORS*/
"<=" {return OP_LE;}
">=" {return OP_GE;}
"==" {return OP_EQ;}
"!=" {return OP_NE;}
"&&" {return OP_AND;}
"||" {return OP_OR;}
/* IDENTIFIERS*/
[_A-Za-z][_A-Za-z0-9]* {yylval.symbol = symtab_insert(yytext, SYMBOL_IDENTIFIER);return TK_ID;}
/* LITERALS*/
[0-9]+ {yylval.symbol = symtab_insert(yytext, SYMBOL_LIT_INT);return LIT_INT;}
[0-9]+\.[0-9]+ {yylval.symbol = symtab_insert(yytext, SYMBOL_LIT_REAL);return LIT_REAL;}
'.' {yylval.symbol = symtab_insert(yytext, SYMBOL_LIT_CHAR);return LIT_CHAR;}
/* STRINGS */
\"(\\.|[^\\"\n])*\" {yylval.symbol = symtab_insert(yytext, SYMBOL_LIT_STRING);return LIT_STRING;}
/* LINE COUNTER*/
\n {lineCounter++;}
/* SINGLELINE COMMENTS*/
\/\/.*\n {lineCounter++;}
/* MULTILINE COMMENTS */
"/*" BEGIN(comment);
<comment>. {}
<comment>\n {lineCounter++;}
<comment>"*/" BEGIN(INITIAL);
/* SPACES AND TABS*/
[ \t\r] {}
/* ERROR*/
. {return TOKEN_ERROR;}
%%
/* SUBROTINES*/
int yywrap(void){
running = 0;
return 1;
}
FILE *set_input_file(char* file_name){
if(yyin = fopen(file_name, "r"))
return yyin;
return NULL;
}
int getLineNumber(void){
return lineCounter;
}
int isRunning(void){
return running;
}