3 #include "Teuchos_Table.hpp"
4 #include "Teuchos_make_lalr1_parser.hpp"
8 template struct Table<Action>;
10 Parser::Parser(GrammarPtr g,
int nstates_reserve):
12 terminal_table(g->nterminals, nstates_reserve),
13 nonterminal_table(get_nnonterminals(*g), nstates_reserve) {
16 int get_nstates(Parser
const& p) {
17 return get_nrows(p.terminal_table);
20 int add_state(Parser& p) {
21 int state = get_nstates(p);
22 resize(p.terminal_table, state + 1, get_ncols(p.terminal_table));
23 resize(p.nonterminal_table, state + 1, get_ncols(p.nonterminal_table));
24 for (
int t = 0; t < p.grammar->nterminals; ++t) {
26 action.kind = ACTION_NONE;
27 at(p.terminal_table, state, t) = action;
29 for (
int nt = 0; nt < get_nnonterminals(*(p.grammar)); ++nt) {
30 at(p.nonterminal_table, state, nt) = -1;
35 void add_terminal_action(Parser& p,
int state,
int terminal, Action action) {
36 TEUCHOS_ASSERT(at(p.terminal_table, state, terminal).kind == ACTION_NONE);
38 if (action.kind == ACTION_SHIFT) {
45 at(p.terminal_table, state, terminal) = action;
48 void add_nonterminal_action(Parser& p,
int state,
int nonterminal,
int next_state) {
52 at(p.nonterminal_table, state, nonterminal) = next_state;
55 Action
const& get_action(Parser
const& p,
int state,
int terminal) {
56 return at(p.terminal_table, state, terminal);
59 int execute_action(Parser
const& p, std::vector<int>& stack, Action
const& action) {
61 if (action.kind == ACTION_SHIFT) {
62 stack.push_back(action.next_state);
64 const Grammar::Production& prod = at(p.grammar->productions, action.production);
65 for (
int i = 0; i < size(prod.rhs); ++i) stack.pop_back();
67 const Grammar& grammar = *(p.grammar);
68 int nt = as_nonterminal(grammar, prod.lhs);
70 int next_state = at(p.nonterminal_table, stack.back(), nt);
71 stack.push_back(next_state);
76 GrammarPtr
const& get_grammar(Parser
const& p) {
return p.grammar; }
78 ParserFail::ParserFail(
const std::string& msg):
79 std::invalid_argument(msg) {
Declares Teuchos::Parser, ParserFail and make_lalr1_parser.
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.