Пример #1
0
void test_hint( int *passed, int *failed )
{
    int res = 1;
    bitset *bs = bitset_create();
    if ( bs != NULL )
    {
        bs = bitset_set(bs,1);
        if ( bs != NULL )
            bs = bitset_set(bs,23);
        if ( bs != NULL )
        {
            hint *h = hint_create( bs, (vgnode*)0x123 );
            if ( h != NULL )
            {
                if ( hint_node(h) != (vgnode*)0x123 )
                {
                    fprintf(stderr,"hint: failed to set node\n");
                    res = 0;
                }
                bitset *bs2 = bitset_create();
                if ( bs2 != NULL )
                {
                    bs2 = bitset_set( bs2, 12 );
                    hint_or( h, bs2 );
                    if ( !hint_contains(h,bs) )
                    {
                        fprintf(stderr,"hint: failed to save bs\n");
                        res = 0;
                    }
                    if ( !hint_contains(h,bs2) )
                    {
                        fprintf(stderr,"hint: failed to save bs2\n");
                        res = 0;
                    }
                    hint *h2 = hint_create( bs2, NULL );
                    if ( h2 != NULL )
                    {
                        hint_append( h, h2 );
                        hint *head = hint_delist( h );
                        if ( head != h2 )
                        {
                            fprintf(stderr,"hint: delist failed\n");
                            res = 0;
                        }
                        hint_dispose( h2 );
                    }
                    bitset_dispose( bs2 );
                }
                hint_dispose( h );
            }
            bitset_dispose( bs );
        }
        else
            res = 0;
    }
    if ( res )
        (*passed)++;
    else
        (*failed)++;
}
Пример #2
0
void
print_results (void)
{
  state_number i;

  /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
     that conflicts with Posix.  */
  FILE *out = xfopen (spec_verbose_file, "w");

  reduce_output (out);
  grammar_rules_partial_print (out,
			       _("Rules never reduced"), rule_never_reduced_p);
  conflicts_output (out);

  print_grammar (out);

  /* If the whole state item sets, not only the kernels, are wanted,
     `closure' will be run, which needs memory allocation/deallocation.   */
  if (report_flag & report_itemsets)
    new_closure (nritems);
  /* Storage for print_reductions.  */
  shift_set =  bitset_create (ntokens, BITSET_FIXED);
  look_ahead_set = bitset_create (ntokens, BITSET_FIXED);
  for (i = 0; i < nstates; i++)
    print_state (out, states[i]);
  bitset_free (shift_set);
  bitset_free (look_ahead_set);
  if (report_flag & report_itemsets)
    free_closure ();

  xfclose (out);
}
Пример #3
0
void
conflicts_solve (void)
{
  state_number i;
  /* List of lookahead tokens on which we explicitly raise a syntax error.  */
  symbol **errors = xnmalloc (ntokens + 1, sizeof *errors);

  conflicts = xcalloc (nstates, sizeof *conflicts);
  shift_set = bitset_create (ntokens, BITSET_FIXED);
  lookahead_set = bitset_create (ntokens, BITSET_FIXED);
  obstack_init (&solved_conflicts_obstack);
  obstack_init (&solved_conflicts_xml_obstack);

  for (i = 0; i < nstates; i++)
    {
      set_conflicts (states[i], errors);

      /* For uniformity of the code, make sure all the states have a valid
	 `errs' member.  */
      if (!states[i]->errs)
	states[i]->errs = errs_new (0, 0);
    }

  free (errors);
}
Пример #4
0
static int make_test_data( plugin_log *log )
{
    c1 = c2 = c3 = c4 = cr = cl = cc = NULL;
    bs1 = bs2 = bs3 = bs4 = bs5 = bsl = bsr = bsm = NULL;
    p1 = p2 = p3 = p4 = pl = pr = pc = NULL;
    bs1 = bitset_create();
    bitset_set(bs1,3);
    bitset_set(bs1,27);
    bs2 = bitset_create();
    bitset_set(bs2,5);
    bitset_set(bs2,17);
    bs3 = bitset_create();
    bitset_set(bs3,7);
    bitset_set(bs3,14);
    bitset_set(bs3,19);
    bs4 = bitset_create();
    bitset_set(bs4,7);
    bitset_set(bs4,14);
    bitset_set(bs4,22);
    bs5 = bitset_create();
    bitset_set(bs5,7);
    bitset_set(bs5,14);
    bsl = bitset_create();
    bsr = bitset_create();
    bsl = bitset_set(bsl,3);
    if ( bsl != NULL )
        bsl = bitset_set(bsl,23);
    bsr = bitset_set(bsr,3);
    if ( bsr != NULL )
        bsr = bitset_set(bsr,21);
    bsm = bitset_create();
    bitset_set(bsm,23);
    bitset_set(bsm,7);
    if ( bs1 != NULL && bs2 != NULL && bs3 != NULL && bs4 != NULL 
        && bs5 != NULL && bsl != NULL && bsr != NULL && bsm != NULL )
    {
        p1 = pair_create_basic(bs1, data1, 6);
        p2 = pair_create_basic(bs2, data2, 5);
        p3 = pair_create_basic(bs3, data3, 4);
        p4 = pair_create_basic(bs4, data4, 6);
        pl = pair_create_basic(bsl,data4,6);
        pr = pair_create_basic(bsr,data5,5);
        pc = pair_create_basic(bsm,data6,7);
        if ( p1 != NULL && p2 != NULL && p3 != NULL && p4 != NULL 
            && pl != NULL && pr != NULL && pc != NULL )
        {
            c1 = card_create( p1, log );
            c2 = card_create( p2, log );
            c3 = card_create( p3, log );
            c4 = card_create( p4, log );
            cl = card_create( pl, log );
            cr = card_create( pr, log );
            cc = card_create( pc, log );
            if ( c1 != NULL && c2 != NULL && c3 != NULL && c4 != NULL 
                && cl != NULL && cr != NULL && cc != NULL )
                return 1;
        }
    }
    return 0;
}
Пример #5
0
/**
 * Add a new card
 * @param list the list of cards to add it to
 * @param c the card to add
 * @param text_off the offset in version for lp
 * @param version the version to follow
 */
