Ejemplo n.º 1
0
void
_VLO_expand_memory (vlo_t *vlo, size_t additional_length)
{
  size_t vlo_length;
  char *new_vlo_start;

  assert (vlo->vlo_start != NULL);
  vlo_length = VLO_LENGTH (*vlo) + additional_length;
  vlo_length += vlo_length / 2 + 1;
  REALLOC (new_vlo_start, vlo->vlo_start, vlo_length);
  if (new_vlo_start != vlo->vlo_start)
    {
      vlo->vlo_free += new_vlo_start - vlo->vlo_start;
      vlo->vlo_start = new_vlo_start;
    }
  vlo->vlo_boundary = vlo->vlo_start + vlo_length;
}
Ejemplo n.º 2
0
void
_VLO_tailor_function (vlo_t *vlo)
{
  size_t vlo_length;
  char *new_vlo_start;

  assert (vlo->vlo_start != NULL);
  vlo_length = VLO_LENGTH (*vlo);
  if (vlo_length == 0)
    vlo_length = 1;
  REALLOC (new_vlo_start, vlo->vlo_start, vlo_length);
  if (new_vlo_start != vlo->vlo_start)
    {
      vlo->vlo_free += new_vlo_start - vlo->vlo_start;
      vlo->vlo_start = new_vlo_start;
    }
  vlo->vlo_boundary = vlo->vlo_start + vlo_length;
}
Ejemplo n.º 3
0
static void
set_up_LR_set_look_ahead_trie (IR_node_t LR_set)
{
  IR_node_t *the_rightmost_trie_nodes_array;
  IR_node_t current_LR_set_look_ahead_trie_node;
  IR_node_t previous_brother;
  int current_look_ahead;
  struct LR_set_look_ahead_trie_token_strings_vector_element
         *current_token_string_ptr;
  int current_the_rightmost_trie_nodes_array_size;
  IR_node_t default_reduce_LR_situation;
  IR_node_t first_LR_set_look_ahead_trie_node;
  IR_node_t single_term_definition;
  IR_node_t back_track_alternative;
  vlo_t the_rightmost_trie_nodes;

#ifndef NDEBUG
  if (debug_level >= 2)
    {
      fprintf (stderr, "build look ahead trie of LR-set:\n");
      output_LR_set_situations (stderr, LR_set, "\t");
    }
#endif
  assert (IR_reachable_flag (LR_set));
  VLO_NULLIFY (LR_set_look_ahead_trie_token_strings_vector);
  default_reduce_LR_situation = NULL;
  for (LR_situation_corresponding_context = IR_LR_situation_list (LR_set);
       LR_situation_corresponding_context != NULL;
       LR_situation_corresponding_context
         = IR_next_LR_situation (LR_situation_corresponding_context))
    {
#ifndef NDEBUG
      if (debug_level >= 2)
        {
          fprintf (stderr, "  process LR-situation:");
          output_LR_situation (stderr, LR_situation_corresponding_context,
                               "\t", TRUE);
        }
#endif
      if (IR_IS_OF_TYPE (IR_element_after_dot
                         (LR_situation_corresponding_context),
                         IR_NM_canonical_rule_end))
        {
          if (IR_look_ahead_context (LR_situation_corresponding_context)
              != NULL)
            process_context_token_strings
              (IR_look_ahead_context (LR_situation_corresponding_context),
               add_token_string_to_LR_set_lookahead_trie_token_strings_vector);
          else
            {
              assert (default_reduce_LR_situation == NULL);
              default_reduce_LR_situation = LR_situation_corresponding_context;
            }
        }
      else if (IR_first_symbol_LR_situation
               (LR_situation_corresponding_context)
               && (!IR_goto_arc_has_been_removed
                   (LR_situation_corresponding_context)
                   || (IR_corresponding_regular_arc
                       (LR_situation_corresponding_context) != NULL))
               && IR_IS_OF_TYPE (IR_element_itself
                                 (IR_element_after_dot
                                  (LR_situation_corresponding_context)),
                                 IR_NM_single_term_definition))
        {
          if (IR_look_ahead_context (LR_situation_corresponding_context)
              == NULL)
            {
              single_term_definition
                = IR_element_itself (IR_element_after_dot
                                     (LR_situation_corresponding_context));
              add_token_string_to_LR_set_lookahead_trie_token_strings_vector
                (get_new_token_string (&single_term_definition, 1));
            }
          else
            process_context_token_strings
              (IR_look_ahead_context (LR_situation_corresponding_context),
               add_token_string_to_LR_set_lookahead_trie_token_strings_vector);
        }
    }
  qsort (VLO_BEGIN (LR_set_look_ahead_trie_token_strings_vector),
         VLO_LENGTH (LR_set_look_ahead_trie_token_strings_vector)
         / sizeof (struct LR_set_look_ahead_trie_token_strings_vector_element),
         sizeof (struct LR_set_look_ahead_trie_token_strings_vector_element),
         compare_LR_set_look_ahead_trie_token_strings);
  VLO_CREATE (the_rightmost_trie_nodes,
              max_look_ahead_number * sizeof (IR_node_t)); 
  the_rightmost_trie_nodes_array = VLO_BEGIN (the_rightmost_trie_nodes);
  current_the_rightmost_trie_nodes_array_size = 0;
  first_LR_set_look_ahead_trie_node = NULL;
  for (current_token_string_ptr
       = VLO_BEGIN (LR_set_look_ahead_trie_token_strings_vector);
       (char *) current_token_string_ptr
       <= (char *) VLO_END (LR_set_look_ahead_trie_token_strings_vector);
       current_token_string_ptr++)
    {
      previous_brother = NULL;
      for (current_look_ahead = 0;
           current_look_ahead < current_the_rightmost_trie_nodes_array_size;
           current_look_ahead++)
        {
          single_term_definition
            = get_n_th_token (current_token_string_ptr->token_string,
                              current_look_ahead);
          previous_brother
            = the_rightmost_trie_nodes_array [current_look_ahead];
          assert (previous_brother != NULL
                  /* There is not possible situation when there are token
                     strings 'ab' and 'abc' in the contexts (i.e. one
                     token string is a prefix of the second token string).
                     The following reflects it. */
                  && single_term_definition != NULL);
          if (IR_corresponding_single_term_definition (previous_brother)
              != single_term_definition)
            break;
        }
      if (current_look_ahead < current_the_rightmost_trie_nodes_array_size
          || ((current_token_string_ptr
               == VLO_BEGIN (LR_set_look_ahead_trie_token_strings_vector))
              && current_the_rightmost_trie_nodes_array_size == 0
              && previous_brother == NULL))
        {
          single_term_definition
            = get_n_th_token (current_token_string_ptr->token_string,
                              current_look_ahead);
          current_LR_set_look_ahead_trie_node
            = get_free_LR_set_look_ahead_trie_node (single_term_definition);
          if (previous_brother == NULL)
            first_LR_set_look_ahead_trie_node
              = current_LR_set_look_ahead_trie_node;
          else
            IR_set_next_brother (previous_brother,
                                 current_LR_set_look_ahead_trie_node);
          for (;;)
            {
              the_rightmost_trie_nodes_array [current_look_ahead]
                = current_LR_set_look_ahead_trie_node;
              current_look_ahead++;
              current_the_rightmost_trie_nodes_array_size = current_look_ahead;
              single_term_definition
                = get_n_th_token (current_token_string_ptr->token_string,
                                  current_look_ahead);
              if (single_term_definition == NULL)
                break;
              IR_set_first_son
                (current_LR_set_look_ahead_trie_node,
                 get_free_LR_set_look_ahead_trie_node
                 (single_term_definition));
              current_LR_set_look_ahead_trie_node
                = IR_first_son (current_LR_set_look_ahead_trie_node);
            }
          IR_set_corresponding_LR_situation
            (current_LR_set_look_ahead_trie_node,
             current_token_string_ptr->corresponding_LR_situation);
        }
      else
        {
          /* New back track alternative: */
          assert (current_look_ahead == 1
                  && IR_under_control_point_flag (current_token_string_ptr
                                                  ->corresponding_LR_situation)
                  && IR_under_control_point_flag (IR_corresponding_LR_situation
                                                  (previous_brother)));
          back_track_alternative
            = IR_new_back_track_alternative (previous_brother, NULL);
          if (IR_first_back_track_alternative (previous_brother) == NULL)
            {
              IR_set_first_back_track_alternative (previous_brother,
                                                   back_track_alternative);
              IR_set_last_back_track_alternative (previous_brother,
                                                  back_track_alternative);
            }
          else
            IR_set_last_back_track_alternative
              (IR_last_back_track_alternative (previous_brother),
               back_track_alternative);
        }
    }
  VLO_DELETE (the_rightmost_trie_nodes); 
  IR_set_LR_set_look_ahead_trie (LR_set,
                                 set_up_default_LR_situation_of_trie_level
                                 (default_reduce_LR_situation,
                                  first_LR_set_look_ahead_trie_node, 0));
}
Ejemplo n.º 4
0
static IR_node_t
set_up_default_LR_situation_of_trie_level
  (IR_node_t default_reduce_LR_situation, IR_node_t first_trie_node,
   int trie_level)
{
  IR_node_t current_trie_node;
  IR_node_t previous_trie_node;
  IR_node_t next_trie_node;
  IR_node_t default_trie_node;
  IR_node_t *current_LR_situation_ptr;
  IR_node_t *next_LR_situation_ptr;
  int reduces_number;
  int shifts_number;
  IR_node_t current_LR_situation;
  vlo_t reduce_LR_situations;

  if (default_reduce_LR_situation != NULL)
    {
      assert (trie_level == 0);
      for (previous_trie_node = NULL, current_trie_node = first_trie_node;
           current_trie_node != NULL;
           previous_trie_node = current_trie_node,
           current_trie_node = IR_next_brother (current_trie_node))
        ;
      assert (previous_trie_node == NULL);
    }
  else
    {
      for (current_trie_node = first_trie_node;
           current_trie_node != NULL;
           current_trie_node = IR_next_brother (current_trie_node))
        if (IR_first_son (current_trie_node) != NULL)
          IR_set_first_son
            (current_trie_node,
             set_up_default_LR_situation_of_trie_level
             (NULL, IR_first_son (current_trie_node), trie_level + 1));
      shifts_number = 0;
      VLO_CREATE (reduce_LR_situations, 0);
      for (current_trie_node = first_trie_node;
           current_trie_node != NULL;
           current_trie_node = IR_next_brother (current_trie_node))
        if (IR_first_son (current_trie_node) == NULL)
          {
            current_LR_situation
              = IR_corresponding_LR_situation (current_trie_node);
            if (IR_IS_OF_TYPE (IR_element_after_dot (current_LR_situation),
                               IR_NM_canonical_rule_end))
              VLO_ADD_MEMORY (reduce_LR_situations,
                              &current_LR_situation, sizeof (IR_node_t));
            else
              shifts_number++;
          }
      qsort (VLO_BEGIN (reduce_LR_situations),
             VLO_LENGTH (reduce_LR_situations) / sizeof (IR_node_t),
             sizeof (IR_node_t), compare_pointers);
      default_reduce_LR_situation = NULL;
      reduces_number = 0;
      current_LR_situation_ptr = VLO_BEGIN (reduce_LR_situations);
      if (error_reduce_flag
	  || (current_LR_situation_ptr
	      > (IR_node_t *) VLO_END (reduce_LR_situations))
	  || (!IR_it_is_errored_LR_set (IR_LR_set (*current_LR_situation_ptr))
	      && (characteristic_symbol_of_LR_set
		  (IR_LR_set (*current_LR_situation_ptr))
		  != error_single_definition)))
	for (;
	     current_LR_situation_ptr
	       <= (IR_node_t *) VLO_END (reduce_LR_situations);
	     current_LR_situation_ptr = next_LR_situation_ptr)
	  {
	    for (next_LR_situation_ptr = current_LR_situation_ptr;
		 next_LR_situation_ptr
		   <= (IR_node_t *) VLO_END (reduce_LR_situations);
		 next_LR_situation_ptr++)
	      if (*current_LR_situation_ptr != *next_LR_situation_ptr)
		break;
	    if (default_reduce_LR_situation == NULL
		|| (reduces_number
		    < next_LR_situation_ptr - current_LR_situation_ptr))
	      {
		default_reduce_LR_situation = *current_LR_situation_ptr;
		reduces_number
		  = next_LR_situation_ptr - current_LR_situation_ptr;
	      }
	  }
      VLO_DELETE (reduce_LR_situations);
      if (default_reduce_LR_situation != NULL
          || (trie_level != 0 && shifts_number > reduces_number))
        {
          if (trie_level != 0 && shifts_number > reduces_number)
            default_reduce_LR_situation = NULL;
          for (previous_trie_node = NULL, current_trie_node = first_trie_node;
               current_trie_node != NULL;
               current_trie_node = next_trie_node)
            {
              next_trie_node = IR_next_brother (current_trie_node);
              if (IR_first_son (current_trie_node) == NULL
                  && ((trie_level != 0 && shifts_number > reduces_number
                       && !IR_IS_OF_TYPE (IR_element_after_dot
                                          (IR_corresponding_LR_situation
                                           (current_trie_node)),
                                          IR_NM_canonical_rule_end))
                      || (IR_corresponding_LR_situation (current_trie_node)
                          == default_reduce_LR_situation)))
                {
                  assert
                    (default_reduce_LR_situation == NULL
                     ||
                     (default_reduce_LR_situation
                      == IR_corresponding_LR_situation (current_trie_node)));
                  /* This default LR-situation is the single shift
                     LR-situatuion for look ahead number 2, 3, ... */
                  default_reduce_LR_situation           
                    = IR_corresponding_LR_situation (current_trie_node);
                  if (previous_trie_node == NULL)
                    first_trie_node = next_trie_node;
                  else
                    IR_set_next_brother (previous_trie_node, next_trie_node);
                  free_LR_set_look_ahead_trie_node (current_trie_node);
                }
              else
                previous_trie_node = current_trie_node;
            }
        }
    }
  if (default_reduce_LR_situation != NULL)
    {
      assert (previous_trie_node != NULL || first_trie_node == NULL);
      default_trie_node = get_free_LR_set_look_ahead_trie_node (NULL);
      IR_set_corresponding_LR_situation (default_trie_node,
                                         default_reduce_LR_situation);
      if (previous_trie_node == NULL)
        first_trie_node = default_trie_node;
      else
        IR_set_next_brother (previous_trie_node, default_trie_node);
    }
  return first_trie_node;
}