OR-Tools  8.2
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);
24extern int orfz_lex_init(void** scanner);
25extern int orfz_lex_destroy(void* scanner);
26extern void orfz_set_in(FILE* in_str, void* yyscanner);
27// Declare external functions and structures in the flatzinc.yy.cc
28// generated file.
29struct yy_buffer_state;
30extern yy_buffer_state* orfz__scan_bytes(const char* input, int size,
31 void* scanner);
32extern void orfz__delete_buffer(yy_buffer_state* b, void* scanner);
33
34namespace operations_research {
35namespace fz {
36// ----- public parsing API -----
37
38bool 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
60bool 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
#define LOG(severity)
Definition: base/logging.h:420
GRBmodel * model
GurobiMPCallbackContext * context
const int INFO
Definition: log_severity.h:31
bool ParseFlatzincString(const std::string &input, Model *model)
Definition: parser.cc:60
bool ParseFlatzincFile(const std::string &filename, Model *model)
Definition: parser.cc:38
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
int orfz_lex_destroy(void *scanner)
int orfz_parse(operations_research::fz::ParserContext *parser, operations_research::fz::Model *model, bool *ok, void *scanner)
yy_buffer_state * orfz__scan_bytes(const char *input, int size, void *scanner)
int orfz_lex_init(void **scanner)
void orfz_set_in(FILE *in_str, void *yyscanner)
void orfz__delete_buffer(yy_buffer_state *b, void *scanner)
static int input(yyscan_t yyscanner)