void card_add_at( card **list, card *c, int text_off, int version )
{
    card *temp = *list;
    bitset *bs = bitset_create();
    if ( bs != NULL )
        bs = bitset_set( bs, version );
    if ( bs != NULL )
    {
        card *last = NULL;
        while ( temp != NULL )
        {
            pair *p = card_pair(temp);
            if ( bitset_next_set_bit(pair_versions(p),version)== version )
            {
                int off = card_text_off(temp);
                if ( off < text_off )
                    last = temp;
                else
                {
                    if ( last != NULL )
                        card_add_after( last, c );
                    else
                    {
                        c->right = *list;
                        *list = c;
                    }
                    break;
                }
            }
            temp = card_next( temp, bs, 0 );
        }
        bitset_dispose( bs );
    }
}
Пример #6
0
static void card_test_overhang( int *passed, int *failed, plugin_log *log )
{
    card_set_right(cl,cr);
    card_set_left(cr,cl);
    bitset *bs = card_node_overhang( cl );
    if ( bs != NULL )
    {
        bitset *bsc = bitset_create();
        if ( bsc != NULL )
        {
            bsc = bitset_or( bsc, bsl );
            bsc = bitset_set( bsc, 7 );
            bitset_and_not( bsc, pair_versions(pr) );
            if ( !bitset_equals(bsc,bs) )
            {
                fprintf(stderr,"card: overhang text failed\n");
                (*failed)++;
            }
            else
                (*passed)++;
        }
        else
            (*failed)++;
        if ( bsc != NULL )
            bitset_dispose( bsc );
    }
    else
    {
        fprintf(stderr,"card: failed to compute overhang\n");
        (*failed)++;
    }
    if ( bs != NULL )
        bitset_dispose( bs );
}
Пример #7
0
/**
 * \brief Initialize a group structure for channels or profiles
 *
 * \param[out] grp       Pointer to the group
 * \param[in]  items_cnt Number of items (can be re-sized in the future)
 * \return On success returns 0. Otherwise returns a non-zero value and the
 *   content of the structure is undefined.
 */
static int
group_init(struct pevents_group *grp, size_t items_cnt)
{
	if (items_cnt == 0) {
		return 1;
	}

	// Clear everything
	memset(grp, 0, sizeof(*grp));

	// Allocate an array of items
	grp->all_prealloc = items_cnt;
	grp->all_ptr = calloc(items_cnt, sizeof(*(grp->all_ptr)));
	if (!grp->all_ptr) {
		MSG_ERROR(msg_module, "Unable to allocate memory (%s:%d)",
			__FILE__, __LINE__);
		return 1;
	}

	// Prepare a bitset
	grp->bitset = bitset_create(items_cnt);
	if (!grp->bitset) {
		MSG_ERROR(msg_module, "Unable to allocate memory (%s:%d)",
			__FILE__, __LINE__);
		free(grp->all_ptr);
		return 1;
	}

	return 0;
}
Пример #8
0
/**
 * \post:
 *   - \c result = a new \c bitset of size \c ::nritems such that any bit \c i
 *     is set iff <tt>ritem[i]</tt> is a nonterminal after which all ritems in
 *     the same RHS are nullable nonterminals.  In other words, the follows of
 *     a goto on <tt>ritem[i]</tt> include the lookahead set of the item.
 */
