Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parser.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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
23
24// Declare external functions in the flatzinc.tab.cc generated file.
27 void* scanner);
28extern int orfz_lex_init(void** scanner);
29extern int orfz_lex_destroy(void* scanner);
30extern void orfz_set_in(FILE* in_str, void* yyscanner);
31// Declare external functions and structures in the flatzinc.yy.cc
32// generated file.
33struct yy_buffer_state;
34extern yy_buffer_state* orfz__scan_bytes(const char* input, int size,
35 void* scanner);
36extern void orfz__delete_buffer(yy_buffer_state* b, void* scanner);
37
38namespace operations_research {
39namespace fz {
40// ----- public parsing API -----
41
42bool ParseFlatzincFile(const std::string& filename, Model* model) {
43 // Init.
44 FILE* const input = fopen(filename.c_str(), "r");
45 if (input == nullptr) {
46 LOG(INFO) << "Could not open file '" << filename << "'";
47 return false;
48 }
50 // Add known constants.
51 context.integer_map["true"] = 1;
52 context.integer_map["false"] = 0;
53 bool ok = true;
54 void* scanner = nullptr;
55 orfz_lex_init(&scanner);
56 orfz_set_in(input, scanner);
57 // Parse.
58 orfz_parse(&context, model, &ok, scanner);
59 // Clean up.
60 if (scanner != nullptr) {
61 orfz_lex_destroy(scanner);
62 }
63 fclose(input);
64 return ok;
65}
66
67bool ParseFlatzincString(const std::string& input, Model* model) {
68 // Init.
70 // Add known constants.
71 context.integer_map["true"] = 1;
72 context.integer_map["false"] = 0;
73 bool ok = true;
74 void* scanner = nullptr;
75 orfz_lex_init(&scanner);
76 yy_buffer_state* const string_buffer =
77 orfz__scan_bytes(input.data(), input.size(), scanner);
78 // Parse.
79 orfz_parse(&context, model, &ok, scanner);
80 // Clean up.
81 if (string_buffer != nullptr) {
82 orfz__delete_buffer(string_buffer, scanner);
83 }
84 if (scanner != nullptr) {
85 orfz_lex_destroy(scanner);
86 }
87 return ok;
88}
89} // namespace fz
90} // namespace operations_research
IntegerValue size
int64_t b
Definition table.cc:45
GRBmodel * model
GurobiMPCallbackContext * context
bool ParseFlatzincString(const std::string &input, Model *model)
Definition parser.cc:67
bool ParseFlatzincFile(const std::string &filename, Model *model)
--— public parsing API --—
Definition parser.cc:42
In SWIG mode, we don't want anything besides these top-level includes.
int orfz_lex_destroy(void *scanner)
int orfz_parse(operations_research::fz::ParserContext *parser, operations_research::fz::Model *model, bool *ok, void *scanner)
Declare external functions in the flatzinc.tab.cc generated file.
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)
This is the context used during parsing.
Definition parser_util.h:31
absl::flat_hash_map< std::string, int64_t > integer_map
Definition parser_util.h:32