blob: 802df5a34d39465b48ca39a757cf23ad307851a5 (
plain)
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
|
#include <cstring>
#include <iostream>
struct Scanner
{
Scanner(char *txt)
: cur(txt), lim(txt + strlen(txt))
{
}
char *cur;
char *lim;
char *ptr;
char *ctx;
char *tok;
};
#define YYCTYPE char
#define YYCURSOR s.cur
#define YYLIMIT s.lim
#define YYMARKER s.ptr
#define YYCTXMARKER s.ctx
#define YYFILL(n)
enum What
{
UNEXPECTED,
KEYWORD,
NUMBER,
EOI
};
char * tokens[] = { "UNEXPECTED", "KEYWORD", "NUMBER", "EOI" };
int scan(Scanner &s)
{
YYCTYPE *cursor = s.cur;
if(cursor == s.lim)
return EOI;
std:
s.tok = cursor;
/*!re2c
("a"|"b")/[1] { return KEYWORD; }
("a"|"b")/[0-9]+ { return KEYWORD; }
[0-9]+ { return NUMBER; }
[ \t\n]
{
if(s.cur == s.lim)
return EOI;
cursor = s.cur;
goto std;
}
.
{
return UNEXPECTED;
}
*/
}
/*!max:re2c */
int main(int,char**)
{
Scanner s("a77 a1 b8 b1");
int t, n = 0;
while ((t = scan(s)) != EOI)
{
std::cout << (++n) << ": " << tokens[t] << " = \""; std::cout.write(s.tok, s.cur-s.tok); std::cout << "\"" << std::endl;
}
}
|