-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitset_parse.yy
60 lines (49 loc) · 1.2 KB
/
bitset_parse.yy
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
%{
#include <cassert> // assert
#include <cstdlib> // atoi
#include <cstdint> // uint64_t
#include <iostream> // std:cerr
uint64_t range_set( unsigned low, unsigned high ) {
assert( low <= high );
assert( high < 64 );
return ~UINT64_C(0) << low
& ((UINT64_C(1) << high) -1);
}
uint64_t discrete_set( unsigned value ) {
assert( value < 64 );
return UINT64_C(1)<<value;
}
//-- Lexer prototype required by bison, aka getNextToken()
extern "C" {
int yylex();
int yyerror(const char *p) { std::cerr << "Error: " << p << "\n"; return 0; }
}
%}
%code requires {
struct bitset_gen {
unsigned lo;
unsigned hi;
uint64_t set;
};
}
//-- SYMBOL SEMANTIC VALUES -----------------------------
%union {
unsigned num;
uint64_t set;
};
%token COMMA DASH ERROR
%token <num> NUMBER
%type <set> range exp
//-- GRAMMAR RULES ---------------------------------------
%%
exp: range COMMA exp { $$ = $1 | $3; }
| range { $$ = $1; }
range: NUMBER { $$ = discrete_set($1); }
| NUMBER DASH NUMBER { $$ = range_set($1, $3); }
%%
//-- FUNCTION DEFINITIONS ---------------------------------
int main()
{
yyparse();
return 0;
}