1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Jip J. Dekker <jip.dekker@monash.edu>
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
%define api.header.include {<minizinc/support/regex_parser.tab.hh>}
%{
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <minizinc/support/regex.hh>
using namespace Gecode;
using namespace MiniZinc;
typedef struct yy_buffer_state *YY_BUFFER_STATE;
YY_BUFFER_STATE regex_yy_scan_string ( const char* yy_str );
extern int yylex();
extern FILE* yyin;
typedef struct REContext{
REG* expr;
const IntSetVal& dom;
} REContext;
void yyerror(REContext& ctx, const char* s);
%}
%union {
int iValue;
char* sValue;
std::set<int>* setValue;
Gecode::REG* rValue;
}
%parse-param {REContext& ctx}
%token<iValue> R_INTEGER
%token R_GROUP_OPEN "("
%token R_GROUP_CLOSE ")"
%token R_STAR "*"
%token R_PLUS "+"
%token R_ANY "."
%token R_UNION "|"
%token R_OPTIONAL "?"
%token R_QUANT_OPEN "{"
%token R_QUANT_CLOSE "}"
%token R_COMMA ","
%token R_CLASS_OPEN "["
%token R_CLASS_CLOSE "]"
%token R_CLASS_RANGE "-"
%token R_CLASS_NEG "^"
%token<sValue> R_IDENTIFIER
%type<rValue> regex expression term factor atom
%type<setValue> set_item set_items
%start regex
%%
regex:
expression
{
*ctx.expr = (*$1);
delete $1;
}
expression:
term
| term "|" expression
{
*$1 = *$1 | *$3;
delete $3;
$$ = $1;
}
term:
factor
| factor term
{
*$1 = *$1 + *$2;
delete $2;
$$ = $1;
}
factor:
atom
| atom "*"
{
*$1 = *(*$1);
$$ = $1;
}
| atom "+"
{
*$1 = +(*$1);
$$ = $1;
}
| atom "?"
{
*$1 = (*$1)(0, 1);
$$ = $1;
}
| atom "{" R_INTEGER "}"
{
*$1 = (*$1)($3, $3);
$$ = $1;
}
| atom "{" R_INTEGER "," "}"
{
*$1 = (*$1)($3);
$$ = $1;
}
| atom "{" R_INTEGER "," R_INTEGER "}"
{
*$1 = (*$1)($3, $5);
$$ = $1;
}
atom:
R_INTEGER
{ $$ = new REG($1); }
| "."
{
IntArgs range = IntArgs::create(static_cast<int>(ctx.dom.max().toInt() - ctx.dom.min().toInt()) + 1,
static_cast<int>(ctx.dom.min().toInt()));
$$ = new REG(range);
}
| "[" set_items "]"
{
std::vector<int> v;
v.reserve($2->size());
for(auto i : *$2) {
v.push_back(i);
}
delete $2;
$$ = new REG(IntArgs(v));
}
| "[" "^" set_items "]"
{
std::vector<int> diff;
std::set<int> domain;
for(auto i = ctx.dom.min().toInt(); i<=ctx.dom.max().toInt(); ++i) {
domain.insert(static_cast<int>(i));
}
std::set_difference(
domain.begin(), domain.end(),
$3->begin(), $3->end(),
std::inserter(diff, diff.begin())
);
delete $3;
$$ = new REG(IntArgs(diff));
}
| "(" expression ")"
{ $$ = $2; }
set_items:
set_item
| set_item set_items
{
$$ = $1;
for (auto i : *$2) {
$1->insert(i);
}
delete $2;
}
set_item:
R_INTEGER
{
$$ = new std::set<int>({$1});
}
| R_INTEGER "-" R_INTEGER
{
int from = $1;
int to = $3;
if (to < from) {
std::swap(from,to);
}
$$ = new std::set<int>;
for(int i = from; i<=to; ++i) {
$$->insert(i);
}
}
%%
void yyerror(REContext& ctx, const char* s) {
// TODO: Add error location
throw std::runtime_error("Cannot parse regular expression: " + std::string(s));
}
std::unique_ptr<REG> regex_from_string(const std::string& regex_str, const IntSetVal& domain) {
REG* expr = new REG();
regex_yy_scan_string(regex_str.c_str());
REContext rctx = REContext{expr, domain};
int err = yyparse(rctx);
if (err != 0) {
throw std::runtime_error("Cannot parse regular expression, error code " + std::to_string(err));
}
return std::unique_ptr<REG>(expr);
}
|