Google OR-Tools
v9.15
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-2025 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
14
#include "
ortools/flatzinc/parser.h
"
15
16
#include <cstdio>
17
#include <string>
18
19
#include "absl/log/log.h"
20
#include "
ortools/flatzinc/model.h
"
21
#include "
ortools/flatzinc/parser.tab.hh
"
22
#include "
ortools/flatzinc/parser_util.h
"
23
24
// Declare external functions in the flatzinc.tab.cc generated file.
25
extern
int
orfz_parse
(
operations_research::fz::ParserContext
* parser,
26
operations_research::fz::Model
* model,
bool
* ok,
27
void
* scanner);
28
extern
int
orfz_lex_init
(
void
** scanner);
29
extern
int
orfz_lex_destroy
(
void
* scanner);
30
extern
void
orfz_set_in
(FILE* in_str,
void
* yyscanner);
31
// Declare external functions and structures in the flatzinc.yy.cc
32
// generated file.
33
struct
yy_buffer_state
;
34
extern
yy_buffer_state
*
orfz__scan_bytes
(
const
char
*
input
,
int
size,
35
void
* scanner);
36
extern
void
orfz__delete_buffer
(
yy_buffer_state
* b,
void
* scanner);
37
38
namespace
operations_research
{
39
namespace
fz {
40
// ----- public parsing API -----
41
42
bool
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
}
49
ParserContext
context;
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
67
bool
ParseFlatzincString
(
const
std::string&
input
,
Model
* model) {
68
// Init.
69
ParserContext
context;
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
operations_research::fz::Model
Definition
model.h:345
model.h
operations_research::fz::ParseFlatzincString
bool ParseFlatzincString(const std::string &input, Model *model)
Definition
parser.cc:67
operations_research::fz::ParseFlatzincFile
bool ParseFlatzincFile(const std::string &filename, Model *model)
Definition
parser.cc:42
operations_research
OR-Tools root namespace.
Definition
binary_indexed_tree.h:21
orfz_lex_destroy
int orfz_lex_destroy(void *scanner)
orfz_parse
int orfz_parse(operations_research::fz::ParserContext *parser, operations_research::fz::Model *model, bool *ok, void *scanner)
orfz__scan_bytes
yy_buffer_state * orfz__scan_bytes(const char *input, int size, void *scanner)
orfz_lex_init
int orfz_lex_init(void **scanner)
orfz_set_in
void orfz_set_in(FILE *in_str, void *yyscanner)
orfz__delete_buffer
void orfz__delete_buffer(yy_buffer_state *b, void *scanner)
parser.h
parser.tab.hh
input
static int input(yyscan_t yyscanner)
parser_util.h
operations_research::fz::ParserContext
Definition
parser_util.h:30
operations_research::fz::ParserContext::integer_map
absl::flat_hash_map< std::string, int64_t > integer_map
Definition
parser_util.h:31
yy_buffer_state
Definition
parser.yy.cc:450
ortools
flatzinc
parser.cc
Generated by
1.15.0