OR-Tools  8.1
parser.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <cstdio>
17 
18 #include "ortools/flatzinc/parser.tab.hh"
19 
20 // Declare external functions in the flatzinc.tab.cc generated file.
23  void* scanner);
24 extern int orfz_lex_init(void** scanner);
25 extern int orfz_lex_destroy(void* scanner);
26 extern void orfz_set_in(FILE* in_str, void* yyscanner);
27 // Declare external functions and structures in the flatzinc.yy.cc
28 // generated file.
29 struct yy_buffer_state;
30 extern yy_buffer_state* orfz__scan_bytes(const char* input, int size,
31  void* scanner);
32 extern void orfz__delete_buffer(yy_buffer_state* b, void* scanner);
33 
34 namespace operations_research {
35 namespace fz {
36 // ----- public parsing API -----
37 
38 bool ParseFlatzincFile(const std::string& filename, Model* model) {
39  // Init.
40  FILE* const input = fopen(filename.c_str(), "r");
41  if (input == nullptr) {
42  LOG(INFO) << "Could not open file '" << filename << "'";
43  return false;
44  }
46  bool ok = true;
47  void* scanner = nullptr;
48  orfz_lex_init(&scanner);
49  orfz_set_in(input, scanner);
50  // Parse.
51  orfz_parse(&context, model, &ok, scanner);
52  // Clean up.
53  if (scanner != nullptr) {
54  orfz_lex_destroy(scanner);
55  }
56  fclose(input);
57  return ok;
58 }
59 
60 bool ParseFlatzincString(const std::string& input, Model* model) {
61  // Init.
63  bool ok = true;
64  void* scanner = nullptr;
65  orfz_lex_init(&scanner);
66  yy_buffer_state* const string_buffer =
67  orfz__scan_bytes(input.data(), input.size(), scanner);
68  // Parse.
69  orfz_parse(&context, model, &ok, scanner);
70  // Clean up.
71  if (string_buffer != nullptr) {
72  orfz__delete_buffer(string_buffer, scanner);
73  }
74  if (scanner != nullptr) {
75  orfz_lex_destroy(scanner);
76  }
77  return ok;
78 }
79 } // namespace fz
80 } // namespace operations_research
INFO
const int INFO
Definition: log_severity.h:31
LOG
#define LOG(severity)
Definition: base/logging.h:420
yy_buffer_state
Definition: parser.yy.cc:450
orfz_lex_init
int orfz_lex_init(void **scanner)
operations_research::fz::Model
Definition: flatzinc/model.h:315
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::fz::ParseFlatzincString
bool ParseFlatzincString(const std::string &input, Model *model)
Definition: parser.cc:60
context
GurobiMPCallbackContext * context
Definition: gurobi_interface.cc:509
orfz_lex_destroy
int orfz_lex_destroy(void *scanner)
operations_research::fz::ParserContext
Definition: parser_util.h:29
orfz__delete_buffer
void orfz__delete_buffer(yy_buffer_state *b, void *scanner)
model
GRBmodel * model
Definition: gurobi_interface.cc:269
orfz__scan_bytes
yy_buffer_state * orfz__scan_bytes(const char *input, int size, void *scanner)
parser.h
input
static int input(yyscan_t yyscanner)
b
int64 b
Definition: constraint_solver/table.cc:43
operations_research::fz::ParseFlatzincFile
bool ParseFlatzincFile(const std::string &filename, Model *model)
Definition: parser.cc:38
orfz_parse
int orfz_parse(operations_research::fz::ParserContext *parser, operations_research::fz::Model *model, bool *ok, void *scanner)
orfz_set_in
void orfz_set_in(FILE *in_str, void *yyscanner)