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.C
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 //
35 
36 #include <sstream>
37 
38 #include "interpreter.H"
39 
40 using namespace std;
41 
42 namespace reranker {
43 
44 using std::ostringstream;
45 
46 void
47 Interpreter::Eval(StreamTokenizer &st) {
48  // Keeps reading assignment statements until there are no more tokens.
49  while (st.PeekTokenType() != StreamTokenizer::EOF_TYPE) {
50  try {
51  // Read variable name or type specifier.
52  StreamTokenizer::TokenType token_type = st.PeekTokenType();
53  VarMapBase *varmap = env_->GetVarMapForType(st.Peek());
54  bool is_type_specifier = varmap != NULL;
55  if (token_type != StreamTokenizer::IDENTIFIER && !is_type_specifier) {
56  string expected_type =
57  string(StreamTokenizer::TypeName(StreamTokenizer::IDENTIFIER)) +
58  " or type specifier";
59  string found_type = StreamTokenizer::TypeName(token_type);
60  WrongTokenTypeError(st.PeekTokenStart(), expected_type, found_type,
61  st.Peek());
62  }
63 
64  string type = "";
65  if (is_type_specifier) {
66  // Consume and remember the type specifier.
67  st.Next(); // Explicit type could be a concrete type.
68  type = varmap->Name(); // Remember the abstract type.
69 
70  // Check that next token is a variable name.
71  StreamTokenizer::TokenType token_type = st.PeekTokenType();
72  if (token_type != StreamTokenizer::IDENTIFIER) {
73  WrongTokenTypeError(st.PeekTokenStart(), StreamTokenizer::IDENTIFIER,
74  token_type, st.Peek());
75  }
76  }
77 
78  string varname = st.Next();
79 
80  // Next, read equals sign.
81  token_type = st.PeekTokenType();
82  if (st.Peek() != "=") {
83  WrongTokenError(st.PeekTokenStart(), "=", st.Peek(), st.PeekTokenType());
84  }
85 
86  // Consume equals sign.
87  st.Next();
88 
89  if (st.PeekTokenType() == StreamTokenizer::EOF_TYPE) {
90  ostringstream err_ss;
91  err_ss << "Interpreter:" << filename_
92  << ": error: unexpected EOF at stream position "
93  << st.tellg();
94  throw std::runtime_error(err_ss.str());
95  }
96 
97  // Consume and set the value for this variable in the environment.
98  env_->ReadAndSet(varname, st, type);
99 
100  token_type = st.PeekTokenType();
101  if (st.Peek() != ";") {
102  WrongTokenError(st.PeekTokenStart(), ";", st.Peek(), st.PeekTokenType());
103  }
104  // Consume semicolon.
105  st.Next();
106  }
107  catch (std::runtime_error &e) {
108  cerr << "threw exception: " << e.what() << endl;
109  // For now, we simply give up.
110  break;
111  }
112  }
113 }
114 
115 void
116 Interpreter::WrongTokenError(size_t pos,
117  const string &expected,
118  const string &found,
119  StreamTokenizer::TokenType found_type) const {
120  ostringstream err_ss;
121  err_ss << "Interpreter:" << filename_ << ": at stream pos " << pos
122  << " expected token \"" << expected << "\" but found \"" << found
123  << "\" (token type: " << StreamTokenizer::TypeName(found_type)
124  << ")";
125  throw std::runtime_error(err_ss.str());
126 }
127 
128 void
129 Interpreter::WrongTokenTypeError(size_t pos,
130  StreamTokenizer::TokenType expected,
131  StreamTokenizer::TokenType found,
132  const string &token) const {
133  WrongTokenTypeError(pos,
134  StreamTokenizer::TypeName(expected),
135  StreamTokenizer::TypeName(found),
136  token);
137 }
138 
139 void
140 Interpreter::WrongTokenTypeError(size_t pos,
141  const string &expected_type,
142  const string &found_type,
143  const string &token) const {
144  ostringstream err_ss;
145  err_ss << "Interpreter:" << filename_ << ": at stream pos " << pos
146  << " expected token type " << expected_type
147  << " but found " << found_type
148  << "; token=\"" << token << "\"";
149  throw std::runtime_error(err_ss.str());
150 }
151 
152 } // namespace reranker
Provides an interpreter for assigning primitives and Factory-constructible objects to named variables...