-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparsegen_shift_reduce_tables.hpp
44 lines (37 loc) · 1.11 KB
/
parsegen_shift_reduce_tables.hpp
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
#pragma once
#include <stack>
#include "parsegen_grammar.hpp"
#include "parsegen_table.hpp"
namespace parsegen {
struct action {
enum class kind {
none,
shift,
reduce,
skip
};
parsegen::action::kind kind;
union {
int production;
int next_state;
};
};
struct shift_reduce_tables {
grammar_ptr grammar;
/* (state x terminal) -> action */
table<action> terminal_table;
/* (state x non-terminal) -> new state */
table<int> nonterminal_table;
shift_reduce_tables() = default;
shift_reduce_tables(grammar_ptr g, int nstates_reserve);
};
int add_state(shift_reduce_tables& p);
int get_nstates(shift_reduce_tables const& p);
void add_terminal_action(shift_reduce_tables& p, int state, int terminal, action action);
void add_nonterminal_action(
shift_reduce_tables& p, int state, int nonterminal, int next_state);
action const& get_action(shift_reduce_tables const& p, int state, int terminal);
int execute_action(
shift_reduce_tables const& p, std::vector<int>& stack, action const& action);
grammar_ptr const& get_grammar(shift_reduce_tables const& p);
} // namespace parsegen