Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Parser.cpp
1 #include "Teuchos_Parser.hpp"
2 
3 #include "Teuchos_Table.hpp"
4 #include "Teuchos_make_lalr1_parser.hpp"
5 
6 namespace Teuchos {
7 
8 template struct Table<Action>;
9 
10 Parser::Parser(GrammarPtr g, int nstates_reserve):
11  grammar(g),
12  terminal_table(g->nterminals, nstates_reserve),
13  nonterminal_table(get_nnonterminals(*g), nstates_reserve) {
14 }
15 
16 int get_nstates(Parser const& p) {
17  return get_nrows(p.terminal_table);
18 }
19 
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) {
25  Action action;
26  action.kind = ACTION_NONE;
27  at(p.terminal_table, state, t) = action;
28  }
29  for (int nt = 0; nt < get_nnonterminals(*(p.grammar)); ++nt) {
30  at(p.nonterminal_table, state, nt) = -1;
31  }
32  return state;
33 }
34 
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);
37  TEUCHOS_ASSERT(action.kind != ACTION_NONE);
38  if (action.kind == ACTION_SHIFT) {
39  TEUCHOS_ASSERT(0 <= action.next_state);
40  TEUCHOS_ASSERT(action.next_state < get_nstates(p));
41  } else {
42  TEUCHOS_ASSERT(0 <= action.production);
43  TEUCHOS_ASSERT(action.production < size(p.grammar->productions));
44  }
45  at(p.terminal_table, state, terminal) = action;
46 }
47 
48 void add_nonterminal_action(Parser& p, int state, int nonterminal, int next_state) {
49  TEUCHOS_ASSERT(0 <= next_state);
50  TEUCHOS_ASSERT(next_state < get_nstates(p));
51  TEUCHOS_ASSERT(at(p.nonterminal_table, state, nonterminal) == -1);
52  at(p.nonterminal_table, state, nonterminal) = next_state;
53 }
54 
55 Action const& get_action(Parser const& p, int state, int terminal) {
56  return at(p.terminal_table, state, terminal);
57 }
58 
59 int execute_action(Parser const& p, std::vector<int>& stack, Action const& action) {
60  TEUCHOS_ASSERT(action.kind != ACTION_NONE);
61  if (action.kind == ACTION_SHIFT) {
62  stack.push_back(action.next_state);
63  } else {
64  const Grammar::Production& prod = at(p.grammar->productions, action.production);
65  for (int i = 0; i < size(prod.rhs); ++i) stack.pop_back();
66  TEUCHOS_ASSERT(p.grammar.get());
67  const Grammar& grammar = *(p.grammar);
68  int nt = as_nonterminal(grammar, prod.lhs);
69  TEUCHOS_ASSERT(!stack.empty());
70  int next_state = at(p.nonterminal_table, stack.back(), nt);
71  stack.push_back(next_state);
72  }
73  return stack.back();
74 }
75 
76 GrammarPtr const& get_grammar(Parser const& p) { return p.grammar; }
77 
78 ParserFail::ParserFail(const std::string& msg):
79  std::invalid_argument(msg) {
80 }
81 
82 } // end namespace Teuchos
Declares Teuchos::Parser, ParserFail and make_lalr1_parser.
#define TEUCHOS_ASSERT(assertion_test)
This macro is throws when an assert fails.