Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (57 loc) · 1.62 KB
/
Copy pathmain.cpp
File metadata and controls
71 lines (57 loc) · 1.62 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
#include <stdio.h>
#include <stdlib.h>
#include <thread>
#include "token_def.h"
#include "lemon_parser.h"
#include "flex_scanner.h"
// Based on https://github.com/theory/flex-lemon-example
// and http://stackoverflow.com/questions/24833465/bison-yacc-vs-lemon-vs-standard-input
typedef float ResultType;
void *ParseAlloc(void *(*mallocProc)(size_t));
void ParseFree(void *p, void (*freeProc)(void*));
void Parse(void *yyp, int yymajor, LexerToken* token, ResultType*);
// int yylex(void);
// int yylval;
float parseString(const char* string) {
yyscan_t scanner;
yylex_init(&scanner);
// Scan the provided string
YY_BUFFER_STATE state = yy_scan_string(string, scanner);
void* lemonParser = ParseAlloc(malloc);
int tokenCode;
ResultType result;
do {
LexerToken token;
tokenCode = yylex(&token, scanner);
Parse(lemonParser, tokenCode, &token, &result);
// printf("Token %d\n", tokenCode);
} while (tokenCode > 0);
return result;
}
int parse_count = 0;
void multithreadTest(int numOfParses, int id) {
for (int n = 0; n < numOfParses; ++n) {
int a = rand() % 1000 + 1;
int b = rand() % 1000 + 1;
int c = a + b;
char string[32];
sprintf(string, "%d + %d", a, b);
parse_count++;
int result = parseString(string);
if (parse_count != 1) printf("+");
parse_count--;
if (result != c) printf("Error[%d]! %s != %d\n", id, string, result);
}
}
int main(void) {
const int numThreads = 20;
int numRuns = 300;
std::thread threads[numThreads];
for (int n = 0; n < numThreads; ++n) {
threads[n] = std::thread(multithreadTest, numRuns, n);
}
for (int n = 0; n < numThreads; ++n) {
threads[n].join();
}
return 0;
}