static bitset
ielr_compute_ritem_sees_lookahead_set (void)
{
  bitset result = bitset_create (nritems, BITSET_FIXED);
  unsigned int i = nritems-1;
  while (i>0)
    {
      --i;
      while (!item_number_is_rule_number (ritem[i])
             && ISVAR (ritem[i])
             && nullable [item_number_as_symbol_number (ritem[i]) - ntokens])
        bitset_set (result, i--);
      if (!item_number_is_rule_number (ritem[i]) && ISVAR (ritem[i]))
        bitset_set (result, i--);
      while (!item_number_is_rule_number (ritem[i]) && i>0)
        --i;
    }
  if (trace_flag & trace_ielr)
    {
      fprintf (stderr, "ritem_sees_lookahead_set:\n");
      debug_bitset (result);
      fprintf (stderr, "\n");
    }
  return result;
}
Пример #9
0
int main(int argc, char *argv[]){
    HREinitBegin(argv[0]);
    HREaddOptions(options,"Tool for signature minimization\n\nOptions");
    char *files[2];
    lts_lib_setup();
    HREinitStart(&argc,&argv,1,2,files,"<input> [<output>]");
    if (task==Undefined){
        Abort("Please select the reduction to apply.");
    }
    lts_t lts=lts_create();
    Debug("reading %s",files[0]);
    lts_read(files[0],lts);
    Print(infoShort,"input has %u states and %u transitions",
                          lts->states,lts->transitions);
    switch(task){
        case Strong:{
            lowmem_strong_reduce(lts);
            break;
        }
        case Branching:{
            bitset_t divergence=NULL;
            if (divergence_sensitive){
                divergence=bitset_create(256,256);
                lts_find_divergent(lts,tau_step,NULL,divergence);
            }
            lowmem_branching_reduce(lts,divergence);
            break;
        }
        case Lumping:{
            lowmem_lumping_reduce(lts);
            break;
        }
        case Copy:{
            break;
        }
        case Silent:{
            silent_compression(lts);
            break;
        }
        case Determinize:{
            lts_mkdet(lts);
            break;
        }
        case Cycle:{
            lts_cycle_elim(lts);
            break;
        }
        default: Abort("missing case");
    }    
    Print(infoShort,"reduced LTS has %u states and %u transitions",
                          lts->states,lts->transitions);
    if (files[1]){
        Debug("writing %s",files[1]);
        lts_write(files[1],lts,NULL,segments);
        Debug("output written");
    } else {
        Debug("no output");
    }
    HREexit(LTSMIN_EXIT_SUCCESS);
}
Пример #10
0
static int
bitset_index_reserve(struct bitset_index *index, size_t size)
{
	if (size <= index->capacity)
		return 0;

	size_t capacity = (index->capacity > 0)
				? index->capacity
				: INDEX_DEFAULT_CAPACITY;

	while (capacity <= size) {
		capacity *= 2;
	}

	struct bitset **bitsets = index->realloc(index->bitsets,
					capacity * sizeof(*index->bitsets));
	if (bitsets == NULL)
		goto error_1;

	memset(bitsets + index->capacity, 0,
	       (capacity - index->capacity) * sizeof(*index->bitsets));

	/* Save bitset ** but do not update index->capacity */
	index->bitsets = bitsets;

	/* Resize rollback buffer */
	char *rollback_buf = (char *) index->realloc(index->rollback_buf,
						     capacity);
	if (rollback_buf == NULL)
		goto error_1;

	index->rollback_buf = rollback_buf;

	/* Initialize bitsets */
	for (size_t b = index->capacity; b < capacity; b++) {
		index->bitsets[b] = index->realloc(NULL,
					sizeof(*index->bitsets[b]));
		if (index->bitsets[b] == NULL)
			goto error_2;

		bitset_create(index->bitsets[b], index->realloc);
	}

	index->capacity = capacity;

	return 0;

error_2:
	for (size_t b = index->capacity; b < capacity; b++) {
		if (index->bitsets[b] == NULL)
			break;

		bitset_destroy(index->bitsets[b]);
		index->realloc(index->bitsets[b], 0);
		index->bitsets[b] = NULL;
	}
error_1:
	return -1;
}
Пример #11
0
void
reduce_grammar (void)
{
  bool reduced;

  /* Allocate the global sets used to compute the reduced grammar */

  N = bitset_create (nvars, BITSET_FIXED);
  P =  bitset_create (nrules, BITSET_FIXED);
  V = bitset_create (nsyms, BITSET_FIXED);
  V1 = bitset_create (nsyms, BITSET_FIXED);

  useless_nonterminals ();
  inaccessable_symbols ();

  reduced = (nuseless_nonterminals + nuseless_productions > 0);
  if (!reduced)
    return;

  reduce_print ();

  if (!bitset_test (N, accept->number - ntokens))
    complain (&startsymbol_location, fatal,
              _("start symbol %s does not derive any sentence"),
              startsymbol->tag);

  /* First reduce the nonterminals, as they renumber themselves in the
     whole grammar.  If you change the order, nonterms would be
     renumbered only in the reduced grammar.  */
  if (nuseless_nonterminals > 0)
    nonterminals_reduce ();
  if (nuseless_productions > 0)
    reduce_grammar_tables ();

  if (trace_flag & trace_grammar)
    {
      grammar_dump (stderr, "Reduced Grammar");

      fprintf (stderr, "reduced %s defines %d terminals, %d nonterminals"
               ", and %d productions.\n",
               grammar_file, ntokens, nvars, nrules);
    }
}
Пример #12
0
static void
no_reduce_bitset_init (state const *s, bitset *no_reduce_set)
{
  int n;
  *no_reduce_set = bitset_create (ntokens, BITSET_FIXED);
  bitset_zero (*no_reduce_set);
  FOR_EACH_SHIFT (s->transitions, n)
    bitset_set (*no_reduce_set, TRANSITION_SYMBOL (s->transitions, n));
  for (n = 0; n < s->errs->num; ++n)
    if (s->errs->symbols[n])
      bitset_set (*no_reduce_set, s->errs->symbols[n]->content->number);
}
Пример #13
0
int bitset_copy(bitset* copy_of, bitset** copy) {
    int creation_status = bitset_create(copy, copy_of->total_bits);

    if(creation_status != BITSET_SUCCESS) {
        return creation_status;
    }

    int num_buckets = (copy_of->total_bits / BUCKET_SIZE) + 1;

    memcpy((*copy)->bit_buffer, copy_of->bit_buffer, num_buckets * SHORT_INT_SIZE);

    return BITSET_SUCCESS;
}
Пример #14
0
/**
 * Compute the overhang for a card node
 * @param c the leading pair of a node
 * @return the bitset of the overhang or NULL (user to free)
 */
