librdesc
Loading...
Searching...
No Matches
main.c
1#include "grammar.h"
2#include "interpreter.h"
3
4#include <rdesc/grammar.h>
5#include <rdesc/util.h>
6#include <rdesc/rdesc.h>
7
8#include "../../../examples/lib/exblex.h"
9
10#include <assert.h>
11#include <stdint.h>
12#include <stdio.h>
13#include <string.h>
14
15
16int main(int argc, char *argv[])
17{
19#include <rdesc/grammar.h>
22struct rdesc_grammar grammar;
23
24/* Returns non-zero value on memory allocation error, abort the program in this
25 * case. */
26assert(rdesc_grammar_init_checked(&grammar,
27 PM_PRODUCTION_COUNT,
28 PM_MAX_ALTERNATIVE_COUNT,
29 PM_MAX_ALTERNATIVE_SIZE,
30 pm_grammar) == 0);
32
33
35#include <rdesc/util.h>
38if (argc > 1 && strcmp(argv[1], "dump_bnf") == 0) {
39 rdesc_dump_bnf(stdout, &grammar, tk_names, nt_names);
40}
42
43
45#include <rdesc/rdesc.h>
47struct rdesc parser;
48
49/* Similar to grammar_init, this function also returns non-zero in case of
50 * memory allocation failure. */
51assert(rdesc_init(&parser, &grammar, sizeof(char *), tk_destroyer) == 0);
53
54
56struct exblex lexer;
57enum rdesc_result res;
58
59char buf[4096];
60
61if (fgets(buf, 4096, stdin) == NULL)
62 buf[0] = '\0';
63
64exblex_init(&lexer, buf, exblex_tks);
65
66/* Memory allocation assertion. */
67assert(rdesc_start(&parser, NT_STMT) == 0);
68
69do {
70 uint16_t tk_id = exblex_next(&lexer);
71 char *tk_seminfo = exblex_current_seminfo(&lexer);
72
73 /* EOF before match */
74 if (tk_id == 0) {
75 res = RDESC_NOMATCH;
76 break;
77 }
78
79 res = rdesc_pump(&parser, tk_id, &tk_seminfo);
80
81 /* Retry if allocation failed. */
82 while (res == RDESC_ENOMEM)
83 res = rdesc_resume(&parser);
84} while (res == RDESC_CONTINUE);
86
87
89#include <rdesc/util.h>
92if (argc > 1 && strcmp(argv[1], "dump_cst") == 0 && res == RDESC_READY) {
93 rdesc_dump_cst(stdout, &parser, node_printer);
94}
96
97
99if (res == RDESC_READY) {
100 printf(">> %.2lf\n", pm_interpreter(&parser, rdesc_root(&parser)));
101}
103
104
106rdesc_destroy(&parser);
107rdesc_grammar_destroy(&grammar);
109}
static char * exblex_current_seminfo(struct exblex *l)
Retrieves the semantic information for the last token.
Definition exblex.h:107
static uint16_t exblex_next(struct exblex *l)
Fetches the next token.
Definition exblex.h:122
static void exblex_init(struct exblex *l, const char *buf, const char *tokens)
Initializes the basic lexer with a null-terminated list of chars.
Definition exblex.h:82
Grammar data structures.
#define rdesc_grammar_init_checked(grammar, production_count, max_alternative_count, max_alternative_size, production_rules)
Grammar initialization instrumented with size assertion.
Definition grammar.h:27
void rdesc_grammar_destroy(struct rdesc_grammar *grammar)
Frees resources allocated by the grammar.
int rdesc_start(struct rdesc *parser, uint16_t start_symbol) _rdesc_wur
Sets start symbol for the next match.
int rdesc_init(struct rdesc *parser, const struct rdesc_grammar *grammar, size_t seminfo_size, void(*token_destroyer)(uint16_t id, void *seminfo)) _rdesc_wur
Initializes a new parser.
enum rdesc_result rdesc_pump(struct rdesc *parser, uint16_t id, void *seminfo) _rdesc_wur
Drives the parsing process, the pump.
void rdesc_destroy(struct rdesc *parser)
Frees memory allocated by the parser and destroys the parser instance.
enum rdesc_result rdesc_resume(struct rdesc *parser) _rdesc_wur
Resume parsing without providing a new token.
struct rdesc_node * rdesc_root(struct rdesc *parser)
Returns the root of the CST.
rdesc_result
Parse operation result codes.
Definition rdesc.h:28
@ RDESC_ENOMEM
Definition rdesc.h:30
@ RDESC_READY
Definition rdesc.h:32
@ RDESC_NOMATCH
Definition rdesc.h:36
@ RDESC_CONTINUE
Definition rdesc.h:34
EXtremely Basic LEXer.
Definition exblex.h:37
const char * buf
Underlying null-terminated input buffer.
Definition exblex.h:39
Grammar definition.
Definition grammar.h:50
Recursive descent parser state.
Definition rdesc.h:40
void rdesc_dump_bnf(FILE *out, const struct rdesc_grammar *grammar, const char *const tk_names[], const char *const nt_names[])
Dumps the grammar in BNF format.
void rdesc_dump_cst(FILE *out, const struct rdesc *parser, void(*node_printer)(FILE *out, const struct rdesc_node *))
Dumps the concrete syntax tree (CST) as a graphviz DOT graph.