Пример #1
0
static ATermList applyImports(SDF_ImportList imports, PT_ParseTree pt)
{
  ATermList result = ATempty;

  while (!SDF_isImportListEmpty(imports)) {
    SDF_Import import = SDF_getImportListHead(imports);

    if (SDF_hasImportRenamings(import)) {
      SDF_Renamings renamings = SDF_getImportRenamings(import);

      result = ATinsert(result, 
			ATBpack(
			  PT_ParseTreeToTerm(applyRenamings(renamings, pt))));
    }
    else {
      result = ATinsert(result, ATBpack(PT_ParseTreeToTerm(pt)));
    }

    if (SDF_hasImportListTail(imports)) {
      imports = SDF_getImportListTail(imports);
    }
    else {
      break;
    }
  }

  return result;
}
Пример #2
0
int main(int argc, char **argv)
{
  int c;
  ATerm bottomOfStack;
  PT_ParseTree tree, flatTree;

  /*  Configuration items  */
  char   *input="-";
  char   *output="-";
  ATbool bafmode=ATtrue;
  ATbool verbose=ATfalse;
  ATbool proceed=ATtrue;

  extern char *optarg;
  extern int   optind;

  /*  Commandline mode  */

  while ((c = getopt(argc, argv, myarguments)) != -1) {
    switch (c) {
      case 'b':  bafmode = ATtrue;                       break;
      case 'i':  input=optarg;                           break;
      case 'o':  output=optarg;                          break;
      case 't':  bafmode = ATfalse;                      break;
      case 'v':  verbose = ATtrue;                       break;
      case 'V':  version(); proceed=ATfalse;             break;

      case 'h':
      default:   usage(); proceed=ATfalse;                     break;
    }
  }
  argc -= optind;
  argv += optind;

  ATinit(argc, argv, &bottomOfStack);
  PT_initMEPTApi();
  PT_initAsFix2Api();

  if(proceed) {
    tree = PT_ParseTreeFromTerm(ATreadFromNamedFile(input));
    flatTree = flattenPT(tree);

    if(!flatTree) {
      ATerror("%s: conversion failed.", myname);
    }
    if(bafmode) {
      ATwriteToNamedBinaryFile(PT_ParseTreeToTerm(flatTree), output);
    }
    else {
      ATwriteToNamedTextFile(PT_ParseTreeToTerm(flatTree), output);
    }
  }

  return 0;
}
static ATerm parse(const unsigned char *inputString, unsigned int inputStringLength, const char *inputPath, const char *parseTableName){
	PT_ParseTree parseTree;
	InputString sglrInputString;
	
	ERR_resetErrorManager();
	
	sglrInputString = IS_allocateString(inputPath, inputString, inputStringLength);
	
	if(inputString == NULL){
		return ERR_SummaryToTerm(ERR_getManagerSummary());
	}
	
	if(!SGLR_isParseTableLoaded(parseTableName)){
		if(SG_AddParseTable(parseTableName) == NULL){
			return ERR_SummaryToTerm(ERR_getManagerSummary());
		}
	}
	
	parseTree = SGLR_parse(sglrInputString, parseTableName);
	
	if(parseTree == NULL){
		return ERR_SummaryToTerm(ERR_getManagerSummary());
	}
	
	return PT_ParseTreeToTerm(parseTree);
}
Пример #4
0
ATerm normalize_and_generate_table(const char *name, PT_ParseTree sdf2term) {
  ATerm pt = NULL;
  PT_Tree ksdf;

  if (PGEN_getStatsFlag) { STATS_Timer(); } 

  if (PGEN_getGenerationModeFlag()) {
    ksdf = PT_getParseTreeTree(sdf2term);
  } else {
    ksdf = normalizeGrammar(name, sdf2term); 
  }

  PGEN_STATS_setCount(PGEN_STATS_normalizationTime, STATS_Timer());

  if (PGEN_getNormalizationModeFlag()) {
    return PT_ParseTreeToTerm(PT_makeValidParseTreeFromTree(ksdf));
  }

  if (ksdf)  {
    pt = sdf_generate_table(ksdf);
  }

  PGEN_STATS_setCount(PGEN_STATS_generationTime, STATS_Timer());
  
  return (ATerm)pt;
}
Пример #5
0
ATerm pretty_print(int cid, ATerm input) 
{
  PT_ParseTree parsetree = NULL;
  BOX_Start box = NULL;
  PT_Tree result = NULL;

  ERR_resetErrorManager();

  parsetree = PT_ParseTreeFromTerm(ATBunpack(input));

  if (parsetree != NULL) {
    box = pandora(parsetree);
  }

  if (box != NULL) {
    result = toText(PT_ParseTreeFromTerm(BOX_StartToTerm(box)));
  }

  if (result != NULL) {
    PT_ParseTree presult = PT_makeValidParseTreeFromTree(result);
    ATerm value = ATBpack(PT_ParseTreeToTerm(presult));
    return ATmake("snd-value(pretty-printed(<term>))", value);
  }
  else {
    ERR_Summary summary = ERR_getManagerSummary();
    return ATmake("snd-value(pretty-print-error(<term>))", summary);
  }
}
Пример #6
0
static ATerm addEqsSyntax(const char *name, ATerm term)
{
  PT_ParseTree parseTree = PT_ParseTreeFromTerm(term);
  PT_Tree ptApplied = addEqsSyntaxFunction(name, parseTree);
  ATerm reduct = innermost(ptApplied);
  PT_Tree asfix = toasfix(reduct);
  
  return PT_ParseTreeToTerm(PT_makeValidParseTreeFromTree(asfix));
}
Пример #7
0
ATerm STRSGLR_anno_position_info(ATerm pt_term, ATerm path_term)
{
    PT_ParseTree pt;
    const char* path;

    if(!AT_isString(path_term)) {
        return NULL;
    }

    path = AT_getString(path_term);
    pt = PT_ParseTreeFromTerm(pt_term);
    pt = PT_addParseTreePosInfoSome(path, pt, -1, ATfalse, ATfalse);

    return PT_ParseTreeToTerm(pt);
}
Пример #8
0
int asc_support_main(ATerm *bottomOfStack, int argc, char *argv[], void (*register_all)(), void (*resolve_all)(), void (*init_all)(), unsigned const char* tableBaf, size_t tableSize, ATbool parseInput, ATBhandler handler) {

  PT_ParseTree pt = NULL;
  PT_Tree asfix;
  PT_Tree trm;
  PT_ParseTree rpt = NULL;
  ATerm reduct;
  const char *outputFilename;
  int numberOfInputs;

  ATinit(argc, argv, bottomOfStack);
  initApis();

  register_all();
  resolve_all();
  init_all();

  initParsetable(tableBaf, tableSize);
  SGLR_initialize();

  OPT_initialize();
  ASC_initializeDefaultOptions();

  /*  Check whether we're a ToolBus process  */
  if (toolbusMode(argc, argv)) {
    ATBinit(argc, argv, bottomOfStack);
    ATBconnect(NULL, NULL, -1, asf_toolbus_handler);
    ATBeventloop();
  }
  else {    
    handleOptions(argc, argv, parseInput);

    numberOfInputs = ASC_getNumberOfParseTrees();
    outputFilename = ASC_getOutputFilename();

    if (!streq(ASC_getPrefixFunction(),"")) {
      pt = applyFunction((const char*) ASC_getPrefixFunction(), 
			 (const char*) ASC_getResultNonTermName(), numberOfInputs, inputs);
    } 
    else {
      if (numberOfInputs == 0) {
	pt = parsetreeFromFile("-", parseInput);
      }
      else if (numberOfInputs == 1) {
	pt = inputs[0];
      }
      else if (numberOfInputs != 1) {
	ATerror("Can only process one argument if no -f and -r option "
		"are supplied.\n"
		"Did a -s argument eat up your -f or -r option?\n");
	return 1;
      }
    }

    if (PT_isValidParseTree(pt)) {
      trm = PT_getParseTreeTop(pt);

      if (ASC_getVerboseFlag()) { ATfprintf(stderr,"Reducing ...\n"); }

      reduct = innermost(trm);

      if (ASC_getVerboseFlag()) { ATfprintf(stderr,"Reducing finished.\n"); }
      if (ASC_getStatsFlag())  { printStats(); }

      if (ASC_getOutputFlag()) {
	asfix = toasfix(reduct);
	rpt = PT_makeParseTreeTop(asfix, 0);

	if (parseInput) {
	  FILE *fp = NULL;

	  if (!strcmp(outputFilename, "-")) {
	    fp = stdout;
	  }
	  else {
	    fp = fopen(outputFilename, "wb");
	  }

	  if (fp != NULL) {
	    PT_yieldParseTreeToFile(rpt, fp, ATfalse);
	  }
	  else {
	    ATerror("asc-main: unable to open %s for writing\n", outputFilename);
	  }
	}
	else {
	  if (ASC_getBafmodeFlag()) {
	    ATwriteToNamedBinaryFile(PT_ParseTreeToTerm(rpt),outputFilename);
	  }
	  else {
	    ATwriteToNamedTextFile(PT_ParseTreeToTerm(rpt),outputFilename);
	  }
	}
      }
    }
  }

  return 0;
}
Пример #9
0
int main(int argc, char **argv)
{
  int c;
  ATerm bottomOfStack;
  char   *input="-";
  char   *output="-";
  char   *argument = NULL;
  ATbool minimizeSort = ATfalse;
  ATbool maximizeSort = ATfalse;
  ATerm inputTerm = NULL;
  ATerm outputTerm = NULL;
  PT_ParseTree inputTree = NULL;
  PT_ParseTree outputTree = NULL;

  while ((c = getopt(argc, argv, myarguments)) != -1) {
    switch (c) {
      case 'i':  
	input=optarg;
	break;
      case 'n':
	minimizeSort = ATtrue;
	argument = optarg;
	break;
      case 'N':
	maximizeSort = ATtrue;
	argument = optarg;
	break;
      case 'o':  
	output=optarg;     
	break;
      case 'V':  
	version();
	return 0;
      case 'h':
	usage();
	return 0;
      default:   
	usage();
	return 1;
    }
  }

  ATinit(argc, argv, &bottomOfStack);     
  PT_initMEPTApi();

  inputTerm = ATreadFromNamedFile(input);
  inputTree = PT_ParseTreeFromTerm(inputTerm);

  if (inputTerm != NULL) {
    /* The tool will always execute one filter at a time, such that the user
     * can implement his own scheduling by repeatedly using this tool.
     * Please do not change this behavior.
     */
    if (XOR(minimizeSort, maximizeSort) && argument != NULL) {
      PT_Symbol sort = PT_makeSymbolCf(PT_makeSymbolSort(argument));
      outputTree = minmax(inputTree, sort, minimizeSort);
    }
    else {
      usage();
      return 1;
    }

    if (outputTree != NULL) {
      outputTree = flattenAgain(outputTree);
      outputTerm = PT_ParseTreeToTerm(outputTree);

      ATwriteToNamedTextFile(outputTerm, output);
    }
    else {
      fprintf(stderr, "%s: filtering failed unexpectedly\n", myname);
      return 1;
    }
  }
  else {
    fprintf(stderr,"%s: could not open file %s\n", myname, input);
    return 1;
  }

  return 0;
}