-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.lex
More file actions
95 lines (79 loc) · 1.84 KB
/
scanner.lex
File metadata and controls
95 lines (79 loc) · 1.84 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Header code */
%{
#include <unistd.h> // isatty()
#include "parser.tab.h"
// include-ing readline.h breaks the parser?!
extern char *readline(const char *);
extern void add_history(const char *);
#define YY_INPUT(buf, result, max_size) result = readInput(buf, max_size);
static int readInput(char *buf, int size) {
if (isatty(STDIN_FILENO)) {
char *line;
if (feof(yyin)) {
return YY_NULL;
}
line = readline("> ");
if (line == 0) {
return YY_NULL;
}
size_t len = strlen(line);
// -2 for newline and NULL
if (len > (size - 2)) {
fprintf(stderr,"input line too long\n");
return YYerror;
}
memcpy(buf, line, len);
buf[len] = '\n';
buf[len + 1] = 0x0;
add_history(line);
free(line);
return strlen(buf);
} else {
// this will include trailing newline
char *maybeNull = fgets(buf, size - 1, stdin);
if (maybeNull == 0) {
return YY_NULL;
}
size_t len = strlen(buf);
// -2 for newline and NULL
// >= because fgets would truncate too long input
if (len >= (size - 2)) {
fprintf(stderr,"input line too long\n");
return YYerror;
}
return len;
}
}
%}
%option noyywrap
%%
0x[0-9a-fA-F]+ {
yylval.i = strtol(yytext, 0x0, 0);
return NUM;
}
0[bB][01]+ {
// strtol doesn't understand "0b" prefixes
yylval.i = strtol(yytext + 2, 0x0, 2);
return NUM;
}
0 {
yylval.i = 0;
return NUM;
}
[1-9][0-9]* {
yylval.i = strtol(yytext, 0x0, 10);
return NUM;
}
"$" { return DOLLAR; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MULT; }
"/" { return DIVIDE; }
"^" { return POWER; }
"<<" { return LEFT_SHIFT; }
">>" { return RIGHT_SHIFT; }
"(" { return LPAREN; }
")" { return RPAREN; }
"\n" { return NEWLINE; }
[ \t\r] ;
%%