예제 #1
0
PT_Symbol     SDFSymbolToPtSymbol(SDF_Symbol sdfSymbol)
{
  PT_Symbol result = NULL;

  if (SDF_isSymbolStart(sdfSymbol)) {
    result = PT_makeSymbolSort("<START>"); 
  }
  else if (SDF_isSymbolFileStart(sdfSymbol)) {
    result = PT_makeSymbolSort("<Start>"); 
  }
  else if (SDF_isSymbolSort(sdfSymbol)) {
    char *str = PT_yieldTreeToString((PT_Tree) SDF_getSymbolSort(sdfSymbol),
				     ATfalse);
    result = PT_makeSymbolSort(str); 
  }
  else if (SDF_isSymbolLit(sdfSymbol)) {
    SDF_StrCon sdfLit = SDF_getSymbolString(sdfSymbol);
    const char *str = SDFStrConToString(sdfLit);
    result = PT_makeSymbolLit(str);
  }
  else if (SDF_isSymbolCiLit(sdfSymbol)) {
    SDF_SingleQuotedStrCon sdfLit = SDF_getSymbolCiString(sdfSymbol);
    const char *str = SDFCiStrConToString(sdfLit);
    result = PT_makeSymbolCilit(str);
  }
  else if (SDF_isSymbolIter(sdfSymbol)) {
    SDF_Symbol sdfIterSymbol = SDF_getSymbolSymbol(sdfSymbol);
    PT_Symbol ptIterSymbol = SDFSymbolToPtSymbol(sdfIterSymbol);
    result = PT_makeSymbolIterPlus(ptIterSymbol);
  }
  else if (SDF_isSymbolIterStar(sdfSymbol)) {
    SDF_Symbol sdfIterSymbol = SDF_getSymbolSymbol(sdfSymbol);
    PT_Symbol ptIterSymbol = SDFSymbolToPtSymbol(sdfIterSymbol);
    result = PT_makeSymbolIterStar(ptIterSymbol);
  }
  else if (SDF_isSymbolIterSep(sdfSymbol)) {
    SDF_Symbol sdfIterSymbol = SDF_getSymbolSymbol(sdfSymbol);
    SDF_Symbol sdfIterSep = SDF_getSymbolSep(sdfSymbol);
    PT_Symbol ptIterSymbol = SDFSymbolToPtSymbol(sdfIterSymbol);
    PT_Symbol ptSepSymbol = SDFSymbolToPtSymbol(sdfIterSep);
    result = PT_makeSymbolIterPlusSep(ptIterSymbol, ptSepSymbol);
  }
  else if (SDF_isSymbolIterStarSep(sdfSymbol)) {
    SDF_Symbol sdfIterSymbol = SDF_getSymbolSymbol(sdfSymbol);
    SDF_Symbol sdfIterSep = SDF_getSymbolSep(sdfSymbol);
    PT_Symbol ptIterSymbol = SDFSymbolToPtSymbol(sdfIterSymbol);
    PT_Symbol ptSepSymbol = SDFSymbolToPtSymbol(sdfIterSep);
    result = PT_makeSymbolIterStarSep(ptIterSymbol, ptSepSymbol);
  } 
  else if (SDF_isSymbolCf(sdfSymbol)) {
    SDF_Symbol sdfSym = SDF_getSymbolSymbol(sdfSymbol);
    PT_Symbol ptSym = SDFSymbolToPtSymbol(sdfSym);
    result = PT_makeSymbolCf(ptSym);
  }
  else if (SDF_isSymbolLex(sdfSymbol)) {
    SDF_Symbol sdfSym = SDF_getSymbolSymbol(sdfSymbol);
    PT_Symbol ptSym = SDFSymbolToPtSymbol(sdfSym);
    result = PT_makeSymbolLex(ptSym);
  }
  else if (SDF_isSymbolVarsym(sdfSymbol)) {
    SDF_Symbol sdfSym = SDF_getSymbolSymbol(sdfSymbol);
    PT_Symbol ptSym = SDFSymbolToPtSymbol(sdfSym);
    result = PT_makeSymbolVarSym(ptSym);
  }
  else if (SDF_isSymbolLayout(sdfSymbol)) {
    result = PT_makeSymbolLayout();
  }
  else if (SDF_isSymbolEmpty(sdfSymbol)) {
    result = PT_makeSymbolEmpty();
  }
  else if (SDF_isSymbolSeq(sdfSymbol)) {
    SDF_Symbol sdfHead = SDF_getSymbolHead(sdfSymbol);
    /* tricky casting of SymbolTail to SymbolList */
    SDF_SymbolList sdfTail = (SDF_SymbolList) SDF_getSymbolTail(sdfSymbol);
    SDF_Symbols sdfSymbols = SDF_makeSymbolsDefault(sdfTail);
    PT_Symbol ptHead = SDFSymbolToPtSymbol(sdfHead);
    PT_Symbols ptTail = SDFSymbolsToPtSymbols(sdfSymbols);
    result = PT_makeSymbolSeq(PT_makeSymbolsMany(ptHead, ptTail));
  }
  else if (SDF_isSymbolOpt(sdfSymbol)) {
    SDF_Symbol sdfSym = SDF_getSymbolSymbol(sdfSymbol);
    PT_Symbol ptSym = SDFSymbolToPtSymbol(sdfSym);
    result = PT_makeSymbolOpt(ptSym);
  }
  else if (SDF_isSymbolTuple(sdfSymbol)) {
    SDF_Symbol sdfHead = SDF_getSymbolHead(sdfSymbol);
    SDF_SymbolRest sdfRest = SDF_getSymbolRest(sdfSymbol);
    PT_Symbol ptHead = SDFSymbolToPtSymbol(sdfHead);
    PT_Symbols ptRest = SDFSymbolRestToPtSymbols(sdfRest);
    result = PT_makeSymbolTuple(ptHead, ptRest);
  }
  else if (SDF_isSymbolFunc(sdfSymbol)) {
    SDF_Symbols sdfArguments = SDF_getSymbolArguments(sdfSymbol);
    SDF_Symbol sdfResult = SDF_getSymbolResults(sdfSymbol);
    PT_Symbols ptArguments = SDFSymbolsToPtSymbols(sdfArguments);
    PT_Symbol ptResult = SDFSymbolToPtSymbol(sdfResult);
    result = PT_makeSymbolFunc(ptArguments,ptResult);
  }
  else if (SDF_isSymbolParameterizedSort(sdfSymbol)) {
    SDF_Sort sdfSort = SDF_getSymbolSort(sdfSymbol);
    SDF_SymbolParameters sdfParameters = SDF_getSymbolParameters(sdfSymbol);
    PT_Symbols ptParameters = SDFSymbolParametersToPtSymbols(sdfParameters);
    char *ptSort = PT_yieldTreeToString((PT_Tree) sdfSort, ATfalse);
    result = PT_makeSymbolParameterizedSort(ptSort, ptParameters);
  }
  else if (SDF_isSymbolAlt(sdfSymbol)) {
    SDF_Symbol sdfLeft = SDF_getSymbolLeft(sdfSymbol);
    SDF_Symbol sdfRight = SDF_getSymbolRight(sdfSymbol);
    PT_Symbol ptLeft = SDFSymbolToPtSymbol(sdfLeft);
    PT_Symbol ptRight = SDFSymbolToPtSymbol(sdfRight);
    result = PT_makeSymbolAlt(ptLeft, ptRight);
  }
  else if (SDF_isSymbolCharClass(sdfSymbol)) {
    SDF_CharClass sdfCC = SDF_getSymbolCharClass(sdfSymbol);
    PT_CharRanges ptCR = SDFCharClassToPtCharRanges(sdfCC);
    result = PT_makeSymbolCharClass(ptCR);
  }
  else if (SDF_isSymbolLabel(sdfSymbol)) {
    SDF_Symbol arg = SDF_getSymbolSymbol(sdfSymbol);
    result = SDFSymbolToPtSymbol(arg);
  }
  else if (SDF_isSymbolStrategy(sdfSymbol)) {
    SDF_Symbol sdfLeft = SDF_getSymbolLeft(sdfSymbol);
    SDF_Symbol sdfRight = SDF_getSymbolRight(sdfSymbol);
    PT_Symbol ptLeft = SDFSymbolToPtSymbol(sdfLeft);
    PT_Symbol ptRight = SDFSymbolToPtSymbol(sdfRight);
    result = PT_makeSymbolStrategy(ptLeft, ptRight);
  }
  else if (SDF_isSymbolBracket(sdfSymbol)) {
    SDF_Symbol sym = SDF_getSymbolSymbol(sdfSymbol);
    result = SDFSymbolToPtSymbol(sym);
  }
  else {
    ATerror("SDFSymbolToPtSymbol: unable to convert symbol %t: %s\n", 
	    sdfSymbol, PT_yieldTreeToString((PT_Tree) sdfSymbol, ATfalse));
    result = NULL;
  }
	    
  return result;
}
예제 #2
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;
}