2014-10-16 15:30:59 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <thread>
|
2014-10-17 17:43:57 +02:00
|
|
|
#include "token_def.h"
|
2014-10-16 15:30:59 +02:00
|
|
|
#include "lemon_parser.h"
|
2014-10-17 17:43:57 +02:00
|
|
|
#include "flex_scanner.h"
|
2014-10-16 15:30:59 +02:00
|
|
|
|
|
|
|
// 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*));
|
2014-10-17 17:43:57 +02:00
|
|
|
void Parse(void *yyp, int yymajor, LexerToken* token, ResultType*);
|
2014-10-16 15:30:59 +02:00
|
|
|
|
2014-10-17 17:43:57 +02:00
|
|
|
// int yylex(void);
|
|
|
|
// int yylval;
|
2014-10-16 15:30:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
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 {
|
2014-10-17 17:43:57 +02:00
|
|
|
LexerToken token;
|
|
|
|
tokenCode = yylex(&token, scanner);
|
|
|
|
Parse(lemonParser, tokenCode, &token, &result);
|
2014-10-16 15:30:59 +02:00
|
|
|
// printf("Token %d\n", tokenCode);
|
|
|
|
} while (tokenCode > 0);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-10-17 17:43:57 +02:00
|
|
|
int parse_count = 0;
|
2014-10-16 15:30:59 +02:00
|
|
|
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);
|
|
|
|
|
2014-10-17 17:43:57 +02:00
|
|
|
parse_count++;
|
2014-10-16 15:30:59 +02:00
|
|
|
int result = parseString(string);
|
2014-10-17 17:43:57 +02:00
|
|
|
if (parse_count != 1) printf("+");
|
|
|
|
parse_count--;
|
|
|
|
|
2014-10-16 15:30:59 +02:00
|
|
|
if (result != c) printf("Error[%d]! %s != %d\n", id, string, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2014-10-17 17:43:57 +02:00
|
|
|
const int numThreads = 20;
|
|
|
|
int numRuns = 300;
|
2014-10-16 15:30:59 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|