bitset *card_node_overhang( card *c )
{
    bitset *bs = bitset_create();
    bs = bitset_or( bs, pair_versions(c->p) );
    if ( pair_is_hint(c->right->p) )
    {
        pair *pp = c->right->p;
        bs = bitset_or( bs, pair_versions(pp) );
        if ( pair_is_hint(pp) )
            bitset_clear_bit(bs,0);
        c = c->right;
    }
    bitset_and_not( bs, pair_versions(c->right->p) );
    return bs;
}
Пример #15
0
void
print_xml (void)
{
  int level = 0;

  FILE *out = xfopen (spec_xml_file, "w");

  fputs ("<?xml version=\"1.0\"?>\n\n", out);
  xml_printf (out, level,
              "<bison-xml-report version=\"%s\" bug-report=\"%s\""
              " url=\"%s\">",
              xml_escape_n (0, VERSION),
              xml_escape_n (1, PACKAGE_BUGREPORT),
              xml_escape_n (2, PACKAGE_URL));

  fputc ('\n', out);
  xml_printf (out, level + 1, "<filename>%s</filename>",
              xml_escape (grammar_file));

  /* print grammar */
  print_grammar (out, level + 1);

  new_closure (nritems);
  no_reduce_set =  bitset_create (ntokens, BITSET_FIXED);

  /* print automaton */
  fputc ('\n', out);
  xml_puts (out, level + 1, "<automaton>");
  {
    state_number i;
    for (i = 0; i < nstates; i++)
      print_state (out, level + 2, states[i]);
  }
  xml_puts (out, level + 1, "</automaton>");

  bitset_free (no_reduce_set);
  free_closure ();

  xml_puts (out, 0, "</bison-xml-report>");

  {
    int i;
    for (i = 0; i < num_escape_bufs; ++i)
      free (escape_bufs[i].ptr);
  }

  xfclose (out);
}
Пример #16
0
static void
useless_nonterminals (void)
{
  bitset Np, Ns;
  rule_number r;

  /* N is set as built.  Np is set being built this iteration. P is
     set of all productions which have a RHS all in N.  */

  Np = bitset_create (nvars, BITSET_FIXED);


  /* The set being computed is a set of nonterminals which can derive
     the empty string or strings consisting of all terminals. At each
     iteration a nonterminal is added to the set if there is a
     production with that nonterminal as its LHS for which all the
     nonterminals in its RHS are already in the set.  Iterate until
     the set being computed remains unchanged.  Any nonterminals not
     in the set at that point are useless in that they will never be
     used in deriving a sentence of the language.

     This iteration doesn't use any special traversal over the
     productions.  A set is kept of all productions for which all the
     nonterminals in the RHS are in useful.  Only productions not in
     this set are scanned on each iteration.  At the end, this set is
     saved to be used when finding useful productions: only
     productions in this set will appear in the final grammar.  */

  while (1)
    {
      bitset_copy (Np, N);
      for (r = 0; r < nrules; r++)
        if (!bitset_test (P, r)
            && useful_production (r, N))
          {
            bitset_set (Np, rules[r].lhs->number - ntokens);
            bitset_set (P, r);
          }
      if (bitset_equal_p (N, Np))
        break;
      Ns = Np;
      Np = N;
      N = Ns;
    }
  bitset_free (N);
  N = Np;
}
Пример #17
0
int bitset_deserialize(bitset** bset, FILE* fp) {

    int bitset_size;
    fread(&bitset_size, 1, sizeof(unsigned int), fp);

    bitset* out;
    int creation_status = bitset_create(&out, bitset_size);
    if(creation_status != BITSET_SUCCESS) {
        return creation_status;
    } 

    int buffer_size = calculate_buffer_size(bitset_size);
    fread(out->bit_buffer, buffer_size, 1, fp);

    (*bset) = out;
    return BITSET_SUCCESS;
}
Пример #18
0
static
void test_cardinality()
{
	header();

	struct bitset bm;
	bitset_create(&bm, realloc);

	fail_unless(bitset_cardinality(&bm) == 0);

	size_t cnt = 0;
	fail_if(bitset_set(&bm, 10) < 0);
	cnt++;
	fail_if(bitset_set(&bm, 15) < 0);
	cnt++;
	fail_if(bitset_set(&bm, 20) < 0);
	cnt++;

	fail_unless(bitset_cardinality(&bm) == cnt);

	fail_if(bitset_set(&bm, 10) < 0);
	fail_unless(bitset_cardinality(&bm) == cnt);

	fail_if(bitset_clear(&bm, 20) < 0);
	cnt--;
	fail_unless(bitset_cardinality(&bm) == cnt);

	fail_if(bitset_clear(&bm, 20) < 0);
	fail_unless(bitset_cardinality(&bm) == cnt);

	fail_if(bitset_clear(&bm, 666) < 0);
	fail_unless(bitset_cardinality(&bm) == cnt);

	fail_if(bitset_clear(&bm, 10) < 0);
	cnt--;
	fail_unless(bitset_cardinality(&bm) == cnt);

	fail_if(bitset_clear(&bm, 15) < 0);
	cnt--;
	fail_unless(bitset_cardinality(&bm) == cnt);

	bitset_destroy(&bm);

	footer();
}
Пример #19
0
/**
 * Create an empty card
 * @param version the version of the empty card
 * @param log the log to record errors in
 * @return an empty card
 */
card *card_create_blank( int version, plugin_log *log )
{
    card *c = NULL;
    bitset *bs = bitset_create();
    if ( bs != NULL )
    {
        bitset_set( bs, version );
        pair *p = pair_create_basic( bs, ustring_empty, 0 );
        if ( p != NULL )
            c = card_create( p, log );
        else
            plugin_log_add(log,"card: failed to create pair");
        bitset_dispose( bs );
    }
    else
        plugin_log_add(log,"card: failed to create bitset");
    return c;
}
Пример #20
0
/**
 * Create a child from the new version
 * @param parent the parent we are a child of
 * @param version the version of the child
 * @param log record errors here
 * @return the newborn child
 */
