Beispiel #1
0
void h_symbol_put(HParseState *state, const char* key, void *value) {
  if (!state->symbol_table) {
    state->symbol_table = h_slist_new(state->arena);
    h_slist_push(state->symbol_table, h_hashtable_new(state->arena,
						      h_eq_ptr,
						      h_hash_ptr));
  }
  HHashTable *head = h_slist_top(state->symbol_table);
  assert(!h_hashtable_present(head, key));
  h_hashtable_put(head, key, value);
}
Beispiel #2
0
HSlist* h_slist_copy(HSlist *slist) {
  HSlist *ret = h_slist_new(slist->arena);
  HSlistNode *head = slist->head;
  HSlistNode *tail;
  if (head != NULL) {
    h_slist_push(ret, head->elem);
    tail = ret->head;
    head = head->next;
    while (head != NULL) {
      // append head item to tail in a new node
      HSlistNode *node = h_arena_malloc(slist->arena, sizeof(HSlistNode));
      node->elem = head->elem;
      node->next = NULL;
      tail = tail->next = node;
      head = head->next;
    }
  }
  return ret;
}
Beispiel #3
0
void setupLR(const HParser *p, HParseState *state, HLeftRec *rec_detect) {
  if (!rec_detect->head) {
    HRecursionHead *some = a_new(HRecursionHead, 1);
    some->head_parser = p;
    some->involved_set = h_slist_new(state->arena);
    some->eval_set = NULL;
    rec_detect->head = some;
  }

  HSlistNode *it;
  for(it=state->lr_stack->head; it; it=it->next) {
    HLeftRec *lr = it->elem;

    if(lr->rule == p)
      break;

    lr->head = rec_detect->head;
    h_slist_push(lr->head->involved_set, (void*)lr->rule);
  }
}
Beispiel #4
0
/* Warth's recursion. Hi Alessandro! */
HParseResult* h_do_parse(const HParser* parser, HParseState *state) {
  HParserCacheKey *key = a_new(HParserCacheKey, 1);
  key->input_pos = state->input_stream; key->parser = parser;
  HParserCacheValue *m = recall(key, state);
  // check to see if there is already a result for this object...
  if (!m) {
    // It doesn't exist, so create a dummy result to cache
    HLeftRec *base = a_new(HLeftRec, 1);
    base->seed = NULL; base->rule = parser; base->head = NULL;
    h_slist_push(state->lr_stack, base);
    // cache it
    h_hashtable_put(state->cache, key, cached_lr(state, base));
    // parse the input
    HParseResult *tmp_res = perform_lowlevel_parse(state, parser);
    // the base variable has passed equality tests with the cache
    h_slist_pop(state->lr_stack);
    // update the cached value to our new position
    HParserCacheValue *cached = h_hashtable_get(state->cache, key);
    assert(cached != NULL);
    cached->input_stream = state->input_stream;
    // setupLR, used below, mutates the LR to have a head if appropriate, so we check to see if we have one
    if (NULL == base->head) {
      h_hashtable_put(state->cache, key, cached_result(state, tmp_res));
      return tmp_res;
    } else {
      base->seed = tmp_res;
      HParseResult *res = lr_answer(key, state, base);
      return res;
    }
  } else {
    // it exists!
    state->input_stream = m->input_stream;
    if (PC_LEFT == m->value_type) {
      setupLR(parser, state, m->left);
      return m->left->seed;
    } else {
      return m->right;
    }
  }
}
Beispiel #5
0
int h_lalr_compile(HAllocator* mm__, HParser* parser, const void* params)
{
  // generate (augmented) CFG from parser
  // construct LR(0) DFA
  // build LR(0) table
  // if necessary, resolve conflicts "by conversion to SLR"

  if (!parser->vtable->isValidCF(parser->env)) {
    return -1;
  }
  HCFGrammar *g = h_cfgrammar_(mm__, h_desugar_augmented(mm__, parser));
  if(g == NULL)     // backend not suitable (language not context-free)
    return -1;

  HLRDFA *dfa = h_lr0_dfa(g);
  if (dfa == NULL) {     // this should normally not happen
    h_cfgrammar_free(g);
    return -1;
  }

  HLRTable *table = h_lr0_table(g, dfa);
  if (table == NULL) {   // this should normally not happen
    h_cfgrammar_free(g);
    return -1;
  }

  if(has_conflicts(table)) {
    HArena *arena = table->arena;

    HLREnhGrammar *eg = enhance_grammar(g, dfa, table);
    if(eg == NULL) {    // this should normally not happen
      h_cfgrammar_free(g);
      h_lrtable_free(table);
      return -1;
    }

    // go through the inadequate states; replace inadeq with a new list
    HSlist *inadeq = table->inadeq;
    table->inadeq = h_slist_new(arena);
    
    for(HSlistNode *x=inadeq->head; x; x=x->next) {
      size_t state = (uintptr_t)x->elem;
      bool inadeq = false;
      
      // clear old forall entry, it's being replaced by more fine-grained ones
      table->forall[state] = NULL;

      // go through each reducible item of state
      H_FOREACH_KEY(dfa->states[state], HLRItem *item)
        if(item->mark < item->len)
          continue;

        // action to place in the table cells indicated by lookahead
        HLRAction *action = h_reduce_action(arena, item);

        // find all LR(0)-enhanced productions matching item
        HHashSet *lhss = h_hashtable_get(eg->corr, item->lhs);
        assert(lhss != NULL);
        H_FOREACH_KEY(lhss, HCFChoice *lhs)
          assert(lhs->type == HCF_CHOICE);  // XXX could be CHARSET?

	  for(HCFSequence **p=lhs->seq; *p; p++) {
            HCFChoice **rhs = (*p)->items;
            if(!match_production(eg, rhs, item->rhs, state)) {
              continue;
	    }

            // the left-hand symbol's follow set is this production's
            // contribution to the lookahead
            const HStringMap *fs = h_follow(1, eg->grammar, lhs);
            assert(fs != NULL);
            assert(fs->epsilon_branch == NULL);
            assert(!h_stringmap_empty(fs));

            // for each lookahead symbol, put action into table cell
            if(terminals_put(table->tmap[state], fs, action) < 0)
              inadeq = true;
	  } H_END_FOREACH // enhanced production
      H_END_FOREACH  // reducible item

      if(inadeq) {
        h_slist_push(table->inadeq, (void *)(uintptr_t)state);
      }
    }
  }

  h_cfgrammar_free(g);
  parser->backend_data = table;
  return has_conflicts(table)? -1 : 0;
}