OR-Tools  9.6
parser.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 #include <string>
18 
19 #include "ortools/flatzinc/parser.tab.hh"
20 
21 // Declare external functions in the flatzinc.tab.cc generated file.
24  void* scanner);
25 extern int orfz_lex_init(void** scanner);
26 extern int orfz_lex_destroy(void* scanner);
27 extern void orfz_set_in(FILE* in_str, void* yyscanner);
28 // Declare external functions and structures in the flatzinc.yy.cc
29 // generated file.
30 struct yy_buffer_state;
31 extern yy_buffer_state* orfz__scan_bytes(const char* input, int size,
32  void* scanner);
33 extern void orfz__delete_buffer(yy_buffer_state* b, void* scanner);
34 
35 namespace operations_research {
36 namespace fz {
37 // ----- public parsing API -----
38 
39 bool ParseFlatzincFile(const std::string& filename, Model* model) {
40  // Init.
41  FILE* const input = fopen(filename.c_str(), "r");
42  if (input == nullptr) {
43  LOG(INFO) << "Could not open file '" << filename << "'";
44  return false;
45  }
47  // Add known constants.
48  context.integer_map["true"] = 1;
49  context.integer_map["false"] = 0;
50  bool ok = true;
51  void* scanner = nullptr;
52  orfz_lex_init(&scanner);
53  orfz_set_in(input, scanner);
54  // Parse.
55  orfz_parse(&context, model, &ok, scanner);
56  // Clean up.
57  if (scanner != nullptr) {
58  orfz_lex_destroy(scanner);
59  }
60  fclose(input);
61  return ok;
62 }
63 
64 bool ParseFlatzincString(const std::string& input, Model* model) {
65  // Init.
67  // Add known constants.
68  context.integer_map["true"] = 1;
69  context.integer_map["false"] = 0;
70  bool ok = true;
71  void* scanner = nullptr;
72  orfz_lex_init(&scanner);
73  yy_buffer_state* const string_buffer =
74  orfz__scan_bytes(input.data(), input.size(), scanner);
75  // Parse.
76  orfz_parse(&context, model, &ok, scanner);
77  // Clean up.
78  if (string_buffer != nullptr) {
79  orfz__delete_buffer(string_buffer, scanner);
80  }
81  if (scanner != nullptr) {
82  orfz_lex_destroy(scanner);
83  }
84  return ok;
85 }
86 } // namespace fz
87 } // namespace operations_research
int64_t b
GRBmodel * model
GurobiMPCallbackContext * context
bool ParseFlatzincString(const std::string &input, Model *model)
Definition: parser.cc:64
bool ParseFlatzincFile(const std::string &filename, Model *model)
Definition: parser.cc:39
Collection of objects used to extend the Constraint Solver library.
int orfz_lex_destroy(void *scanner)
int orfz_parse(operations_research::fz::ParserContext *parser, operations_research::fz::Model *model, bool *ok, void *scanner)
int orfz_lex_init(void **scanner)
void orfz_set_in(FILE *in_str, void *yyscanner)
yy_buffer_state * orfz__scan_bytes(const char *input, int size, void *scanner)
void orfz__delete_buffer(yy_buffer_state *b, void *scanner)
static int input(yyscan_t yyscanner)