-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscanner.l
More file actions
74 lines (63 loc) · 2.15 KB
/
Copy pathscanner.l
File metadata and controls
74 lines (63 loc) · 2.15 KB
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
%{
#define YYSTYPE char *
#include "y.tab.h"
int cur_line = 1;
void yyerror(const char *msg);
void unrecognized_char(char c);
void unterminate_string();
#define _DUPTEXT {yylval = strdup(yytext);}
%}
/* note \042 is '"' */
WHITESPACE ([ \t\r\a]+)
SINGLE_COMMENT1 ("//"[^\n]*)
SINGLE_COMMENT2 ("#"[^\n]*)
OPERATOR ([+*-/%=,;!<>(){}])
INTEGER ("0x"[0-9a-f]{8}|[0-9]+)
IDENTIFIER ([_a-zA-Z][_a-zA-Z0-9]*)
UNTERM_STRING (\042[^\042\n]*)
STRING (\042[^\042\n]*\042)
INT_POINTER ("int"[ \t\r\a]+"*")
%%
\n { cur_line++; }
{WHITESPACE} { /* ignore every whitespace */ }
{SINGLE_COMMENT1} { /* skip for single line comment */ }
{SINGLE_COMMENT2} { /* skip for single line comment */ }
{OPERATOR} { return yytext[0]; }
"int" { return T_Int; }
"void" { return T_Void; }
"return" { return T_Return; }
"print" { return T_Print; }
"readint" { return T_ReadInt; }
"while" { return T_While; }
"if" { return T_If; }
"else" { return T_Else; }
"break" { return T_Break; }
"continue" { return T_Continue; }
"<=" { return T_Le; }
">=" { return T_Ge; }
"==" { return T_Eq; }
"!=" { return T_Ne; }
"&&" { return T_And; }
"||" { return T_Or; }
{INT_POINTER} { _DUPTEXT return T_IntPointer; }
{INTEGER} { _DUPTEXT return T_IntConstant; }
{STRING} { _DUPTEXT return T_StringConstant; }
{IDENTIFIER} { _DUPTEXT return T_Identifier; }
{UNTERM_STRING} { unterminate_string(); }
. { unrecognized_char(yytext[0]); }
%%
int yywrap(void) {
return 1;
}
void unrecognized_char(char c) {
char buf[32] = "Unrecognized character: ?";
buf[24] = c;
yyerror(buf);
}
void unterminate_string() {
yyerror("Unterminate string constant");
}
void yyerror(const char *msg) {
fprintf(stderr, "Error at line %d:\n\t%s\n", cur_line, msg);
exit(-1);
}