2008-01-08 14:52:32 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Eliot
|
|
|
|
* Copyright (C) 2005-2007 Antoine Fraboulet
|
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <list>
|
2008-07-20 14:15:51 +02:00
|
|
|
#include <algorithm>
|
2008-01-08 14:52:32 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
# include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "dic.h"
|
|
|
|
#include "regexp.h"
|
|
|
|
#include "automaton.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#ifdef DEBUG_AUTOMATON
|
|
|
|
# define DMSG(a) (a)
|
|
|
|
#else
|
|
|
|
# define DMSG(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_TRANSITION_LETTERS 256
|
|
|
|
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
/* ************************************************** *
|
|
|
|
Definition of the automaton state
|
|
|
|
* ************************************************** */
|
|
|
|
|
|
|
|
static string idToString(const set<uint64_t> &iId);
|
|
|
|
|
|
|
|
class State
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
State(const set<uint64_t> iId) : m_id(iId) { init(); }
|
|
|
|
State(uint64_t iId)
|
|
|
|
{
|
|
|
|
m_id.insert(iId);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
const set<uint64_t> & getId() const { return m_id; }
|
|
|
|
|
|
|
|
// FIXME: should be private
|
|
|
|
bool m_accept;
|
|
|
|
int id_static;
|
|
|
|
State * m_next[MAX_TRANSITION_LETTERS];
|
|
|
|
|
|
|
|
private:
|
2008-07-27 17:28:50 +02:00
|
|
|
/**
|
|
|
|
* Id of the state. For the first automaton, each ID contains only 1
|
|
|
|
* integer, but the ID of the deterministic automaton will contain
|
|
|
|
* several integers, according to the usual "determinization" algorithm.
|
|
|
|
*/
|
2008-07-27 15:32:47 +02:00
|
|
|
set<uint64_t> m_id;
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
m_accept = false;
|
|
|
|
id_static = 0;
|
|
|
|
memset(m_next, 0, sizeof(State*) * MAX_TRANSITION_LETTERS);
|
|
|
|
DMSG(printf("** state %s creation\n", idToString(m_id).c_str()));
|
|
|
|
}
|
|
|
|
};
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/* ************************************************** *
|
|
|
|
Helper class, allowing to build a NFA, then a DFA
|
|
|
|
* ************************************************** */
|
|
|
|
|
|
|
|
class AutomatonHelper
|
|
|
|
{
|
|
|
|
public:
|
2008-07-27 15:32:47 +02:00
|
|
|
AutomatonHelper(State * iInitState);
|
2008-01-08 14:52:32 +01:00
|
|
|
~AutomatonHelper();
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
State * getInitState() const { return m_initState; }
|
2008-01-08 14:52:32 +01:00
|
|
|
#ifdef DEBUG_AUTOMATON
|
|
|
|
void dump(const string &iFileName) const;
|
|
|
|
#endif
|
|
|
|
|
2008-07-13 09:55:47 +02:00
|
|
|
static AutomatonHelper *ps2nfa(uint64_t iInitState, int *ptl, uint64_t *PS);
|
2008-01-08 14:52:32 +01:00
|
|
|
static AutomatonHelper *nfa2dfa(const AutomatonHelper &iNfa,
|
2008-07-27 17:28:50 +02:00
|
|
|
const searchRegExpLists &iList);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/// List of states
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *> m_states;
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Initial state of the automaton
|
2008-07-27 15:32:47 +02:00
|
|
|
State * m_initState;
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
void addState(State * s);
|
|
|
|
State * getState(const set<uint64_t> &iId) const;
|
2008-01-08 14:52:32 +01:00
|
|
|
void printNodes(FILE* f) const;
|
|
|
|
void printEdges(FILE* f) const;
|
2008-07-27 15:32:47 +02:00
|
|
|
void setAccept(State * s) const;
|
2008-07-27 17:28:50 +02:00
|
|
|
set<uint64_t> getSuccessor(const set<uint64_t> &S, int letter,
|
|
|
|
const searchRegExpLists &iList) const;
|
2008-01-08 14:52:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* ************************************************** *
|
|
|
|
Definition of the Automaton class
|
|
|
|
* ************************************************** */
|
|
|
|
|
2008-07-27 17:28:50 +02:00
|
|
|
Automaton::Automaton(uint64_t iInitState, int *ptl, uint64_t *PS,
|
|
|
|
const searchRegExpLists &iList)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
AutomatonHelper *nfa = AutomatonHelper::ps2nfa(iInitState, ptl, PS);
|
|
|
|
DMSG(printf("\n non deterministic automaton OK \n\n"));
|
|
|
|
DMSG(nfa->dump("auto_nfa"));
|
|
|
|
|
|
|
|
AutomatonHelper *dfa = AutomatonHelper::nfa2dfa(*nfa, iList);
|
|
|
|
DMSG(printf("\n deterministic automaton OK \n\n"));
|
|
|
|
DMSG(dfa->dump("auto_dfa"));
|
|
|
|
|
|
|
|
finalize(*dfa);
|
|
|
|
DMSG(printf("\n final automaton OK \n\n"));
|
2008-07-27 15:32:47 +02:00
|
|
|
DMSG(dump("auto_fin"));
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
delete nfa;
|
|
|
|
delete dfa;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Automaton::~Automaton()
|
|
|
|
{
|
|
|
|
delete[] m_acceptors;
|
2008-07-27 17:28:50 +02:00
|
|
|
for (unsigned int i = 0; i <= m_nbStates; i++)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
delete[] m_transitions[i];
|
|
|
|
}
|
|
|
|
delete[] m_transitions;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Automaton::finalize(const AutomatonHelper &iHelper)
|
|
|
|
{
|
|
|
|
/* Creation */
|
|
|
|
m_nbStates = iHelper.m_states.size();
|
|
|
|
m_acceptors = new bool[m_nbStates + 1];
|
|
|
|
memset(m_acceptors, 0, (m_nbStates + 1) * sizeof(bool));
|
|
|
|
m_transitions = new int*[m_nbStates + 1];
|
2008-07-27 17:28:50 +02:00
|
|
|
for (unsigned int i = 0; i <= m_nbStates; i++)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
m_transitions[i] = new int[MAX_TRANSITION_LETTERS];
|
|
|
|
memset(m_transitions[i], 0, MAX_TRANSITION_LETTERS * sizeof(int));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create new id for states */
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *>::const_iterator it;
|
2008-01-08 14:52:32 +01:00
|
|
|
int i;
|
|
|
|
for (i = 1, it = iHelper.m_states.begin();
|
|
|
|
it != iHelper.m_states.end(); it++, i++)
|
|
|
|
{
|
|
|
|
(*it)->id_static = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build new automaton */
|
|
|
|
for (it = iHelper.m_states.begin(); it != iHelper.m_states.end(); it++)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
State * s = *it;
|
2008-01-08 14:52:32 +01:00
|
|
|
int i = s->id_static;
|
|
|
|
|
|
|
|
if (s == iHelper.getInitState())
|
|
|
|
m_init = i;
|
2008-07-27 15:32:47 +02:00
|
|
|
if (s->m_accept)
|
2008-01-08 14:52:32 +01:00
|
|
|
m_acceptors[i] = true;
|
|
|
|
|
|
|
|
for (int l = 0; l < MAX_TRANSITION_LETTERS; l++)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
if (s->m_next[l])
|
|
|
|
m_transitions[i][l] = s->m_next[l]->id_static;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Automaton::dump(const string &iFileName) const
|
|
|
|
{
|
|
|
|
FILE *f = fopen(iFileName.c_str(), "w");
|
|
|
|
fprintf(f, "digraph automaton {\n");
|
2008-07-27 17:28:50 +02:00
|
|
|
for (unsigned int i = 1; i <= m_nbStates; i++)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
fprintf(f, "\t%d [label = \"%d\"", i, i);
|
|
|
|
if (i == m_init)
|
|
|
|
fprintf(f, ", style = filled, color=lightgrey");
|
|
|
|
if (accept(i))
|
|
|
|
fprintf(f, ", shape = doublecircle");
|
|
|
|
fprintf(f, "];\n");
|
|
|
|
}
|
|
|
|
fprintf(f, "\n");
|
2008-07-27 17:28:50 +02:00
|
|
|
for (unsigned int i = 1; i <= m_nbStates; i++)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
for (int l = 0; l < MAX_TRANSITION_LETTERS; l++)
|
|
|
|
{
|
|
|
|
if (m_transitions[i][l])
|
|
|
|
{
|
|
|
|
fprintf(f, "\t%d -> %d [label = \"", i, m_transitions[i][l]);
|
|
|
|
regexp_print_letter(f, l);
|
|
|
|
fprintf(f, "\"];\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(f, "fontsize=20;\n");
|
|
|
|
fprintf(f, "}\n");
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
pid_t pid = fork ();
|
|
|
|
if (pid > 0)
|
|
|
|
{
|
|
|
|
wait(NULL);
|
|
|
|
}
|
|
|
|
else if (pid == 0)
|
|
|
|
{
|
|
|
|
execlp("dotty", "dotty", iFileName.c_str(), NULL);
|
|
|
|
printf("exec dotty failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ************************************************** *
|
|
|
|
Definition of the AutomatonHelper class
|
|
|
|
* ************************************************** */
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
AutomatonHelper::AutomatonHelper(State * iInitState)
|
2008-01-08 14:52:32 +01:00
|
|
|
: m_initState(iInitState)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AutomatonHelper::~AutomatonHelper()
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *>::const_iterator it;
|
2008-01-08 14:52:32 +01:00
|
|
|
for (it = m_states.begin(); it != m_states.end(); it++)
|
|
|
|
{
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
void AutomatonHelper::addState(State * s)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
m_states.push_front(s);
|
2008-07-27 15:32:47 +02:00
|
|
|
DMSG(printf("** state %s added to automaton\n", idToString(s->getId()).c_str()));
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
State * AutomatonHelper::getState(const set<uint64_t> &iId) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *>::const_iterator it;
|
2008-01-08 14:52:32 +01:00
|
|
|
for (it = m_states.begin(); it != m_states.end(); it++)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
State * s = *it;
|
|
|
|
if (s->getId() == iId)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
//DMSG(printf("** get state %s ok\n", idToString(s->getId()).c_str()));
|
2008-01-08 14:52:32 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************** *
|
|
|
|
* ************************************************** *
|
|
|
|
* ************************************************** */
|
|
|
|
|
2008-07-13 09:55:47 +02:00
|
|
|
AutomatonHelper *AutomatonHelper::ps2nfa(uint64_t init_state_id, int *ptl, uint64_t *PS)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-13 09:55:47 +02:00
|
|
|
uint64_t maxpos = PS[0];
|
2008-07-27 15:32:47 +02:00
|
|
|
State * current_state;
|
|
|
|
bool used_letter[MAX_TRANSITION_LETTERS];
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* 1: init_state = root->PP */
|
2008-07-27 15:32:47 +02:00
|
|
|
State * temp_state = new State(init_state_id);
|
2008-01-08 14:52:32 +01:00
|
|
|
AutomatonHelper *nfa = new AutomatonHelper(temp_state);
|
|
|
|
nfa->addState(temp_state);
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *> L;
|
2008-01-08 14:52:32 +01:00
|
|
|
L.push_front(temp_state);
|
|
|
|
/* 2: while \exist state \in state_list */
|
|
|
|
while (! L.empty())
|
|
|
|
{
|
|
|
|
current_state = L.front();
|
|
|
|
L.pop_front();
|
2008-07-27 15:32:47 +02:00
|
|
|
DMSG(printf("** current state = %s\n", idToString(current_state->getId()).c_str()));
|
2008-01-08 14:52:32 +01:00
|
|
|
memset(used_letter, 0, sizeof(used_letter));
|
|
|
|
/* 3: \foreach l in \sigma | l \neq # */
|
2008-07-13 09:55:47 +02:00
|
|
|
for (uint32_t p = 1; p < maxpos; p++)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
int current_letter = ptl[p];
|
2008-07-27 15:32:47 +02:00
|
|
|
if (used_letter[current_letter] == false)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
/* 4: int set = \cup { PS(pos) | pos \in state \wedge pos == l } */
|
2008-07-13 09:55:47 +02:00
|
|
|
uint64_t ens = 0;
|
|
|
|
for (uint32_t pos = 1; pos <= maxpos; pos++)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
if (ptl[pos] == current_letter &&
|
2008-07-27 15:32:47 +02:00
|
|
|
(unsigned int)*(current_state->getId().begin()) & (1 << (pos - 1)))
|
2008-01-08 14:52:32 +01:00
|
|
|
ens |= PS[pos];
|
|
|
|
}
|
|
|
|
/* 5: transition from current_state to temp_state */
|
|
|
|
if (ens)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
set<uint64_t> temp_id;
|
|
|
|
temp_id.insert(ens);
|
2008-01-08 14:52:32 +01:00
|
|
|
temp_state = nfa->getState(temp_id);
|
|
|
|
if (temp_state == NULL)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
temp_state = new State(temp_id);
|
2008-01-08 14:52:32 +01:00
|
|
|
nfa->addState(temp_state);
|
|
|
|
L.push_front(temp_state);
|
|
|
|
}
|
2008-07-27 15:32:47 +02:00
|
|
|
current_state->m_next[current_letter] = temp_state;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
2008-07-27 15:32:47 +02:00
|
|
|
used_letter[current_letter] = true;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *>::const_iterator it;
|
2008-01-08 14:52:32 +01:00
|
|
|
for (it = nfa->m_states.begin(); it != nfa->m_states.end(); it++)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
State * s = *it;
|
|
|
|
if (*(s->getId().begin()) & (1 << (maxpos - 1)))
|
|
|
|
s->m_accept = true;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nfa;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************** *
|
|
|
|
* ************************************************** *
|
|
|
|
* ************************************************** */
|
|
|
|
|
2008-07-13 09:55:47 +02:00
|
|
|
set<uint64_t> AutomatonHelper::getSuccessor(const set<uint64_t> &S,
|
|
|
|
int letter,
|
2008-07-27 17:28:50 +02:00
|
|
|
const searchRegExpLists &iList) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-13 09:55:47 +02:00
|
|
|
set<uint64_t> R, r;
|
|
|
|
set<uint64_t>::const_iterator it;
|
2008-01-08 14:52:32 +01:00
|
|
|
for (it = S.begin(); it != S.end(); it++) /* \forall y \in S */
|
|
|
|
{
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
set<uint64_t> t;
|
|
|
|
t.insert(*it);
|
|
|
|
State *y = getState(t);
|
|
|
|
assert(y != NULL);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2008-07-13 09:55:47 +02:00
|
|
|
set<uint64_t> Ry; /* Ry = \empty */
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
State *z;
|
|
|
|
if ((z = y->m_next[letter]) != NULL) /* \delta (y,z) = l */
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
r = getSuccessor(z->getId(), RE_EPSILON, iList);
|
2008-01-08 14:52:32 +01:00
|
|
|
Ry.insert(r.begin(), r.end());
|
2008-07-27 15:32:47 +02:00
|
|
|
Ry.insert(z->getId().begin(), z->getId().end()); /* Ry = Ry \cup succ(z) */
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* \epsilon transition from start node */
|
2008-07-27 15:32:47 +02:00
|
|
|
if ((z = y->m_next[RE_EPSILON]) != NULL) /* \delta (y,z) = \epsilon */
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
r = getSuccessor(z->getId(), letter, iList);
|
2008-01-08 14:52:32 +01:00
|
|
|
Ry.insert(r.begin(), r.end()); /* Ry = Ry \cup succ(z) */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (letter < RE_FINAL_TOK)
|
|
|
|
{
|
2008-07-27 17:28:50 +02:00
|
|
|
for (unsigned int i = 0; i < iList.symbl.size(); i++)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-27 17:28:50 +02:00
|
|
|
if (iList.letters[i][letter] && (z = y->m_next[(int)iList.symbl[i]]) != NULL)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-27 17:28:50 +02:00
|
|
|
DMSG(printf("*** letter "));
|
|
|
|
DMSG(regexp_print_letter(stdout, letter));
|
|
|
|
DMSG(printf("is in "));
|
|
|
|
DMSG(regexp_print_letter(stdout, i));
|
|
|
|
|
|
|
|
r = getSuccessor(z->getId(), RE_EPSILON, iList);
|
|
|
|
Ry.insert(r.begin(), r.end());
|
|
|
|
Ry.insert(z->getId().begin(), z->getId().end());
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-27 17:28:50 +02:00
|
|
|
R.insert(Ry.begin(), Ry.end()); /* R = R \cup Ry */
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
void AutomatonHelper::setAccept(State * s) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
DMSG(printf("=== setting accept for node (%s) :", idToString(s->getId()).c_str()));
|
|
|
|
list<State *>::const_iterator it;
|
2008-01-08 14:52:32 +01:00
|
|
|
for (it = m_states.begin(); it != m_states.end(); it++)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
State * ns = *it;
|
|
|
|
uint64_t idx = *(ns->getId().begin());
|
|
|
|
DMSG(printf("%s ", idToString(ns->getId()).c_str()));
|
|
|
|
if (ns->m_accept && (std::find(s->getId().begin(), s->getId().end(), idx) != s->getId().end()))
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
DMSG(printf("(ok) "));
|
2008-07-27 15:32:47 +02:00
|
|
|
s->m_accept = true;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
DMSG(printf("\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AutomatonHelper *AutomatonHelper::nfa2dfa(const AutomatonHelper &iNfa,
|
2008-07-27 17:28:50 +02:00
|
|
|
const searchRegExpLists &iList)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
State * current_state;
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *> L;
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
// Clone the list
|
2008-07-27 15:32:47 +02:00
|
|
|
State * temp_state = new State(iNfa.m_initState->getId());
|
2008-01-08 14:52:32 +01:00
|
|
|
AutomatonHelper *dfa = new AutomatonHelper(temp_state);
|
|
|
|
dfa->addState(temp_state);
|
|
|
|
L.push_front(temp_state);
|
|
|
|
while (! L.empty())
|
|
|
|
{
|
|
|
|
current_state = L.front();
|
|
|
|
L.pop_front();
|
2008-07-27 15:32:47 +02:00
|
|
|
DMSG(printf("** current state = %s\n", idToString(current_state->getId()).c_str()));
|
2008-01-08 14:52:32 +01:00
|
|
|
for (int letter = 1; letter < DIC_LETTERS; letter++)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
// DMSG(printf("*** start successor of %s\n", idToString(current_state->getId()).c_str()));
|
2008-01-08 14:52:32 +01:00
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
set<uint64_t> temp_id = iNfa.getSuccessor(current_state->getId(), letter, iList);
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
if (! temp_id.empty())
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
DMSG(printf("*** successor of %s for ", idToString(current_state->getId()).c_str()));
|
2008-01-08 14:52:32 +01:00
|
|
|
DMSG(regexp_print_letter(stdout, letter));
|
2008-07-27 15:32:47 +02:00
|
|
|
DMSG(printf(" = %s\n", idToString(temp_id).c_str()));
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
temp_state = dfa->getState(temp_id);
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
// DMSG(printf("*** automaton get state -%s- ok\n", idToString(temp_id).c_str()));
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
if (temp_state == NULL)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
temp_state = new State(temp_id);
|
2008-01-08 14:52:32 +01:00
|
|
|
dfa->addState(temp_state);
|
|
|
|
L.push_front(temp_state);
|
|
|
|
}
|
2008-07-27 15:32:47 +02:00
|
|
|
current_state->m_next[letter] = temp_state;
|
2008-01-08 14:52:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *>::const_iterator it;
|
2008-01-08 14:52:32 +01:00
|
|
|
for (it = dfa->m_states.begin(); it != dfa->m_states.end(); it++)
|
|
|
|
{
|
|
|
|
iNfa.setAccept(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dfa;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ************************************************** *
|
|
|
|
* ************************************************** *
|
|
|
|
* ************************************************** */
|
|
|
|
|
2008-07-27 15:32:47 +02:00
|
|
|
static string idToString(const set<uint64_t> &iId)
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
set<uint64_t>::const_iterator it;
|
|
|
|
for (it = iId.begin(); it != iId.end(); it++)
|
|
|
|
{
|
|
|
|
char tmp[50];
|
|
|
|
sprintf(tmp, "%llu ", *it);
|
|
|
|
s += tmp;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void AutomatonHelper::printNodes(FILE* f) const
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *>::const_iterator it;
|
2008-01-08 14:52:32 +01:00
|
|
|
for (it = m_states.begin(); it != m_states.end(); it++)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
State * s = *it;
|
|
|
|
string sid = idToString(s->getId());
|
2008-01-08 14:52:32 +01:00
|
|
|
fprintf(f, "\t\"%s\" [label = \"%s\"", sid.c_str(), sid.c_str());
|
|
|
|
if (s == m_initState)
|
|
|
|
{
|
|
|
|
fprintf(f, ", style = filled, color=lightgrey");
|
|
|
|
}
|
2008-07-27 15:32:47 +02:00
|
|
|
if (s->m_accept)
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
fprintf(f, ", shape = doublecircle");
|
|
|
|
}
|
|
|
|
fprintf(f, "];\n");
|
|
|
|
}
|
|
|
|
fprintf(f, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AutomatonHelper::printEdges(FILE* f) const
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
list<State *>::const_iterator it;
|
2008-01-08 14:52:32 +01:00
|
|
|
for (it = m_states.begin(); it != m_states.end(); it++)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
State * s = *it;
|
2008-01-08 14:52:32 +01:00
|
|
|
for (int letter = 0; letter < 255; letter++)
|
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
if (s->m_next[letter])
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
2008-07-27 15:32:47 +02:00
|
|
|
fprintf(f, "\t\"%s\" -> ", idToString(s->getId()).c_str());
|
|
|
|
fprintf(f, "\"%s\" [label = \"", idToString(s->m_next[letter]->getId()).c_str());
|
2008-01-08 14:52:32 +01:00
|
|
|
regexp_print_letter(f, letter);
|
|
|
|
fprintf(f, "\"];\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_AUTOMATON
|
|
|
|
void AutomatonHelper::dump(const string &iFileName) const
|
|
|
|
{
|
|
|
|
FILE *f = fopen(iFileName.c_str(), "w");
|
|
|
|
fprintf(f, "digraph automaton {\n");
|
|
|
|
printNodes(f);
|
|
|
|
printEdges(f);
|
|
|
|
fprintf(f, "fontsize=20;\n");
|
|
|
|
fprintf(f, "}\n");
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid > 0)
|
|
|
|
{
|
|
|
|
wait(NULL);
|
|
|
|
}
|
|
|
|
else if (pid == 0)
|
|
|
|
{
|
|
|
|
execlp("dotty", "dotty", iFileName.c_str(), NULL);
|
|
|
|
printf("exec dotty failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|