Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | List of all members
reranker::Interpreter Class Reference

Provides an interpreter for assigning primitives and Factory-constructible objects to named variables, as well as vectors thereof. More...

#include <interpreter.H>

Public Member Functions

 Interpreter (int debug=0)
 Constructs a new instance with the specified debug level. More...
 
virtual ~Interpreter ()
 Destroys this interpreter. More...
 
void Eval (const string &filename)
 Evaluates the statements in the specified text file. More...
 
void EvalString (const string &input)
 Evaluates the statements in the specified string. More...
 
void Eval (istream &is)
 Evaluates the statements in the specified stream. More...
 
void PrintEnv (ostream &os) const
 
void PrintFactories (ostream &os) const
 
template<typename T >
bool Get (const string &varname, T *value) const
 Retrieves the value of the specified variable. More...
 
EnvironmentImplenv ()
 Returns a pointer to the environment of this interpreter. More...
 

Detailed Description

Provides an interpreter for assigning primitives and Factory-constructible objects to named variables, as well as vectors thereof.

The interpreter maintains an internal environment whereby previously defined variables may be used in the definition of subsequent ones. The syntax of this language extends the syntax of the Factory class, described in the documentation of the Factory::CreateOrDie method.

Statements in this language look like the following:

// This is a comment.
bool b = true; // assigns the value true to the boolean variable "b"
int f = 1; // assigns the int value 1 to the variable "f"
double g = 2.4; // assigns the double value 2.4 to the variable "g"
string n = "foo" // assigns the string value "foo" to the variable "n"
bool[] b_vec = {true, false, true}; // assigns a vector of bool to "b_vec"
// Constructs an object of abstract type Model and assigns it to "m1"
Model m1 = PerceptronModel(name("foo"));
// Constructs an object of type Model and assigns it to "m2", crucially
// using the previously defined string variable "n" as the value for
// the PerceptronModel's name parameter.
Model m2 = PerceptronModel(name(n));
// Constructs a vector of Model objects and assigns it to "m_vec".
Model[] m_vec = {m2, PerceptronModel(name("bar"))};

Additionally, the interpreter can do type inference, so all the type specifiers in the previous examples are optional. For example, one may write the following statements:

b = true; // assigns the value true to the boolean variable "b"
// Constructs an object of abstract type Model and assigns it to "m1"
m1 = PerceptronModel(name("foo"));
// Constructs a vector of Model objects and assigns it to "m_vec".
m_vec = {m1, PerceptronModel(name("bar"))};

Here's an example of using the interpreter after it has read the three statements from the previous example from a file called "example.infact":

#include "interpreter.H"
// ...
i.Eval("example.infact");
shared_ptr<Model> model;
vector<shared_ptr<Model> > model_vector;
bool b;
// The next statement only assigns a value to b if a variable "b"
// exists in the interpreter's environment.
i.Get("b", &b);
i.Get("m1", &model);
i.Get("m_vec", &model_vector);

More formally, a statement in this language must conform to the following grammar, defined on top of the BNF syntax in the documentation of the Factory::CreateOrDie method:

<statement_list> ::= [ <statement> ]*
<statement> ::= [ <type_specifier> ] <variable_name> '=' <value> ';'
<type_specifier> ::=
"bool" | "int" | "string" | "double" | "bool[]" | "int[]" "string[]" | "double[]" | T | T[]
where T is any Factory-constructible type.
<variable_name> ::= any valid C++ identifier
<value> ::= <literal> | '{' <literal_list> '}' |
<spec> | '{' <spec_list> '}'

The above grammar doesn’t contain rules covering C++ style line comments, but they have the same behavior in this language as they do in C++, i.e., everything after the // to the end of the current line is treated as a comment and ignored. There are no C-style comments in this language.

Definition at line 165 of file interpreter.H.

Constructor & Destructor Documentation

reranker::Interpreter::Interpreter ( int  debug = 0)
inline

Constructs a new instance with the specified debug level.

The wrapped Environment will also have the specified debug level.

Definition at line 170 of file interpreter.H.

virtual reranker::Interpreter::~Interpreter ( )
inlinevirtual

Destroys this interpreter.

Definition at line 175 of file interpreter.H.

Member Function Documentation

EnvironmentImpl* reranker::Interpreter::env ( )
inline

Returns a pointer to the environment of this interpreter.

Crucially, this method returns a pointer to the Environment implementation class, EnvironmentImpl, so that its templated EnvironmentImpl::Get method may be invoked.

Definition at line 227 of file interpreter.H.

void reranker::Interpreter::Eval ( const string &  filename)
inline

Evaluates the statements in the specified text file.

Definition at line 180 of file interpreter.H.

void reranker::Interpreter::Eval ( istream &  is)
inline

Evaluates the statements in the specified stream.

Definition at line 193 of file interpreter.H.

void reranker::Interpreter::EvalString ( const string &  input)
inline

Evaluates the statements in the specified string.

Definition at line 187 of file interpreter.H.

template<typename T >
bool reranker::Interpreter::Get ( const string &  varname,
T *  value 
) const
inline

Retrieves the value of the specified variable.

It is an error if the type of the specified pointer to a value object is different from the specified variable in this interpreter’s environment.

Template Parameters
thetype of value object being set by this method
Parameters
varnamethe name of the variable for which to retrieve the value
valuea pointer to the object whose value to be set by this method

Definition at line 217 of file interpreter.H.

void reranker::Interpreter::PrintEnv ( ostream &  os) const
inline

Definition at line 199 of file interpreter.H.

void reranker::Interpreter::PrintFactories ( ostream &  os) const
inline

Definition at line 203 of file interpreter.H.


The documentation for this class was generated from the following files: