view anagram/agcore/cra.cpp @ 5:7661c1604e49

Add additional operator delete calls gcc 10 asked for.
author David A. Holland
date Mon, 30 May 2022 23:32:56 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * AnaGram, A System for Syntax Directed Programming
 * Copyright 1993-1999 Parsifal Software. All Rights Reserved.
 * See the file COPYING for license and usage terms.
 *
 * cra.cpp - Chain Reduction Analysis
 */

#include <string.h>
#include "port.h"

#include "arrays.h"
#include "assert.h"
#include "cra.h"
#include "data.h"
#include "q1glbl.h"
#include "q5.h"
#include "rule.h"
#include "token.h"
#include "tsd.h"

//#define INCLUDE_LOGGING
#include "log.h"


static int *complete_form;


static int crax(Token completingToken, Rule rule) {
  LOGSECTION("crax");
  LOGV(completingToken) LCV(rule);
  RuleDescriptor &ruleDescriptor(rule);

/*
  if (rule.isNotNull() && rule->proc_name == 0
      && rule->not_unique_reduction == 0) {
    if (rule->length() == 1) {
      Token primaryToken = rule->prim_tkn;
*/
  if (rule.isNotNull() && ruleDescriptor.proc_name == 0
      && ruleDescriptor.not_unique_reduction == 0) {
    if (ruleDescriptor.length() == 1) {
      Token primaryToken = ruleDescriptor.prim_tkn;
      if (!primaryToken->subgrammar) {
        if (tut[primaryToken]) {
          at(lgt, (int)completingToken, complete_form[primaryToken]);
          LOGS("appendTuple to lgt") LV(completingToken)
	    LCV(complete_form[primaryToken]);
          return 1;
        }
        LOGV(primaryToken) LCV(complete_form[primaryToken]);
        if (complete_form[primaryToken] != -1) {
	  return crax(completingToken, complete_form[primaryToken]);
	}
      }
    }
  }
  at(lcftp, (int) completingToken, (int) rule);
  return 0;
}

void cra(void) {
  LOGSECTION("cra");
  int *p, *q;
  int i, k, n;
  int s, t, f;
  state_number_map *msn;

  if (lcft->nt == 0) {
    return;
  }
  msn = &map_state_number[kits];
  LOGV(kits);
  k = 0;
  //complete_form = token_list;
  complete_form = local_array(ntkns+1, int);
  memset(complete_form, -1, sizeof(*complete_form)*(ntkns+1));
  memset(tut, 0, sizeof(*tut) * (ntkns+1));
  q = lcft->sb;
  n = lcft->nt;
  for (i = 0; i< n; i++) {
    t = *q++;
    f = *q++;
    LOGV(t) LCV(f) LCV(tut[t]);
    assert(complete_form[t] == -1);
    complete_form[t] = f;
  }
  q = lgt->sb;
  n = lgt->nt;
  for (i = 0; i < n; i++) {
    t = *q++;
    s = *q++;
    tut[t] = 1;
    LOGV(t) LCV(s) LCV(tut[t]);
    assert(complete_form[t] == -1);
    complete_form[t] = s;
  }
  p = lcft->sb;
  n = lcft->nt;
  reset_tsd(lcftp);
  for (i = 0; i < n; i++) {
    t = *p++;
    f = *p++;
    k += crax(t, f);
  }
  if (lcftp->nt) {
    chain_completions_list = (unsigned *)
      size_prop(chain_completions_list, lcftp->nt);
    msn->chain_completions_index = store_tuples(lcftp, chain_completions_list);
    msn->n_chain_completions = lcftp->nt;
  }
  if (k) {
    chain_gotos_list = (unsigned *) size_prop(chain_gotos_list, lgt->nt);
    msn->chain_gotos_index = store_tuples(lgt, chain_gotos_list);
    msn->n_chain_gotos = lgt->nt;
    //check_counter_overflow(msn->n_chain_gotos == lgt->nt, "Chain gotos");
  }
  LOGV(lcft->nt) LCV(lcftp->nt) LCV(msn->n_gotos) LCV(msn->n_chain_gotos);
  assert(lcftp->nt + k == lcft->nt);
}