2004-04-08 11:43:06 +02:00
|
|
|
/* Eliot */
|
2005-05-06 01:45:04 +02:00
|
|
|
/* Copyright (C) 1999 Antoine Fraboulet */
|
2004-04-08 11:43:06 +02:00
|
|
|
/* */
|
2005-05-06 01:45:04 +02:00
|
|
|
/* This file is part of Eliot. */
|
|
|
|
/* */
|
|
|
|
/* Eliot is free software; you can redistribute it and/or modify */
|
2004-04-08 11:43:06 +02:00
|
|
|
/* 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. */
|
|
|
|
/* */
|
2005-05-06 01:45:04 +02:00
|
|
|
/* Elit is distributed in the hope that it will be useful, */
|
2004-04-08 11:43:06 +02:00
|
|
|
/* 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 */
|
2005-10-23 16:53:42 +02:00
|
|
|
/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
2005-05-06 01:45:04 +02:00
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
/**
|
2005-11-05 18:56:22 +01:00
|
|
|
* \file dic_search.c
|
2005-04-19 18:26:50 +02:00
|
|
|
* \brief Dictionary lookup functions
|
|
|
|
* \author Antoine Fraboulet
|
|
|
|
* \date 2002
|
2004-04-08 11:43:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "dic_internals.h"
|
|
|
|
#include "dic.h"
|
2005-04-09 21:16:09 +02:00
|
|
|
#include "regexp.h"
|
2005-04-27 19:35:03 +02:00
|
|
|
#include "dic_search.h"
|
2005-04-19 18:26:50 +02:00
|
|
|
#include "libdic_a-er.h" /* generated by bison */
|
2005-04-27 19:35:03 +02:00
|
|
|
#include "scanner.h" /* generated by flex */
|
|
|
|
#include "automaton.h"
|
2005-04-19 18:26:50 +02:00
|
|
|
|
2005-05-06 01:45:04 +02:00
|
|
|
/*
|
|
|
|
* shut down the compiler
|
|
|
|
*/
|
|
|
|
static int yy_init_globals (yyscan_t yyscanner )
|
|
|
|
{
|
|
|
|
yy_init_globals(yyscanner);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-04-08 11:43:06 +02:00
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
/**
|
|
|
|
* Dic_seel_edgeptr
|
|
|
|
* walk the dictionary until the end of the word
|
|
|
|
* @param dic : dictionnary
|
|
|
|
* @param s : current pointer to letters
|
|
|
|
* @param eptr : current edge in the dawg
|
|
|
|
*/
|
2004-04-08 11:43:06 +02:00
|
|
|
static Dawg_edge*
|
2005-02-05 12:14:56 +01:00
|
|
|
Dic_seek_edgeptr(const Dictionary dic, const char* s, Dawg_edge *eptr)
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
if (*s)
|
|
|
|
{
|
|
|
|
Dawg_edge *p = dic->dawg + eptr->ptr;
|
|
|
|
do {
|
2005-04-19 18:26:50 +02:00
|
|
|
if (p->chr == (unsigned)(*s & DIC_CHAR_MASK))
|
2005-02-05 12:14:56 +01:00
|
|
|
return Dic_seek_edgeptr (dic,s + 1, p);
|
2004-04-08 11:43:06 +02:00
|
|
|
} while (!(*p++).last);
|
2005-02-05 12:14:56 +01:00
|
|
|
return dic->dawg;
|
2004-04-08 11:43:06 +02:00
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
else
|
|
|
|
return eptr;
|
2004-04-08 11:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
/**
|
|
|
|
* Dic_search_word : direct application of Dic_seek_edgeptr
|
|
|
|
* @param dic : dictionary
|
|
|
|
* @param word : word to lookup
|
|
|
|
* @result 0 not a valid word, 1 ok
|
|
|
|
*/
|
2004-04-08 11:43:06 +02:00
|
|
|
|
|
|
|
int
|
2005-02-05 12:14:56 +01:00
|
|
|
Dic_search_word(const Dictionary dic, const char* word)
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
Dawg_edge *e;
|
|
|
|
e = Dic_seek_edgeptr(dic,word,dic->dawg + dic->root);
|
|
|
|
return e->term;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
/**
|
|
|
|
* global variables for Dic_search_word_by_len :
|
2004-04-08 11:43:06 +02:00
|
|
|
*
|
|
|
|
* a pointer to the structure is passed as a parameter
|
|
|
|
* so that all the search_* variables appear to the functions
|
2005-02-05 12:14:56 +01:00
|
|
|
* as global but the code remains re-entrant.
|
2004-04-08 11:43:06 +02:00
|
|
|
* Should be better to change the algorithm ...
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct params_7plus1_t {
|
|
|
|
Dictionary search_dic;
|
|
|
|
int search_len;
|
|
|
|
int search_wordlistlen;
|
|
|
|
int search_wordlistlenmax;
|
|
|
|
char search_wordtst[DIC_WORD_MAX];
|
2005-04-09 21:16:09 +02:00
|
|
|
char search_letters[DIC_LETTERS];
|
2004-04-08 11:43:06 +02:00
|
|
|
char (*search_wordlist)[RES_7PL1_MAX][DIC_WORD_MAX];
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
Dic_search_word_by_len(struct params_7plus1_t *params, int i, Dawg_edge *edgeptr)
|
|
|
|
{
|
|
|
|
/* depth first search in the dictionary */
|
|
|
|
do {
|
2005-02-05 12:14:56 +01:00
|
|
|
/* we use a static array and not a real list so we have to stop if
|
2004-04-08 11:43:06 +02:00
|
|
|
* the array is full */
|
|
|
|
if (params->search_wordlistlen >= params->search_wordlistlenmax)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* the test is false only when reach the end-node */
|
2005-02-05 12:14:56 +01:00
|
|
|
if (edgeptr->chr)
|
|
|
|
{
|
2004-04-08 11:43:06 +02:00
|
|
|
|
|
|
|
/* is the letter available in search_letters */
|
|
|
|
if (params->search_letters[edgeptr->chr])
|
2005-02-05 12:14:56 +01:00
|
|
|
{
|
2004-04-08 11:43:06 +02:00
|
|
|
params->search_wordtst[i] = edgeptr->chr + 'A' - 1;
|
|
|
|
params->search_letters[edgeptr->chr] --;
|
|
|
|
if (i == params->search_len)
|
|
|
|
{
|
|
|
|
if ((edgeptr->term)
|
|
|
|
/* && (params->search_wordlistlen < params->search_wordlistlenmax) */)
|
|
|
|
strcpy((*params->search_wordlist)[params->search_wordlistlen++],params->search_wordtst);
|
|
|
|
}
|
|
|
|
else /* if (params->search_wordlistlen < params->search_wordlistlenmax) */
|
|
|
|
{
|
|
|
|
Dic_search_word_by_len(params,i + 1, params->search_dic->dawg + edgeptr->ptr);
|
|
|
|
}
|
|
|
|
params->search_letters[edgeptr->chr] ++;
|
|
|
|
params->search_wordtst[i] = '\0';
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
/* the letter is of course available if we have a joker available */
|
|
|
|
if (params->search_letters[0])
|
|
|
|
{
|
|
|
|
params->search_wordtst[i] = edgeptr->chr + 'a' - 1;
|
|
|
|
params->search_letters[0] --;
|
|
|
|
if (i == params->search_len)
|
|
|
|
{
|
|
|
|
if ((edgeptr->term)
|
|
|
|
/* && (params->search_wordlistlen < params->search_wordlistlenmax) */)
|
|
|
|
strcpy((*(params->search_wordlist))[params->search_wordlistlen++],params->search_wordtst);
|
|
|
|
}
|
|
|
|
else /* if (params->search_wordlistlen < params->search_wordlistlenmax) */
|
|
|
|
{
|
|
|
|
Dic_search_word_by_len(params,i + 1,params->search_dic->dawg + edgeptr->ptr);
|
|
|
|
}
|
|
|
|
params->search_letters[0] ++;
|
|
|
|
params->search_wordtst[i] = '\0';
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
}
|
2004-04-08 11:43:06 +02:00
|
|
|
} while (! (*edgeptr++).last);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
Dic_search_7pl1(const Dictionary dic, const char* rack,
|
2005-04-09 21:16:09 +02:00
|
|
|
char buff[DIC_LETTERS][RES_7PL1_MAX][DIC_WORD_MAX],
|
2005-02-05 12:14:56 +01:00
|
|
|
int joker)
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
int i,j,wordlen;
|
2005-02-05 12:14:56 +01:00
|
|
|
const char* r = rack;
|
2004-04-08 11:43:06 +02:00
|
|
|
struct params_7plus1_t params;
|
|
|
|
Dawg_edge *root_edge;
|
|
|
|
|
2005-04-09 21:16:09 +02:00
|
|
|
for(i=0; i < DIC_LETTERS; i++)
|
2004-04-08 11:43:06 +02:00
|
|
|
for(j=0; j < RES_7PL1_MAX; j++)
|
|
|
|
buff[i][j][0] = '\0';
|
|
|
|
|
2005-04-09 21:16:09 +02:00
|
|
|
for(i=0; i<DIC_LETTERS; i++)
|
2005-02-05 12:14:56 +01:00
|
|
|
params.search_letters[i] = 0;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
|
|
|
if (dic == NULL || rack == NULL)
|
|
|
|
return;
|
|
|
|
|
2005-02-05 12:14:56 +01:00
|
|
|
/*
|
2004-04-08 11:43:06 +02:00
|
|
|
* the letters are verified and changed to the dic internal
|
2005-04-19 18:26:50 +02:00
|
|
|
* representation (*r & DIC_CHAR_MASK)
|
2004-04-08 11:43:06 +02:00
|
|
|
*/
|
|
|
|
for(wordlen=0; wordlen < DIC_WORD_MAX && *r; r++)
|
|
|
|
{
|
|
|
|
if (isalpha(*r))
|
|
|
|
{
|
2005-04-19 18:26:50 +02:00
|
|
|
params.search_letters[(int)*r & DIC_CHAR_MASK]++;
|
2004-04-08 11:43:06 +02:00
|
|
|
wordlen++;
|
|
|
|
}
|
|
|
|
else if (*r == '?')
|
|
|
|
{
|
|
|
|
if (joker)
|
|
|
|
{
|
|
|
|
params.search_letters[0]++;
|
|
|
|
wordlen++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(buff[0][0],"** joker **",DIC_WORD_MAX);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wordlen < 1)
|
|
|
|
return;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
|
|
|
root_edge = dic->dawg + (dic->dawg[dic->root].ptr);
|
2004-04-08 11:43:06 +02:00
|
|
|
|
|
|
|
params.search_dic = dic;
|
|
|
|
params.search_wordlistlenmax = RES_7PL1_MAX;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
/* search for all the words that can be done with the letters */
|
|
|
|
params.search_len = wordlen - 1;
|
|
|
|
params.search_wordtst[wordlen]='\0';
|
|
|
|
params.search_wordlist = & buff[0];
|
2005-02-05 12:14:56 +01:00
|
|
|
params.search_wordlistlen = 0;
|
2004-04-08 11:43:06 +02:00
|
|
|
Dic_search_word_by_len(¶ms,0,root_edge);
|
|
|
|
|
|
|
|
/* search for all the words that can be done with the letters +1 */
|
|
|
|
params.search_len = wordlen;
|
|
|
|
params.search_wordtst[wordlen + 1]='\0';
|
|
|
|
for(i='a'; i <= 'z'; i++)
|
|
|
|
{
|
2005-04-19 18:26:50 +02:00
|
|
|
params.search_letters[i & DIC_CHAR_MASK]++;
|
2004-04-08 11:43:06 +02:00
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
params.search_wordlist = & buff[i & DIC_CHAR_MASK];
|
2005-02-05 12:14:56 +01:00
|
|
|
params.search_wordlistlen = 0;
|
2004-04-08 11:43:06 +02:00
|
|
|
Dic_search_word_by_len(¶ms,0,root_edge);
|
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
params.search_letters[i & DIC_CHAR_MASK]--;
|
2004-04-08 11:43:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/****************************************/
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
Dic_search_Racc(const Dictionary dic, const char* word,
|
|
|
|
char wordlist[RES_RACC_MAX][DIC_WORD_MAX])
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
/* search_racc will try to add a letter in front and at the end of a word */
|
|
|
|
|
|
|
|
int i,wordlistlen;
|
|
|
|
Dawg_edge *edge;
|
|
|
|
char wordtst[DIC_WORD_MAX];
|
|
|
|
|
|
|
|
for(i=0; i < RES_RACC_MAX; i++)
|
|
|
|
wordlist[i][0] = 0;
|
|
|
|
|
|
|
|
if (dic == NULL || wordlist == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* let's try for the front */
|
|
|
|
wordlistlen = 0;
|
|
|
|
strcpy(wordtst+1,word);
|
|
|
|
for(i='a'; i <= 'z'; i++)
|
|
|
|
{
|
|
|
|
wordtst[0] = i;
|
|
|
|
if (Dic_search_word(dic,wordtst) && wordlistlen < RES_RACC_MAX)
|
|
|
|
strcpy(wordlist[wordlistlen++],wordtst);
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
/* add a letter at the end */
|
|
|
|
for(i=0; word[i]; i++)
|
|
|
|
wordtst[i] = word[i];
|
|
|
|
|
|
|
|
wordtst[i ] = '\0';
|
|
|
|
wordtst[i+1] = '\0';
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
edge = Dic_seek_edgeptr(dic,word,dic->dawg + dic->root);
|
|
|
|
|
|
|
|
/* points to what the next letter can be */
|
|
|
|
edge = dic->dawg + edge->ptr;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
if (edge != dic->dawg)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
if (edge->term && wordlistlen < RES_RACC_MAX)
|
|
|
|
{
|
|
|
|
wordtst[i] = edge->chr + 'a' - 1;
|
|
|
|
strcpy(wordlist[wordlistlen++],wordtst);
|
|
|
|
}
|
|
|
|
} while (!(*edge++).last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/****************************************/
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
Dic_search_Benj(const Dictionary dic, const char* word,
|
|
|
|
char wordlist[RES_BENJ_MAX][DIC_WORD_MAX])
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
int i,wordlistlen;
|
|
|
|
char wordtst[DIC_WORD_MAX];
|
|
|
|
Dawg_edge *edge0,*edge1,*edge2,*edgetst;
|
|
|
|
|
|
|
|
for(i=0; i < RES_BENJ_MAX; i++)
|
|
|
|
wordlist[i][0] = 0;
|
|
|
|
|
|
|
|
if (dic == NULL || word == NULL)
|
|
|
|
return;
|
2005-02-05 12:14:56 +01:00
|
|
|
|
2004-04-08 11:43:06 +02:00
|
|
|
wordlistlen = 0;
|
|
|
|
|
|
|
|
strcpy(wordtst+3,word);
|
|
|
|
edge0 = dic->dawg + (dic->dawg[dic->root].ptr);
|
|
|
|
do {
|
|
|
|
wordtst[0] = edge0->chr + 'a' - 1;
|
|
|
|
edge1 = dic->dawg + edge0->ptr;
|
|
|
|
do {
|
|
|
|
wordtst[1] = edge1->chr + 'a' - 1;
|
|
|
|
edge2 = dic->dawg + edge1->ptr;
|
|
|
|
do {
|
|
|
|
wordtst[2] = edge2->chr + 'a' - 1;
|
|
|
|
edgetst = Dic_seek_edgeptr(dic,word,edge2);
|
|
|
|
if (edgetst->term && wordlistlen < RES_BENJ_MAX)
|
|
|
|
strcpy(wordlist[wordlistlen++],wordtst);
|
|
|
|
} while (!(*edge2++).last);
|
|
|
|
} while (!(*edge1++).last);
|
|
|
|
} while (!(*edge0++).last);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/****************************************/
|
|
|
|
|
|
|
|
struct params_cross_t {
|
|
|
|
Dictionary dic;
|
|
|
|
int wordlen;
|
|
|
|
int wordlistlen;
|
|
|
|
int wordlistlenmax;
|
|
|
|
char mask[DIC_WORD_MAX];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-11-04 21:00:05 +01:00
|
|
|
Dic_search_cross_rec(struct params_cross_t *params,
|
|
|
|
char wordlist[RES_CROS_MAX][DIC_WORD_MAX],
|
2005-04-19 18:26:50 +02:00
|
|
|
Dawg_edge *edgeptr)
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
Dawg_edge *current = params->dic->dawg + edgeptr->ptr;
|
|
|
|
|
|
|
|
if (params->mask[params->wordlen] == '\0' && edgeptr->term)
|
|
|
|
{
|
|
|
|
if (params->wordlistlen < params->wordlistlenmax)
|
|
|
|
strcpy(wordlist[params->wordlistlen++],params->mask);
|
|
|
|
}
|
|
|
|
else if (params->mask[params->wordlen] == '.')
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
params->mask[params->wordlen] = current->chr + 'a' - 1;
|
|
|
|
params->wordlen ++;
|
|
|
|
Dic_search_cross_rec(params,wordlist,current);
|
|
|
|
params->wordlen --;
|
|
|
|
params->mask[params->wordlen] = '.';
|
|
|
|
}
|
|
|
|
while (!(*current++).last);
|
|
|
|
}
|
2005-02-05 12:14:56 +01:00
|
|
|
else
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2005-04-19 18:26:50 +02:00
|
|
|
if (current->chr == (unsigned int)(params->mask[params->wordlen] & DIC_CHAR_MASK))
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
params->wordlen ++;
|
|
|
|
Dic_search_cross_rec(params,wordlist,current);
|
|
|
|
params->wordlen --;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!(*current++).last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-02-05 12:14:56 +01:00
|
|
|
Dic_search_Cros(const Dictionary dic, const char* mask,
|
|
|
|
char wordlist[RES_CROS_MAX][DIC_WORD_MAX])
|
2004-04-08 11:43:06 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct params_cross_t params;
|
|
|
|
|
|
|
|
for(i=0; i < RES_CROS_MAX; i++)
|
|
|
|
wordlist[i][0] = 0;
|
|
|
|
|
|
|
|
if (dic == NULL || mask == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(i=0; i < DIC_WORD_MAX && mask[i]; i++)
|
|
|
|
{
|
|
|
|
if (isalpha(mask[i]))
|
2005-04-19 18:26:50 +02:00
|
|
|
params.mask[i] = (mask[i] & DIC_CHAR_MASK) + 'A' - 1;
|
2004-04-08 11:43:06 +02:00
|
|
|
else
|
|
|
|
params.mask[i] = '.';
|
|
|
|
}
|
|
|
|
params.mask[i] = '\0';
|
|
|
|
|
|
|
|
params.dic = dic;
|
|
|
|
params.wordlen = 0;
|
|
|
|
params.wordlistlen = 0;
|
|
|
|
params.wordlistlenmax = RES_CROS_MAX;
|
|
|
|
Dic_search_cross_rec(¶ms, wordlist, dic->dawg + dic->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/****************************************/
|
|
|
|
|
2004-06-19 20:43:35 +02:00
|
|
|
struct params_regexp_t {
|
|
|
|
Dictionary dic;
|
2005-11-05 18:56:22 +01:00
|
|
|
int minlength;
|
|
|
|
int maxlength;
|
2005-04-19 18:26:50 +02:00
|
|
|
automaton automaton;
|
2005-11-04 21:00:05 +01:00
|
|
|
struct search_RegE_list_t *charlist;
|
2005-04-19 18:26:50 +02:00
|
|
|
char word[DIC_WORD_MAX];
|
|
|
|
int wordlen;
|
|
|
|
int wordlistlen;
|
|
|
|
int wordlistlenmax;
|
2004-06-19 20:43:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2005-11-04 21:00:05 +01:00
|
|
|
Dic_search_regexp_rec(struct params_regexp_t *params,
|
2005-04-19 18:26:50 +02:00
|
|
|
int state,
|
2005-11-04 21:00:05 +01:00
|
|
|
Dawg_edge *edgeptr,
|
2005-04-19 18:26:50 +02:00
|
|
|
char wordlist[RES_REGE_MAX][DIC_WORD_MAX])
|
2004-06-19 20:43:35 +02:00
|
|
|
{
|
2005-04-19 18:26:50 +02:00
|
|
|
int next_state;
|
|
|
|
Dawg_edge *current;
|
|
|
|
/* if we have a valid word we store it */
|
2005-05-06 01:45:04 +02:00
|
|
|
if (automaton_get_accept(params->automaton,state) && edgeptr->term)
|
2005-04-09 21:16:09 +02:00
|
|
|
{
|
2005-11-05 18:56:22 +01:00
|
|
|
int l = strlen(params->word);
|
|
|
|
if (params->wordlistlen < params->wordlistlenmax &&
|
|
|
|
params->minlength <= l &&
|
|
|
|
params->maxlength >= l)
|
2005-04-09 21:16:09 +02:00
|
|
|
{
|
2005-04-19 18:26:50 +02:00
|
|
|
strcpy(wordlist[params->wordlistlen++],params->word);
|
2005-04-09 21:16:09 +02:00
|
|
|
}
|
2005-11-04 21:00:05 +01:00
|
|
|
}
|
2005-04-19 18:26:50 +02:00
|
|
|
/* we now drive the search by exploring the dictionary */
|
|
|
|
current = params->dic->dawg + edgeptr->ptr;
|
|
|
|
do {
|
|
|
|
/* the current letter is current->chr */
|
2005-05-06 01:45:04 +02:00
|
|
|
next_state = automaton_get_next_state(params->automaton,state,current->chr);
|
2005-04-19 18:26:50 +02:00
|
|
|
/* 1 : the letter appears in the automaton as is */
|
2005-05-06 01:45:04 +02:00
|
|
|
if (next_state)
|
2005-04-19 18:26:50 +02:00
|
|
|
{
|
|
|
|
params->word[params->wordlen] = current->chr + 'a' - 1;
|
|
|
|
params->wordlen ++;
|
|
|
|
Dic_search_regexp_rec(params,next_state,current,wordlist);
|
|
|
|
params->wordlen --;
|
|
|
|
params->word[params->wordlen] = '\0';
|
|
|
|
}
|
|
|
|
} while (!(*current++).last);
|
|
|
|
}
|
2005-04-09 21:16:09 +02:00
|
|
|
|
2005-05-06 01:45:04 +02:00
|
|
|
|
2005-11-04 21:00:05 +01:00
|
|
|
/**
|
2005-04-27 19:35:03 +02:00
|
|
|
* function prototype for parser generated by bison
|
|
|
|
*/
|
2005-11-04 21:00:05 +01:00
|
|
|
int regexpparse(yyscan_t scanner, NODE** root,
|
2005-04-27 19:35:03 +02:00
|
|
|
struct search_RegE_list_t *list,
|
|
|
|
struct regexp_error_report_t *err);
|
2004-06-19 20:43:35 +02:00
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
void
|
|
|
|
Dic_search_RegE(const Dictionary dic, const char* re,
|
|
|
|
char wordlist[RES_REGE_MAX][DIC_WORD_MAX],
|
|
|
|
struct search_RegE_list_t *list)
|
|
|
|
{
|
|
|
|
int i,p,n,value;
|
2005-11-04 21:00:05 +01:00
|
|
|
int ptl[REGEXP_MAX+1];
|
|
|
|
int PS [REGEXP_MAX+1];
|
2005-04-19 18:26:50 +02:00
|
|
|
NODE* root;
|
|
|
|
yyscan_t scanner;
|
|
|
|
YY_BUFFER_STATE buf;
|
|
|
|
automaton a;
|
|
|
|
char stringbuf[250];
|
|
|
|
struct params_regexp_t params;
|
2005-04-27 19:35:03 +02:00
|
|
|
struct regexp_error_report_t report;
|
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
/* init */
|
2004-06-19 20:43:35 +02:00
|
|
|
for(i=0; i < RES_REGE_MAX; i++)
|
|
|
|
wordlist[i][0] = 0;
|
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
if (dic == NULL || re == NULL)
|
2004-06-19 20:43:35 +02:00
|
|
|
return;
|
|
|
|
|
2005-04-19 18:26:50 +02:00
|
|
|
/* (expr)# */
|
|
|
|
sprintf(stringbuf,"(%s)#",re);
|
|
|
|
for(i=0; i < REGEXP_MAX; i++)
|
|
|
|
{
|
|
|
|
PS[i] = 0;
|
|
|
|
ptl[i] = 0;
|
|
|
|
}
|
2005-04-09 21:16:09 +02:00
|
|
|
|
2005-04-27 19:35:03 +02:00
|
|
|
report.pos1 = 0;
|
|
|
|
report.pos2 = 0;
|
|
|
|
report.msg[0] = '\0';
|
|
|
|
|
|
|
|
/* parsing */
|
2005-04-26 18:31:45 +02:00
|
|
|
regexplex_init( &scanner );
|
|
|
|
buf = regexp_scan_string( stringbuf, scanner );
|
2005-04-19 18:26:50 +02:00
|
|
|
root = NULL;
|
2005-04-27 19:35:03 +02:00
|
|
|
value = regexpparse( scanner , &root, list, &report);
|
2005-04-26 18:31:45 +02:00
|
|
|
regexp_delete_buffer(buf,scanner);
|
|
|
|
regexplex_destroy( scanner );
|
2005-04-19 18:26:50 +02:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
2005-05-06 01:45:04 +02:00
|
|
|
#ifdef DEBUG_FLEX_IS_BROKEN
|
2005-04-27 19:35:03 +02:00
|
|
|
fprintf(stderr,"parser error at pos %d - %d : %s\n",
|
|
|
|
report.pos1, report.pos2, report.msg);
|
2005-11-04 21:00:05 +01:00
|
|
|
#endif
|
2005-04-19 18:26:50 +02:00
|
|
|
regexp_delete_tree(root);
|
|
|
|
return ;
|
|
|
|
}
|
2005-04-09 21:16:09 +02:00
|
|
|
|
|
|
|
n = 1;
|
|
|
|
p = 1;
|
2005-04-19 18:26:50 +02:00
|
|
|
regexp_parcours(root, &p, &n, ptl);
|
2005-04-09 21:16:09 +02:00
|
|
|
PS [0] = p - 1;
|
|
|
|
ptl[0] = p - 1;
|
|
|
|
|
|
|
|
regexp_possuivante(root,PS);
|
2005-04-19 18:26:50 +02:00
|
|
|
|
2005-05-06 01:45:04 +02:00
|
|
|
if ((a = automaton_build(root->PP,ptl,PS,list)) != NULL)
|
2005-04-19 18:26:50 +02:00
|
|
|
{
|
2005-04-27 19:35:03 +02:00
|
|
|
params.dic = dic;
|
2005-11-05 18:56:22 +01:00
|
|
|
params.minlength = list->minlength;
|
|
|
|
params.maxlength = list->maxlength;
|
2005-04-27 19:35:03 +02:00
|
|
|
params.automaton = a;
|
|
|
|
params.charlist = list;
|
|
|
|
memset(params.word,'\0',sizeof(params.word));
|
|
|
|
params.wordlen = 0;
|
|
|
|
params.wordlistlen = 0;
|
|
|
|
params.wordlistlenmax = RES_REGE_MAX;
|
2005-05-06 01:45:04 +02:00
|
|
|
Dic_search_regexp_rec(¶ms, automaton_get_init(a), dic->dawg + dic->root, wordlist);
|
2005-11-04 21:00:05 +01:00
|
|
|
|
2005-04-27 19:35:03 +02:00
|
|
|
automaton_delete(a);
|
|
|
|
}
|
2005-05-06 01:45:04 +02:00
|
|
|
regexp_delete_tree(root);
|
2004-06-19 20:43:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************/
|
|
|
|
/****************************************/
|
|
|
|
|