コード例 #1
0
ファイル: gamma.c プロジェクト: dengxuyue/postgresql
int rules_add_rule(RULES *rules, int num, int *rule) {
    int i ,
        w ;
    SYMB a ,
         t ;
    SYMB *rule_start , *r ;
    NODE u ;
    NODE **Trie ;
    KW *keyw ,
       *k_s ;
    KW ***o_l ;

    if ( !rules ) return 1;       /* error rules obj not initialized */
    if ( !rules -> r_p ) return 2;  /* RULE_PARAM not allocated */
    if ( rules -> ready ) return 3; /* rules have already be readied */
    if ( rules -> rule_number >= MAXRULES ) {
        RET_ERR( "rules_add_rule: Too many rules are being added.",
                 rules -> err_p, 4);
    }

    /* get local copies of stuff saved in RULES */
    o_l = rules -> r_p -> output_link ;
    k_s = rules -> r_p -> key_space ;
    
    Trie = rules -> Trie ;
    r = rules -> r ;

    keyw = k_s + rules -> rule_number ;
    MEM_ERR(keyw, rules -> err_p, 5);

    u = EPSILON ;
    rule_start = r ; /* save rule start for inclusion in the record */
    if ( rule_start > rules -> rule_end ) {
        RET_ERR( "rules_add_rule: Too many rules for allocated memory.",
                 rules -> err_p, 5);
    }

    for (i=0; ; i++, r++ ) {
        if (i >= num) {
            RET_ERR( "rules_add_rule: invalid rule structure.",
                     rules -> err_p, 6);
        }

        *r = rule[i] ;
        /* -- a fail at the beginning of a field indicates end of record
           unless it's at the beginning of the record, in which case
           it's the end of file -- */
        if ( *r == FAIL ) {
            if ( i == 0 ) return 0;
            break;
        }

        /* -- check the input -- */
        if ( !is_input_symbol( *r ) ) {
            RET_ERR2( "rules_add_rule: Bad Input Token %d at rule %d",
                      *r,
                      rules -> rule_number ,
                      rules -> err_p,
                      7 ) ;
        }

        /* -- build the trie structure -- */
        if ( Trie[ u ][ *r ] == FAIL ) {
            if ( ++rules -> last_node >= MAXNODES ) {
                RET_ERR( "rules_add_rule: Too many nodes in gamma function",
                        rules -> err_p,
                        8 ) ;
            }
            Trie[ u ][ *r ] = rules -> last_node ;
            PAGC_CALLOC_STRUC(Trie[rules -> last_node],NODE,MAXINSYM,rules -> err_p,9) ;
            for ( a = 0 ;
                  a < MAXINSYM ;
                  a++ ) {
                Trie[ rules -> last_node ][ a ] = FAIL ;
            }
            if ( !initialize_link( rules -> err_p ,
                                   o_l ,
                                   rules -> last_node ) ) {
                return 10;
            }
        }
        u = Trie[ u ][ *r ] ;
    } /* end of for loop */

    keyw -> Input = rule_start ;
    if ( ( keyw -> Length = i ) == 0 ) {
        RET_ERR1( "rules_add_rule: Error 0 length rule #%d",
                  rules -> rule_number,
                  rules -> err_p,
                  11 ) ;
    }

    /* -- read the output tokens into the rule_space -- */
    r++ ; /* -- move to beginning of the output tokens -- */
    rule_start = r ; /* -- remember the beginning -- */
    while ( TRUE ) {
        i++;
        if ( i >= num ) {
            RET_ERR( "rules_add_rule: invalid rule structure.",
                     rules -> err_p, 6);
        }
        *r = rule[i] ;
        if ( *r == FAIL ) break;
        if ( !is_output_symbol( *r ) ) {
            RET_ERR2( "rules_add_rule: Rule File: Non-Token %d in Rule #%d\n",
                      *r ,
                      rules -> rule_number,
                      rules -> err_p,
                      7 ) ;
        }
        r++ ;
    }
    keyw -> Output = rule_start ;

    /* -- classify the output -- */
    i++ ;
    t = rule[i] ;
    i++ ;
    w = rule[i] ; 

    classify_link( rules -> r_p ,
                   o_l ,
                   keyw ,
                   u ,
                   w ,
                   t ) ;

    rules -> rule_number++ ;
    rules -> r = ++r ; ;
    return 0;
}
コード例 #2
0
ファイル: gamma.c プロジェクト: dengxuyue/postgresql
/*--------------------------------------------------------------------------- 
gamma.c (create_rules)
called by standard.l (init_stand_process)
calls util.c (open_aux_file)
calls gamma.c (initialize_link, is_input_symbol, is_output_symbol,
classify_link,precompute_gamma_function) 
----------------------------------------------------------------------------*/
RULE_PARAM *create_rules( const char *rule_name ,
                          PAGC_GLOBAL *glo_p ) {
   /* -- returns size of Gamma Function Matrix -- */
   SYMB a , 
        t ;
   NODE u ;
   int i , 
       w ;
   int is_eof = FALSE ;
   int rule_number = 0 ;
   int last_node = EPSILON ;
   FILE *rule_file ;
   SYMB *rule_start ,
        *rule_end ,
        *r ;
   KW *keyw , *k_s ;
   KW ***o_l ; 
   NODE **Trie ; 
   SYMB *r_s ;
   RULE_PARAM *r_p ; 
   ERR_PARAM *err_p ;

   err_p = glo_p -> process_errors ;

   PAGC_ALLOC_STRUC(r_p,RULE_PARAM,err_p,NULL) ; 

   /* -- initialize the statistics record -- */
   r_p -> collect_statistics = FALSE ;
   r_p -> total_best_keys = 0 ;
   r_p -> total_key_hits = 0 ;


   /* -- open the rule file, if possible -- */
   if ( ( rule_file = open_aux_file( glo_p ,
                                     rule_name ) ) == NULL ) {
      return NULL ;
   }
   /* -- rule file has the format of i i ... i -1 o o ... o -1 t f -- */


   /* -- storage for input and output records -- */
   PAGC_CALLOC_STRUC(r_s,SYMB,RULESPACESIZE,err_p,NULL); 

   /* -- storage for temporary trie for rules -- */
   PAGC_CALLOC_STRUC(Trie,NODE *,MAXNODES,err_p,NULL); 

   /* -- initialize the first( EPSILON ) node of the trie -- */
   PAGC_CALLOC_STRUC(Trie[EPSILON],NODE,MAXINSYM,err_p,NULL); 

   for ( a = 0 ; 
         a < MAXINSYM ; 
         a++ ) {
      Trie[ EPSILON ][ a ] = FAIL ;
   }

   /* -- storage for global output_link -- */
   PAGC_CALLOC_STRUC(o_l,KW **,MAXNODES,err_p,NULL); 
   PAGC_CALLOC_STRUC(k_s,KW,MAXRULES,err_p,NULL); 

   rule_end = r_s + RULESPACESIZE ; 
   if ( !initialize_link( err_p ,
                          o_l , 
                          EPSILON ) ) {
      return NULL ;
   }
   for ( r = r_s ; 
         !feof( rule_file ) ; 
         r++, rule_number++ ) {
      if ( rule_number >= MAXRULES ) {
         CLIENT_ERR( err_p ) ;
         RET_ERR( "create_rules: Too many rules in file",
                  err_p,
                  NULL) ; 
      }
      keyw = k_s + rule_number ; 
      MEM_ERR(keyw,err_p,NULL);
      /* -- get input record -- */

      u = EPSILON ;
      rule_start = r ; /* -- save rule start for inclusion in record -- */
      if ( rule_start > rule_end ) {
         RET_ERR( "create_rules: Too many rules for allocated memory",
                  err_p,
                  NULL ) ; 
      }
      for ( i = 0 ; 
            ; 
            i++, r++  ) {

         /* -- read the first integer -- */
         fscanf( rule_file, 
                 "%d", 
                 r ) ;
         /* -- a fail at the beginning of a field indicates end of record
            unless it's at the beginning of the record, in which case
            it's the end of file -- */
         if ( *r == FAIL ) {
            if ( i == 0 ) {
               is_eof = TRUE ;
            }
            break ;
         }
         /* -- check the input -- */
         if ( !is_input_symbol( *r ) ) {
            CLIENT_ERR( err_p ) ;
            RET_ERR2( "create_rules: Rule file: Bad Input Token %d at rule %d", 
                      *r, 
                      rule_number , 
                      err_p, 
                      NULL ) ;
         }

         /* -- build the trie structure -- */
         if ( Trie[ u ][ *r ] == FAIL ) {
            if ( ++last_node >= MAXNODES ) { 
               RET_ERR( "create_rules: Too many nodes in gamma function",
                        err_p,
                        NULL ) ; 
            }
            Trie[ u ][ *r ] = last_node ;
            PAGC_CALLOC_STRUC(Trie[last_node],NODE,MAXINSYM,err_p,NULL) ;
            for ( a = 0 ; 
                  a < MAXINSYM ; 
                  a++ ) {
               Trie[ last_node ][ a ] = FAIL ;
            }        
            if ( !initialize_link( err_p ,
                                   o_l , 
                                   last_node ) ) {
               return NULL ;
            }
         }
         u = Trie[ u ][ *r ] ;
      }
      if ( is_eof )
         break ;
      keyw -> Input = rule_start ;
      if ( ( keyw -> Length = i ) == 0 ) {
         CLIENT_ERR( err_p ) ;
         RET_ERR1( "create_rules: Error Rule File: 0 length rule #%d",
                   rule_number,
                   err_p,
                   NULL ) ;
      }

      /* -- read the output tokens into the rule_space -- */
      r++ ; /* -- move to beginning of the output tokens -- */
      rule_start = r ; /* -- remember the beginning -- */
      while ( TRUE ) {
         fscanf( rule_file, 
                 "%d", 
                 r ) ;
         if ( *r == FAIL )
            break ;
         if ( !is_output_symbol( *r ) ) {
            RET_ERR2( "create_rules: Rule File: Non-Token %d in Rule #%d\n", 
                      *r , 
                      rule_number,
                      err_p,
                      NULL ) ;
         }
         r++ ;
      }
      keyw -> Output = rule_start ;

      /* -- classify the output -- */
      fscanf( rule_file , 
              "%d" , 
              &t ) ;
      fscanf( rule_file , 
              "%d" , 
              &w ) ;

      classify_link( r_p ,
                     o_l , 
                     keyw , 
                     u , 
                     w , 
                     t ) ;
   } /* -- end of file read -- */


   r_p -> rule_space = r_s ;
   r_p -> key_space = k_s ;
   r_p -> output_link = o_l ;
   r_p -> rules_read = rule_number ;

   fclose( rule_file ) ;


   if ( ++last_node >= MAXNODES ) { 
      RET_ERR( "create_rules: Too many nodes in gamma function" ,
               err_p,
               NULL) ; 
   }
   /* -- change the EPSILON node transitions in preparation for Gamma -- */
   for ( a = 0 ; 
         a < MAXINSYM ; 
         a++ ) {
      if ( Trie[ EPSILON ][ a ] == FAIL ) {
         Trie[ EPSILON ][ a ] = EPSILON ;
      }
   }

   /* -- create the global Gamma function matrix -- */
   if ( ( r_p -> gamma_matrix = precompute_gamma_function( err_p, 
                                                           Trie , 
                                                           o_l , 
                                                           last_node ) ) == NULL ) {
      return NULL ;
   }

   /* -- no longer need the Trie -- */
   PAGC_DESTROY_2D_ARRAY(Trie,NODE,last_node) ; 


   r_p -> num_nodes = last_node ; 

   if ( glo_p -> log_init ) {
      CLIENT_ERR( err_p ) ;
      LOG_MESS2( "create_rules: Rules installed with %d nodes and %d rules",
                 last_node ,
                 rule_number ,
                 err_p ) ;
   }

   return r_p ; 
}
コード例 #3
0
ファイル: tape.cpp プロジェクト: donellie/turingmachine
// =========
bool Tape::is_valid_symbol (const symbol_t& symbol_i) const
{
  return (is_input_symbol (symbol_i) || is_internal_symbol (symbol_i) || is_empty_symbol (symbol_i));
}