예제 #1
0
파일: main.c 프로젝트: abachman/anagrammit
void MainLoop(short lexicon[][WORD_LEN], short input[], short temp_results[], 
              short results[][SINGLE_RESULT], short next_temp_result, 
              lexiptr temp_lex, char orig_words[][MAX_STR])
{   while (temp_lex.head != NULL && temp_lex.tail != NULL) 
    {   temp_results[next_temp_result] = PopQ(&temp_lex);
        inputRemove(input, lexicon[temp_results[next_temp_result]]);    
            
        if (isempty(input)) 
        {   COUNT_RESULTS++;
#ifdef DEBUG            
            puts("Found One!");
            printf("%i\t",COUNT_RESULTS);
            printf("%i => ", next_result);
#endif      
            if (next_result % 10000 == 0)
                printf("%i\n",next_result);
            if (next_result < MAX_RESULTS) 
            {   for (c=0;c<=next_temp_result;c++) 
                {   results[next_result][c] = temp_results[c]; 
#ifdef DEBUG                
                    printf("%s (%i) ", orig_words[temp_results[c]], temp_results[c]);
#endif             
                }
            }
#ifdef DEBUG
            printf("  length = %i\n",next_temp_result);
#endif
            inputAdd(input, lexicon[temp_results[next_temp_result]]);   
            temp_results[next_temp_result] = -1;
            next_result += 1;
        }
        else 
        {   /* CREATE NEW LEXICON */
            lexiptr new_lex;
            new_lex.tail = NULL; // in case nothing is added
            itemptr tmp_item = temp_lex.head;
            while (tmp_item != NULL) 
            {   if (wordCheck(input, lexicon[tmp_item->word]))
                    PushQ(&new_lex, tmp_item->word);
                tmp_item = (itemptr)tmp_item->next;
            }
            
            if (new_lex.tail == NULL)
            {   // empty lexicon, stay at this level and go to the next word.
                inputAdd(input, lexicon[temp_results[next_temp_result]]);
                temp_results[next_temp_result] = -1;
            }   
            else
            {   // full lexicon, full input, drill down.
#ifdef DEBUG                
                printf("Call again!\n");
#endif
                MainLoop(lexicon, input, temp_results, results,  
                        next_temp_result+1, new_lex, orig_words);
                inputAdd(input, lexicon[temp_results[next_temp_result]]);
                temp_results[next_temp_result] = -1;
            }
        }
    }
}
예제 #2
0
파일: regex2dfa.c 프로젝트: cran/rcqp
void
FormState(int Q)
{

  int I, S, S1, X; 
  int qX, Q1, Q2;
  int A, B;
  State SP;
  Exp E, E1;

  IBuf = NULL;
  IMax = 0;
  STab = NULL;
  Ss = 0; 
  AddState(1, &Q);

  for (S = 0; S < Ss; S++) 
    {
      SP = &STab[S];
      for (Xs = 0, S1 = 0; S1 < SP->States; S1++) 
        PushQ(SP->SList[S1]);
      for (SP->Empty = 0, Is = 0, X = 0; X < Xs; X++)  {
        qX = XStack[X];
      EVALUATE:
        E = EquTab[qX].Value;
        switch (E->Tag)  {
        case SymX:
          AddBuf(E->Body.Leaf, MakeExp(-1, OneX));
          break;
        case OneX:
          SP->Empty = 1;
          break;
        case ZeroX:
          break;
        case OptX:
          Q1 = E->Body.Arg[0];
          MakeExp(qX, OrX, MakeExp(-1, OneX), E->Body.Arg[0]);
          goto EVALUATE;
        case PlusX:
          Q1 = E->Body.Arg[0];
          MakeExp(qX, AndX, Q1, MakeExp(-1, StarX, Q1));
          goto EVALUATE;
        case StarX:
          Q1 = E->Body.Arg[0];
          MakeExp(qX, OrX, MakeExp(-1, OneX), MakeExp(-1, PlusX, Q1));
          goto EVALUATE;
        case OrX:
          Q1 = E->Body.Arg[0];
          Q2 = E->Body.Arg[1];
          PushQ(Q1);
          PushQ(Q2);
          break;
        case AndX:
          Q1 = E->Body.Arg[0], Q2 = E->Body.Arg[1];
          E1 = EquTab[Q1].Value;
          switch (E1->Tag) {
          case SymX:
            AddBuf(E1->Body.Leaf, Q2);
            break;
          case OneX:
            EquTab[qX].Value = EquTab[Q2].Value;
            goto EVALUATE;
          case ZeroX:
            MakeExp(qX, ZeroX);
            break;
          case OptX:
            A = E1->Body.Arg[0];
            MakeExp(qX, OrX, Q2, MakeExp(-1, AndX, A, Q2));
            goto EVALUATE;
          case PlusX:
            A = E1->Body.Arg[0];
            MakeExp(qX, AndX, A, MakeExp(-1, OrX, Q2, qX));
            goto EVALUATE;
          case StarX:
            A = E1->Body.Arg[0];
            MakeExp(qX, OrX, Q2, MakeExp(-1, AndX, A, qX));
            goto EVALUATE;
          case OrX:
            A = E1->Body.Arg[0], B = E1->Body.Arg[1];
            MakeExp(qX, OrX,
                    MakeExp(-1, AndX, A, Q2), MakeExp(-1, AndX, B, Q2));
            goto EVALUATE;
          case AndX:
            A = E1->Body.Arg[0], B = E1->Body.Arg[1];
            MakeExp(qX, AndX, A, MakeExp(-1, AndX, B, Q2));
            goto EVALUATE;
          }
        }
      }
      while (Xs > 0) 
        PopQ();
      SP->Shifts = Is;
      SP->ShList = cl_malloc(sizeof *SP->ShList * Is);
      for (I = 0; I < Is; I++)  {
        int rhs_state = -1;
        SP->ShList[I].LHS = IBuf[I].LHS;
        rhs_state = AddState(IBuf[I].Size, IBuf[I].RHS);
        SP = &STab[S];        /* AddState() might have reallocated state table -> update pointer */
        SP->ShList[I].RHS = rhs_state;
      }
    }
  free(IBuf);
  IBuf = 0;
  Is = IMax = 0;
}
예제 #3
0
파일: main.c 프로젝트: abachman/anagrammit
int main() {
    int counter = 0, orig_words_len = 0, i;
    char orig_words[LEXICON_LEN][MAX_STR];
    // Initialize orig_words
    for (counter=0;counter<LEXICON_LEN;counter++)
        orig_words[counter][0] = '\0';
    puts(" GET WORDS ");
    getWordList(orig_words);
    puts(" GOT 'EM ");
    for (counter=0;counter<LEXICON_LEN;counter++)
    {   if (orig_words[counter][0] == '\0')
        {   orig_words_len = counter;
            break;
        }
    }
    // Initialize original lexicon
    short lexicon[orig_words_len][WORD_LEN];
    for (i=0;i<orig_words_len;i++) 
    {   letterCount(orig_words[i], lexicon[i]);
        lexicon[i][END] = i;
    }
    
    // Initialize input
    short inpt[WORD_LEN];
    const char *inpt_str = "wellpunchmeinthefac";
    letterCount(inpt_str, inpt);
    
    // Create Original Lexicon Queue...
    /* Add to orig_lex the index of the 
    words that could possibly be spelled with the
    letters in the input.  This list will be 
    passed to the finding function */
    lexiptr orig_lex;
    for (i=0;i<orig_words_len;i++)
        if (wordCheck(inpt, lexicon[i])) 
            PushQ(&orig_lex, i);
    
    // Initialize results
    short temp_results[SINGLE_RESULT];
    for (i=0;i<SINGLE_RESULT;i++) 
        temp_results[i] = -1; 
    short results[MAX_RESULTS][SINGLE_RESULT];
    for (counter=0;counter<MAX_RESULTS;counter++)
        for (i=0;i<SINGLE_RESULT;i++)
            results[counter][i] = -1;
    
    /* Current Variables:
       char orig_words[][30], short lexicon[][27], short inpt[], 
       short temp_results[], short results[][15], next_result  */
    
    puts(" GO! ");
    printf("Start  = %d\n", time(NULL)); 
    MainLoop(lexicon, inpt, temp_results, results, 0, orig_lex, orig_words);
    printf("Finish = %d, %i results found.\n", time(NULL), COUNT_RESULTS);
    
    for (counter=0;counter<50;counter++)
    {   if (results[counter][0] == -1) continue;
        for (i=0;i<SINGLE_RESULT;i++)
            if (results[counter][i] != -1)
                printf("%s ", orig_words[results[counter][i]]);
        puts("");
    }
    return 1;
}
예제 #4
0
파일: dfa.c 프로젝트: kalngyk/learn
void FormStates(Exp E) {
   int S, I, Diff; Exp A, B; int AX, BX;
   STab = 0, Ss = 0; AddState(MakeExp(ZeroX)); AddState(E);
   TList = 0, TMax = 0;
   for (S = 0; S < Ss; S++) {
      PushQ(STab[S]);
      while (Xs > 0) {
         E = XStack[Xs - 1];
         if (E->Normal) { PopQ(); continue; }
         switch (E->Tag) {
            case ZeroX: case OneX:
               E->Terms = 0, E->Sum = 0, E->Normal = 1; PopQ();
            break;
            case SymX:
               E->Terms = 1, E->Sum = Allocate(sizeof *E->Sum);
               E->Sum[0].X = E->Body.Leaf, E->Sum[0].Q = MakeExp(OneX);
               E->Normal = 1; PopQ();
            break;
            case OptX:
               A = E->Body.Arg[0];
               if (!A->Normal) { PushQ(A); continue; }
               E->Terms = A->Terms, E->Sum = A->Sum;
               E->Normal = 1; PopQ();
            break;
            case StarX: case PlusX:
               A = E->Body.Arg[0];
               if (!A->Normal) { PushQ(A); continue; }
               B = E->Unit? E: MakeExp(StarX, A);
           Catenate:
               E->Terms = A->Terms;
               E->Sum = Allocate(E->Terms * sizeof *E->Sum);
               for (I = 0; I < E->Terms; I++)
                  E->Sum[I].X = A->Sum[I].X,
                  E->Sum[I].Q = CatExp(A->Sum[I].Q, B);
               E->Normal = 1; PopQ();
            break;
            case CatX: case OrX: case AndX: case DiffX: case ProdX:
               A = E->Body.Arg[0];
               if (!A->Normal) { PushQ(A); continue; }
               B = E->Body.Arg[1];
               if (E->Tag == CatX && !A->Unit) goto Catenate;
               if (!B->Normal) { PushQ(B); continue; }
               for (AX = BX = Ts = 0; AX < A->Terms || BX < B->Terms; ) {
                  Exp dA, dB; Symbol xA, xB;
                  Diff = 0;
                  if (AX >= A->Terms) Diff = 1;
                  else dA = A->Sum[AX].Q, xA = A->Sum[AX].X;
                  if (BX >= B->Terms) Diff = -1;
                  else dB = B->Sum[BX].Q, xB = B->Sum[BX].X;
                  if (Diff == 0) Diff = strcmp(xA->Name, xB->Name);
                  if (Diff <= 0) AX++;
                  if (Diff >= 0) BX++;
                  switch (E->Tag) {
                     case AndX:
                        if (Diff == 0) AddTerm(xA, BinExp(AndX, dA, dB));
                     break;
                     case CatX:
                        if (Diff <= 0) dA = CatExp(dA, B);
                     goto Join;
                     case ProdX:
                        if (Diff <= 0) dA = BinExp(ProdX, dA, B);
                        if (Diff >= 0) dB = BinExp(ProdX, dB, A);
                     case OrX: Join:
                        if (Diff < 0) AddTerm(xA, dA);
                        else if (Diff > 0) AddTerm(xB, dB);
                        else AddTerm(xA, BinExp(OrX, dA, dB));
                     break;
                     case DiffX:
                        if (Diff == 0) dA = MakeExp(DiffX, dA, dB);
                        if (Diff <= 0) AddTerm(xA, dA);
                     break;
                     default: break;
                  }
               }
               E->Terms = Ts;
               E->Sum = Allocate(E->Terms * sizeof *E->Sum);
               for (I = 0; I < Ts; I++) E->Sum[I] = TList[I];
               E->Normal = 1; PopQ();
            break;
         }
      }
      E = STab[S];
      for (I = 0; I < E->Terms; I++) AddState(E->Sum[I].Q);
   }
   free(XStack), XMax = 0; free(TList), TMax = 0;
}