Skip to content

Instantly share code, notes, and snippets.

@mrdomino
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdomino/a2cec4de86f59f51aa0d to your computer and use it in GitHub Desktop.
Save mrdomino/a2cec4de86f59f51aa0d to your computer and use it in GitHub Desktop.
#include <hammer/glue.h>
#include <hammer/hammer.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
HParser* parser;
HBenchmarkResults* results;
int main() {
H_RULE(one, h_ch('1'));
H_RULE(zero, h_ch('0'));
H_RULE(bit, h_choice(zero, one, NULL));
H_RULE(unary_indirect, h_indirect());
H_RULE(unary_rec, h_sequence(one, unary_indirect, NULL));
H_RULE(unary, h_choice(unary_rec, zero, NULL));
h_bind_indirect(unary_indirect, unary);
H_RULE(elias_indirect, h_indirect());
H_RULE(elias_rec, h_sequence(zero, elias_indirect, bit, NULL));
H_RULE(elias, h_choice(elias_rec, one, NULL));
h_bind_indirect(elias_indirect, elias);
H_RULE(either, h_choice(elias, unary, NULL));
H_RULE(parser, h_sequence(either, h_end_p(), NULL));
HParserTestcase tests[] = {
{(uint8_t*)"0", 1, "(u0x30)"},
{(uint8_t*)"1", 1, "(u0x31)"},
{(uint8_t*)"10", 2, "((u0x31 u0x30))"},
{(uint8_t*)"010", 3, "((u0x30 u0x31 u0x30))"},
{(uint8_t*)"011", 3, "((u0x30 u0x31 u0x31))"},
{(uint8_t*)"1111111111111110", 16,
"((u0x31 (u0x31 (u0x31 (u0x31 (u0x31 (u0x31 (u0x31 (u0x31 (u0x31 "
"(u0x31 (u0x31 (u0x31 (u0x31 (u0x31 (u0x31 u0x30))))))))))))))))"},
{(uint8_t*)"000000000011110001111", 21,
"((u0x30 (u0x30 (u0x30 (u0x30 (u0x30 (u0x30 (u0x30 (u0x30 (u0x30 "
"(u0x30 u0x31 u0x31) u0x31) u0x31) u0x30) u0x30) u0x30) u0x31) u0x31) "
"u0x31) u0x31))"},
{0},
};
#ifdef TRY_PARSE
h_compile(parser, PB_LALR, NULL);
HParseResult *p = h_parse(parser, tests[5].input,
tests[5].length);
char *res_unamb = h_write_result_unamb(p->ast);
fprintf(stderr, "res:{%s}\n", res_unamb);
free(res_unamb);
h_parse_result_free(p);
#endif
results = h_benchmark(parser, tests);
h_benchmark_report(stdout, results);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment