-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2pos.cpp
55 lines (51 loc) · 1.17 KB
/
index2pos.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include "lexer.h"
int num;
char word[1000];
bool str_int_eq(const char *str1, int x) {
return x == -1 || atoi(str1) == x;
}
int main(int argc, char **argv) {
std::vector<int> index;
int token;
extern int yyoffset;
int prev_offset = -1;
int offset;
bool had_close = false;
index.push_back(-1);
while ((offset = yyoffset, (token = yylex())) != 0 &&
(had_close ||
(int)index.size() - (index.back() == -1 ? 1 : 0) != argc - 1 ||
!std::equal(argv + 1, argv + argc, index.begin(), str_int_eq)))
{
// printf("token: %d\n", token);
// printf("index: ");
// for (size_t i = 0; i < index.size(); ++i) printf("%d ", index[i]);
// printf("\n");
switch (token) {
case OPEN:
++index.back();
index.push_back(-1);
break;
case CLOSE:
had_close = true;
index.pop_back();
break;
case SPACE:
continue;
default:
++index.back();
}
had_close = false;
prev_offset = offset;
}
// printf("token: %d\n", token);
// printf("index: ");
// for (size_t i = 0; i < index.size(); ++i) printf("%d ", index[i]);
// printf("\n");
offset = prev_offset;
if (token)
printf("%d", offset);
}