Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Parser.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_PARSER_HPP
11 #define TEUCHOS_PARSER_HPP
12 
17 #include <Teuchos_TableDecl.hpp>
18 #include <Teuchos_Grammar.hpp>
19 
20 namespace Teuchos {
21 
22 enum ActionKind {
23  ACTION_NONE,
24  ACTION_SHIFT,
25  ACTION_REDUCE
26 };
27 
28 struct Action {
29  ActionKind kind;
30  union {
31  int production;
32  int next_state;
33  };
34 };
35 
36 #ifdef HAVE_TEUCHOSCORE_CXX11
37 extern template struct Table<Action>;
38 #endif
39 
40 struct Parser {
41  GrammarPtr grammar;
42  /* (state x terminal) -> action */
43  Table<Action> terminal_table;
44  /* (state x non-terminal) -> new state */
45  Table<int> nonterminal_table;
46  Parser() {}
47  Parser(GrammarPtr g, int nstates_reserve);
48 };
49 
50 int add_state(Parser& p);
51 int get_nstates(Parser const& p);
52 void add_terminal_action(Parser& p, int state, int terminal, Action action);
53 void add_nonterminal_action(Parser& p, int state, int nonterminal, int next_state);
54 Action const& get_action(Parser const& p, int state, int terminal);
55 int execute_action(Parser const& p, std::vector<int>& stack, Action const& action);
56 GrammarPtr const& get_grammar(Parser const& p);
57 
73 class ParserFail: public std::invalid_argument {
74  public:
75  ParserFail(const std::string& msg);
76 };
77 
103 Parser make_lalr1_parser(GrammarPtr grammar, bool verbose = false);
104 
105 }
106 
107 #endif
Parser make_lalr1_parser(GrammarPtr grammar, bool verbose)
Tries to create LALR(1) parser tables for a given grammar.
Tries to create LALR(1) parser tables for a given grammar.
Smart reference counting pointer class for automatic garbage collection.