Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
interpreter.H
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
36 
37 #ifndef RERANKER_INTERPRETER_H_
38 #define RERANKER_INTERPRETER_H_
39 
40 #include <iostream>
41 #include <fstream>
42 #include <string>
43 #include <unordered_map>
44 #include <unordered_set>
45 
46 #include "environment-impl.H"
47 
48 namespace reranker {
49 
50 using std::iostream;
51 using std::ifstream;
52 
53 class EnvironmentImpl;
54 
165 class Interpreter {
166  public:
170  Interpreter(int debug = 0) {
171  env_ = new EnvironmentImpl(debug);
172  }
173 
175  virtual ~Interpreter() {
176  delete env_;
177  }
178 
180  void Eval(const string &filename) {
181  filename_ = filename;
182  ifstream file(filename_.c_str());
183  Eval(file);
184  }
185 
187  void EvalString(const string& input) {
188  StreamTokenizer st(input);
189  Eval(st);
190  }
191 
193  void Eval(istream &is) {
194  StreamTokenizer st(is);
195  Eval(st);
196  }
197 
198 
199  void PrintEnv(ostream &os) const {
200  env_->Print(os);
201  }
202 
203  void PrintFactories(ostream &os) const {
204  env_->PrintFactories(os);
205  }
206 
216  template<typename T>
217  bool Get(const string &varname, T *value) const {
218  return env_->Get(varname, value);
219  }
220 
227  EnvironmentImpl *env() { return env_; }
228 
229  private:
231  void Eval(StreamTokenizer &st);
232 
233  void WrongTokenError(size_t pos,
234  const string &expected,
235  const string &found,
236  StreamTokenizer::TokenType found_type) const;
237 
238  void WrongTokenTypeError(size_t pos,
241  const string &token) const;
242 
243  void WrongTokenTypeError(size_t pos,
244  const string &expected_type,
245  const string &found_type,
246  const string &token) const;
247 
249  EnvironmentImpl *env_;
250 
253  string filename_;
254 };
255 
256 } // namespace reranker
257 
258 #endif
A simple class for tokenizing a stream of tokens for the formally specified language used to construc...
void Eval(const string &filename)
Evaluates the statements in the specified text file.
Definition: interpreter.H:180
virtual void PrintFactories(ostream &os) const
Prints out a human-readable string with the names of all abstract base types and their concrete imple...
void PrintFactories(ostream &os) const
Definition: interpreter.H:203
virtual ~Interpreter()
Destroys this interpreter.
Definition: interpreter.H:175
virtual void Print(ostream &os) const
Prints a human-readable string of all the variables in this environment, their types and...
bool Get(const string &varname, T *value) const
Retrieves the value of the variable with the specified name and puts into into the object pointed to ...
Provides a set of named variables and their types, as well as the values for those variables...
Interpreter(int debug=0)
Constructs a new instance with the specified debug level.
Definition: interpreter.H:170
Provides an environment for variables and their values, either primitive or Factory-constructible obj...
Provides an interpreter for assigning primitives and Factory-constructible objects to named variables...
Definition: interpreter.H:165
void Eval(istream &is)
Evaluates the statements in the specified stream.
Definition: interpreter.H:193
TokenType
The set of types of tokens read by this stream tokenizer.
void PrintEnv(ostream &os) const
Definition: interpreter.H:199
void EvalString(const string &input)
Evaluates the statements in the specified string.
Definition: interpreter.H:187
EnvironmentImpl * env()
Returns a pointer to the environment of this interpreter.
Definition: interpreter.H:227
bool Get(const string &varname, T *value) const
Retrieves the value of the specified variable.
Definition: interpreter.H:217