card *card_make_child( card *parent, int version, plugin_log *log )
{
    card *cc = NULL;
    bitset *bs = bitset_create();
    if ( bs != NULL )
    {
        bs = bitset_set( bs, version );
        if ( bs != NULL )
        {
            pair *child = pair_create_child( bs );
            pair_add_child(card_pair(parent), child );
            bitset_dispose( bs );
            cc = card_create( child, log );
        }
        else
            plugin_log_add(log,"card: failed to create bitset\n");
    }
    return cc;
}
Пример #21
0
int huffman_tree_serialize(huffman_node* root, FILE* fp) {

    bitset* bset;
    int bitset_creation_status = bitset_create(&bset, 30);
    if(bitset_creation_status == BITSET_ALLOC_ERROR) {
        return HUFFMAN_ALLOC_ERROR;
    }

    int bits_stored = 0;
    int serialization_status = huffman_tree_serialize_recurse(root, bset, &bits_stored);
    if(serialization_status != HUFFMAN_SUCCESS) {
        bitset_destroy(&bset);
        return serialization_status;
    }

    bitset_serialize(bset, fp);
    bitset_destroy(&bset);
    return HUFFMAN_SUCCESS;
}
Пример #22
0
static bitset_t calc_int_matrix(lima_gp_ir_prog_t* prog)
{
	unsigned num_regs = prog->reg_alloc;
	bitset_t ret = bitset_create(num_regs*num_regs);
	lima_gp_ir_block_t* block;
	gp_ir_prog_for_each_block(prog, block)
	{
		lima_gp_ir_root_node_t* node;
		gp_ir_block_for_each_node(block, node)
		{
			if (node->node.op != lima_gp_ir_op_store_reg)
				continue;
			
			lima_gp_ir_store_reg_node_t* store_reg_node =
				gp_ir_node_to_store_reg(&node->node);
			
			unsigned reg_index = store_reg_node->reg->index;
			calc_interference(node->live_virt_after, ret, reg_index, num_regs);
		}
	}
Пример #23
0
/**
 * Find a point to insert a card belonging to the new version
 * @param l the left bound of the unaligned region (itself aligned)
 * @param r the right bound of the unaligned region (itself aligned)
 * @param v the new version
 * @return the card AFTER which to insert the blank or filler, or NULL
 */
card *card_get_insertion_point_nv( card *l, card *r, int v )
{
    card *ip = NULL;
    if ( card_node_indegree(l,v)==1 )
        ip = l;
    else if ( card_node_outdegree(r->left,v)==1 )
        ip = r->left;
    if ( ip == NULL )
    {
        bitset *bs = bitset_create();
        if ( bs != NULL )
        {
            bitset_set( bs, v );
            ip = card_get_insertion_point_internal( l, r, bs );
            bitset_dispose( bs );
        }
    }
    if ( ip == NULL )
    {
        // force insertion on left
        vgnode *vg = vgnode_create();
        if ( vg != NULL )
        {
            if ( vgnode_compute(vg,l,v) )
            {
                card *b = card_create_blank_bs(vgnode_incoming(vg),NULL);
                if ( b != NULL )
                {
                    card_add_after( l, b );
                    ip = b;
                }
            }
            vgnode_dispose( vg );
        }
        // else return NULL
    }
    return ip;
}
Пример #24
0
static void
inaccessable_symbols (void)
{
  bitset Vp, Vs, Pp;

  /* Find out which productions are reachable and which symbols are
     used.  Starting with an empty set of productions and a set of
     symbols which only has the start symbol in it, iterate over all
     productions until the set of productions remains unchanged for an
     iteration.  For each production which has a LHS in the set of
     reachable symbols, add the production to the set of reachable
     productions, and add all of the nonterminals in the RHS of the
     production to the set of reachable symbols.

     Consider only the (partially) reduced grammar which has only
     nonterminals in N and productions in P.

     The result is the set P of productions in the reduced grammar,
     and the set V of symbols in the reduced grammar.

     Although this algorithm also computes the set of terminals which
     are reachable, no terminal will be deleted from the grammar. Some
     terminals might not be in the grammar but might be generated by
     semantic routines, and so the user might want them available with
     specified numbers.  (Is this true?)  However, the nonreachable
     terminals are printed (if running in verbose mode) so that the
     user can know.  */

  Vp = bitset_create (nsyms, BITSET_FIXED);
  Pp = bitset_create (nrules, BITSET_FIXED);

  /* If the start symbol isn't useful, then nothing will be useful. */
  if (bitset_test (N, accept->number - ntokens))
    {
      bitset_set (V, accept->number);

      while (1)
        {
          rule_number r;
          bitset_copy (Vp, V);
          for (r = 0; r < nrules; r++)
            {
              if (!bitset_test (Pp, r)
                  && bitset_test (P, r)
                  && bitset_test (V, rules[r].lhs->number))
                {
                  item_number *rhsp;
                  for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++)
                    if (ISTOKEN (*rhsp) || bitset_test (N, *rhsp - ntokens))
                      bitset_set (Vp, *rhsp);
                  bitset_set (Pp, r);
                }
            }
          if (bitset_equal_p (V, Vp))
            break;
          Vs = Vp;
          Vp = V;
          V = Vs;
        }
    }

  bitset_free (V);
  V = Vp;

  /* Tokens 0, 1, and 2 are internal to Bison.  Consider them useful. */
  bitset_set (V, endtoken->number);             /* end-of-input token */
  bitset_set (V, errtoken->number);             /* error token */
  bitset_set (V, undeftoken->number);           /* some undefined token */

  bitset_free (P);
  P = Pp;

  nuseful_productions = bitset_count (P);
  nuseless_productions = nrules - nuseful_productions;

  nuseful_nonterminals = 0;
  {
    symbol_number i;
    for (i = ntokens; i < nsyms; i++)
      if (bitset_test (V, i))
        nuseful_nonterminals++;
  }
  nuseless_nonterminals = nvars - nuseful_nonterminals;

  /* A token that was used in %prec should not be warned about.  */
  {
    rule_number r;
    for (r = 0; r < nrules; ++r)
      if (rules[r].precsym != 0)
        bitset_set (V1, rules[r].precsym->number);
  }
}
Пример #25
0
static
void test_get_set()
{
	header();

	struct bitset bm;
	bitset_create(&bm, realloc);

	const size_t NUM_SIZE = (size_t) 1 << 14;
	size_t *nums = malloc(NUM_SIZE * sizeof(size_t));

	printf("Generating test set... ");
	for(size_t i = 0; i < NUM_SIZE; i++) {
		nums[i] = rand();
	}
	printf("ok\n");

	printf("Settings bits... ");
	for(size_t i = 0; i < NUM_SIZE; i++) {
		fail_if(bitset_set(&bm, nums[i]) < 0);
	}
	printf("ok\n");

	printf("Checking bits... ");
	shuffle(nums, NUM_SIZE);
	for(size_t i = 0; i < NUM_SIZE; i++) {
		fail_unless(bitset_test(&bm, nums[i]));
	}
	printf("ok\n");

	printf("Unsetting random bits... ");
	shuffle(nums, NUM_SIZE);
	for(size_t i = 0; i < NUM_SIZE; i++) {
		if (nums[i] % 5 == 0) {
			fail_if(bitset_clear(&bm, nums[i]) < 0);
			// printf("Clear :%zu\n", nums[i]);
			fail_if(bitset_test(&bm, nums[i]));
		}
	}
	printf("ok\n");

	printf("Checking set bits... ");
	shuffle(nums, NUM_SIZE);
	for(size_t i = 0; i < NUM_SIZE; i++) {
		if (nums[i] % 5 == 0) {
			continue;
		}

		if (!bitset_test(&bm, nums[i])) {
			printf("Fail :%zu\n", nums[i]);
		}
		fail_unless(bitset_test(&bm, nums[i]));
	}
	printf("ok\n");

	printf("Checking all bits... ");
	qsort(nums, NUM_SIZE, sizeof(size_t), size_compator);

	size_t *pn = nums;

	size_t i_max = (size_t) 1 << 14;
	if (i_max > RAND_MAX) {
		i_max = RAND_MAX;
	}

	for(size_t i = 0; i < i_max; i++) {
		if (*pn < SIZE_MAX && *pn == i) {
			fail_unless(bitset_test(&bm, *pn));
			pn++;
		} else {
			fail_if(bitset_test(&bm, i));
		}
	}
	printf("ok\n");


	printf("Unsetting all bits... ");
	shuffle(nums, NUM_SIZE);
	for(size_t i = 0; i < NUM_SIZE; i++) {
		if (nums[i] == SIZE_MAX) {
			continue;
		}

		fail_if(bitset_clear(&bm, nums[i]) < 0);
	}
	printf("ok\n");


	printf("Checking all bits... ");
	for(size_t i = 0; i < i_max; i++) {
		fail_if(bitset_test(&bm, i));
	}
	printf("ok\n");

	free(nums);

	bitset_destroy(&bm);

	footer();
}
Пример #26
0
/**
 * \note
 *   - FIXME: It might be an interesting experiment to compare the space and
 *     time efficiency of computing \c item_lookahead_sets either:
 *     - Fully up front.
 *     - Just-in-time, as implemented below.
 *     - Not at all.  That is, just let annotations continue even when
 *       unnecessary.
 */
