2008-01-08 14:52:32 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Eliot
|
2012-10-07 16:39:33 +02:00
|
|
|
* Copyright (C) 2005-2012 Antoine Fraboulet & Olivier Teulière
|
2008-01-08 14:52:32 +01:00
|
|
|
* Authors: Antoine Fraboulet <antoine.fraboulet @@ free.fr>
|
2012-10-07 16:39:33 +02:00
|
|
|
* Olivier Teulière <ipkiss @@ gmail.com>
|
2008-01-08 14:52:32 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*****************************************************************************/
|
2005-05-06 01:45:04 +02:00
|
|
|
|
2009-11-29 17:01:31 +01:00
|
|
|
#ifndef DIC_AUTOMATON_H_
|
|
|
|
#define DIC_AUTOMATON_H_
|
2005-05-06 01:45:04 +02:00
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
#include "logging.h"
|
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
class AutomatonHelper;
|
2008-07-27 17:28:50 +02:00
|
|
|
struct searchRegExpLists;
|
2005-05-06 01:45:04 +02:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
class Automaton
|
|
|
|
{
|
2012-02-18 22:26:52 +01:00
|
|
|
DEFINE_LOGGER();
|
2008-01-08 14:52:32 +01:00
|
|
|
public:
|
|
|
|
/// Constructor
|
2005-05-06 01:45:04 +02:00
|
|
|
/**
|
2008-01-08 14:52:32 +01:00
|
|
|
* Build a static deterministic finite automaton from
|
2005-05-06 01:45:04 +02:00
|
|
|
* "init_state", "ptl" and "PS" given by the parser
|
2005-11-04 21:00:05 +01:00
|
|
|
*/
|
2008-07-27 17:28:50 +02:00
|
|
|
Automaton(uint64_t init_state, int *ptl, uint64_t *PS,
|
|
|
|
const searchRegExpLists &iList);
|
2005-05-06 01:45:04 +02:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
/// Destructor
|
|
|
|
~Automaton();
|
2005-05-06 01:45:04 +02:00
|
|
|
|
|
|
|
/**
|
2008-01-08 14:52:32 +01:00
|
|
|
* Get the number of states in the automaton.
|
2005-05-06 01:45:04 +02:00
|
|
|
* @returns number of states
|
|
|
|
*/
|
2008-01-08 14:52:32 +01:00
|
|
|
int getNbStates() const { return m_nbStates; }
|
2005-05-06 01:45:04 +02:00
|
|
|
|
|
|
|
/**
|
2008-01-08 14:52:32 +01:00
|
|
|
* Query the id of the init state.
|
2005-05-06 01:45:04 +02:00
|
|
|
* @returns init state id
|
|
|
|
*/
|
2008-01-08 14:52:32 +01:00
|
|
|
int getInitId() const { return m_init; }
|
2005-05-06 01:45:04 +02:00
|
|
|
|
|
|
|
/**
|
2008-01-08 14:52:32 +01:00
|
|
|
* Query the acceptor flag for the given state
|
|
|
|
* @return true/false
|
2005-05-06 01:45:04 +02:00
|
|
|
*/
|
2008-07-13 09:55:47 +02:00
|
|
|
bool accept(uint64_t state) const { return m_acceptors[state]; }
|
2005-05-06 01:45:04 +02:00
|
|
|
|
|
|
|
/**
|
2008-01-08 14:52:32 +01:00
|
|
|
* Return the next state when the transition is taken
|
2005-05-06 01:45:04 +02:00
|
|
|
* @returns next state id (1 <= id <= nstate, 0 = invalid id)
|
|
|
|
*/
|
2008-07-13 09:55:47 +02:00
|
|
|
uint64_t getNextState(uint64_t start, char l) const
|
2008-01-08 14:52:32 +01:00
|
|
|
{
|
|
|
|
return m_transitions[start][(int)l];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump the automaton into a file (for debugging purposes)
|
|
|
|
*/
|
|
|
|
void dump(const string &iFileName) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Number of states
|
2008-07-27 17:28:50 +02:00
|
|
|
unsigned int m_nbStates;
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/// ID of the init state
|
2008-07-27 17:28:50 +02:00
|
|
|
uint64_t m_init;
|
2008-01-08 14:52:32 +01:00
|
|
|
|
|
|
|
/// Array of booleans, one for each state
|
|
|
|
bool *m_acceptors;
|
|
|
|
|
|
|
|
/// Matrix of transitions
|
|
|
|
int **m_transitions;
|
2005-05-06 01:45:04 +02:00
|
|
|
|
2008-01-08 14:52:32 +01:00
|
|
|
void finalize(const AutomatonHelper &a);
|
|
|
|
};
|
2004-06-20 22:13:59 +02:00
|
|
|
|
2005-05-06 01:45:04 +02:00
|
|
|
#endif /* _DIC_AUTOMATON_H_ */
|