bool
ielr_item_has_lookahead (state *s, symbol_number lhs, size_t item,
                         symbol_number lookahead, state ***predecessors,
                         bitset **item_lookahead_sets)
{
  if (!item_lookahead_sets[s->number])
    {
      size_t i;
      item_lookahead_sets[s->number] =
        xnmalloc (s->nitems, sizeof item_lookahead_sets[s->number][0]);
      for (i = 0; i < s->nitems; ++i)
        item_lookahead_sets[s->number][i] = NULL;
    }
  if (!item_lookahead_sets[s->number][item])
    {
      item_lookahead_sets[s->number][item] =
        bitset_create (ntokens, BITSET_FIXED);
      /* If this kernel item is the beginning of a RHS, it must be the kernel
         item in the start state, and so its LHS has no follows and no goto to
         check.  If, instead, this kernel item is the successor of the start
         state's kernel item, there are still no follows and no goto.  This
         situation is fortunate because we want to avoid the - 2 below in both
         cases.

         Actually, IELR(1) should never invoke this function for either of
         those cases because (1) follow_kernel_items will never reference a
         kernel item for this RHS because the end token blocks sight of the
         lookahead set from the RHS's only nonterminal, and (2) no reduction
         has a lookback dependency on this lookahead set.  Nevertheless, I
         didn't change this test to an aver just in case the usage of this
         function evolves to need those two cases.  In both cases, the current
         implementation returns the right result.  */
      if (s->items[item] > 1)
        {
          /* If the LHS symbol of this item isn't known (because this is a
             top-level invocation), go get it.  */
          if (!lhs)
            {
              unsigned int i;
              for (i = s->items[item];
                   !item_number_is_rule_number (ritem[i]);
                   ++i)
                ;
              lhs = rules[item_number_as_rule_number (ritem[i])].lhs->number;
            }
          /* If this kernel item is next to the beginning of the RHS, then
             check all predecessors' goto follows for the LHS.  */
          if (item_number_is_rule_number (ritem[s->items[item] - 2]))
            {
              state **predecessor;
              aver (lhs != accept->number);
              for (predecessor = predecessors[s->number];
                   *predecessor;
                   ++predecessor)
                bitset_or (item_lookahead_sets[s->number][item],
                           item_lookahead_sets[s->number][item],
                           goto_follows[map_goto ((*predecessor)->number,
                                                  lhs)]);
            }
          /* If this kernel item is later in the RHS, then check all
             predecessor items' lookahead sets.  */
          else
            {
              state **predecessor;
              for (predecessor = predecessors[s->number];
                   *predecessor;
                   ++predecessor)
                {
                  size_t predecessor_item;
                  for (predecessor_item = 0;
                       predecessor_item < (*predecessor)->nitems;
                       ++predecessor_item)
                    if ((*predecessor)->items[predecessor_item]
                        == s->items[item] - 1)
                      break;
                  aver (predecessor_item != (*predecessor)->nitems);
                  ielr_item_has_lookahead (*predecessor, lhs,
                                           predecessor_item, 0 /*irrelevant*/,
                                           predecessors, item_lookahead_sets);
                  bitset_or (item_lookahead_sets[s->number][item],
                             item_lookahead_sets[s->number][item],
                             item_lookahead_sets[(*predecessor)->number]
                                                [predecessor_item]);
                }
            }
        }
    }
  return bitset_test (item_lookahead_sets[s->number][item], lookahead);
}
Пример #27
0
/**
 * \pre:
 *   - \c ritem_sees_lookahead_set was computed by
 *     \c ielr_compute_ritem_sees_lookahead_set.
 * \post:
 *   - Each of \c *edgesp and \c *edge_countsp is a new array of size
 *     \c ::ngotos.
 *   - <tt>(*edgesp)[i]</tt> points to a \c goto_number array of size
 *     <tt>(*edge_countsp)[i]+1</tt>.
 *   - In such a \c goto_number array, the last element is \c ::END_NODE.
 *   - All remaining elements are the indices of the gotos to which there is an
 *     internal follow edge from goto \c i.
 *   - There is an internal follow edge from goto \c i to goto \c j iff both:
 *     - The from states of gotos \c i and \c j are the same.
 *     - The transition nonterminal for goto \c i appears as the first RHS
 *       symbol of at least one production for which both:
 *       - The LHS is the transition symbol of goto \c j.
 *       - All other RHS symbols are nullable nonterminals.
 *     - In other words, the follows of goto \c i include the follows of
 *       goto \c j and it's an internal edge because the from states are the
 *       same.
 */
static void
ielr_compute_internal_follow_edges (bitset ritem_sees_lookahead_set,
                                    goto_number ***edgesp, int **edge_countsp)
{
  *edgesp = xnmalloc (ngotos, sizeof **edgesp);
  *edge_countsp = xnmalloc (ngotos, sizeof **edge_countsp);
  {
    bitset sources = bitset_create (ngotos, BITSET_FIXED);
    goto_number i;
    for (i = 0; i < ngotos; ++i)
      (*edge_countsp)[i] = 0;
    for (i = 0; i < ngotos; ++i)
      {
        int nsources = 0;
        {
          rule **rulep;
          for (rulep = derives[states[to_state[i]]->accessing_symbol
                               - ntokens];
               *rulep;
               ++rulep)
            {
              /* If there is at least one RHS symbol, if the first RHS symbol
                 is a nonterminal, and if all remaining RHS symbols (if any)
                 are nullable nonterminals, create an edge from the LHS
                 symbol's goto to the first RHS symbol's goto such that the RHS
                 symbol's goto will be the source of the edge after the
                 eventual relation_transpose below.

                 Unlike in ielr_compute_always_follows, I use a bitset for
                 edges rather than an array because it is possible that
                 multiple RHS's with the same first symbol could fit and thus
                 that we could end up with redundant edges.  With the
                 possibility of redundant edges, it's hard to know ahead of
                 time how large to make such an array.  Another possible
                 redundancy is that source and destination might be the same
                 goto.  Eliminating all these possible redundancies now might
                 possibly help performance a little.  I have not proven any of
                 this, but I'm guessing the bitset shouldn't entail much of a
                 performance penalty, if any.  */
              if (bitset_test (ritem_sees_lookahead_set,
                               (*rulep)->rhs - ritem))
                {
                  goto_number source =
                    map_goto (from_state[i],
                              item_number_as_symbol_number (*(*rulep)->rhs));
                  if (i != source && !bitset_test (sources, source))
                    {
                      bitset_set (sources, source);
                      ++nsources;
                      ++(*edge_countsp)[source];
                    }
                }
            }
        }
        if (nsources == 0)
          (*edgesp)[i] = NULL;
        else
          {
            (*edgesp)[i] = xnmalloc (nsources + 1, sizeof *(*edgesp)[i]);
            {
              bitset_iterator biter_source;
              bitset_bindex source;
              int j = 0;
              BITSET_FOR_EACH (biter_source, sources, source, 0)
                (*edgesp)[i][j++] = source;
            }
            (*edgesp)[i][nsources] = END_NODE;
          }
        bitset_zero (sources);
      }
    bitset_free (sources);
  }

  relation_transpose (edgesp, ngotos);

  if (trace_flag & trace_ielr)
    {
      fprintf (stderr, "internal_follow_edges:\n");
      relation_print (*edgesp, ngotos, stderr);
    }
}
Пример #28
0
static void setclear(){
	int i;
	bitset_t set=bitset_create(16,16);

    printf("setting 5 and 60\n");
	bitset_set(set,5);
	bitset_set(set,60);
	for(i=0;i<80;i++) printf("%s",bitset_test(set,i)?"1":"0");
	printf("\n");
	
	int N=10;
	printf("%d in set is %d\n",N,bitset_test(set,N));
	printf("set %d\n",N);
    bitset_set(set,N);
	printf("%d in set is %d\n",N,bitset_test(set,N));
	printf("clear %d\n",N);
	bitset_clear(set,N);
	printf("%d in set is %d\n",N,bitset_test(set,N));

	N=100;
	printf("%d in set is %d\n",N,bitset_test(set,N));
	printf("set %d\n",N);
	bitset_set(set,N);
	printf("%d in set is %d\n",10,bitset_test(set,10));
	printf("%d in set is %d\n",N,bitset_test(set,N));
	printf("clear %d\n",N);
	bitset_clear(set,N);
	printf("%d in set is %d\n",N,bitset_test(set,N));

	N=200;
	printf("%d in set is %d\n",N,bitset_test(set,N));
	bitset_fprint(stdout,set);
	printf("set %d\n",N);
	bitset_set(set,N);
	bitset_fprint(stdout,set);
	printf("%d in set is %d\n",10,bitset_test(set,10));
	printf("%d in set is %d\n",N,bitset_test(set,N));
	printf("clear %d\n",N);
	bitset_clear(set,N);
	printf("%d in set is %d\n",N,bitset_test(set,N));
	bitset_fprint(stdout,set);
	printf("\n");

	N=1000000;
	printf("%d in set is %d\n",N,bitset_test(set,N));
	printf("set %d\n",N);
	bitset_set(set,N);
	printf("%d in set is %d\n",10,bitset_test(set,10));
	printf("%d in set is %d\n",N,bitset_test(set,N));
	printf("clear %d\n",N);
	bitset_clear(set,N);
	printf("%d in set is %d\n",N,bitset_test(set,N));

	N=10000;
	printf("%d in set is %d\n",N,bitset_test(set,N));
	printf("set %d\n",N);
	bitset_set(set,N);
	printf("%d in set is %d\n",10,bitset_test(set,10));
	printf("%d in set is %d\n",N,bitset_test(set,N));
	printf("clear %d\n",N);
	bitset_clear(set,N);
	printf("%d in set is %d\n",N,bitset_test(set,N));

    printf("setting 5 and 60\n");
	bitset_set(set,5);
	bitset_set(set,60);
	for(i=0;i<80;i++) printf("%s",bitset_test(set,i)?"1":"0");
	printf("\n");
	
	
	N=1222333;
	printf("set %d\n",N);
	bitset_set(set,N);
	
	printf("set:{");
	for(element_t e=0;bitset_next_set(set,&e);e++){
	    printf(" %u",e);
	}
	printf(" }\n");
	
	bitset_destroy(set);
}
Пример #29
0
void set2_reduce_strong(lts_t lts){
	int *map,count,*newmap,i,*tmp,iter,setcount,j,set;
	bitset_t has_repr;

	has_repr=bitset_create(8,16);
	lts_uniq(lts);
	lts_sort(lts);
	lts_set_type(lts,LTS_BLOCK);
	map=(int*)malloc(sizeof(int)*lts->states);
	newmap=(int*)malloc(sizeof(int)*lts->states);
	if (!map || !newmap || !has_repr) Fatal(1,1,"out of memory");
	for(i=0;i<lts->states;i++){
		map[i]=0;
	}
	count=1;
	iter=0;
	for(;;){
		SetClear(-1);
		iter++;
		setcount=0;
		for(i=0;i<lts->states;i++){
			set=EMPTY_SET;
			for(j=lts->begin[i];j<lts->begin[i+1];j++){
				set=SetInsert(set,lts->label[j],map[lts->dest[j]]);
			}
			newmap[i]=set;
			if (!bitset_test(has_repr,set)){
				bitset_set(has_repr,set);
				setcount++;
			}
		}
		Warning(2,"count is %d",setcount);
		if(count==setcount) break;
		bitset_clear_all(has_repr);
		count=setcount;
		tmp=map;
		map=newmap;
		newmap=tmp;
	}
	setcount=0;
	for(i=0;i<lts->states;i++){
		newmap[i]=SetGetTag(map[i]);
		if (newmap[i]<0) {
			SetSetTag(map[i],setcount);
			newmap[i]=setcount;
			setcount++;
		}
	}
	free(map);
	map=newmap;
	Warning(2,"final count is %d",setcount);
	SetFree();
	lts_set_type(lts,LTS_LIST);
	lts->root=map[lts->root];
        lts->root2=map[lts->root2];
	for(i=0;i<lts->transitions;i++){
		lts->src[i]=map[lts->src[i]];
		lts->dest[i]=map[lts->dest[i]];
	}
	lts->states=count;
	lts_uniq(lts);
	free(map);
	bitset_destroy(has_repr);
	Warning(1,"reduction took %d iterations",iter);
}