コード例 #1
0
ファイル: lpprint.c プロジェクト: 109383670/skynet
static void printinst (const Instruction *op, const Instruction *p) {
  const char *const names[] = {
    "any", "char", "set",
    "testany", "testchar", "testset",
    "span", "behind",
    "ret", "end",
    "choice", "jmp", "call", "open_call",
    "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup",
     "fullcapture", "opencapture", "closecapture", "closeruntime"
  };
  printf("%02ld: %s ", (long)(p - op), names[p->i.code]);
  switch ((Opcode)p->i.code) {
    case IChar: {
      printf("'%c'", p->i.aux);
      break;
    }
    case ITestChar: {
      printf("'%c'", p->i.aux); printjmp(op, p);
      break;
    }
    case IFullCapture: {
      printcapkind(getkind(p));
      printf(" (size = %d)  (idx = %d)", getoff(p), p->i.key);
      break;
    }
    case IOpenCapture: {
      printcapkind(getkind(p));
      printf(" (idx = %d)", p->i.key);
      break;
    }
    case ISet: {
      printcharset((p+1)->buff);
      break;
    }
    case ITestSet: {
      printcharset((p+2)->buff); printjmp(op, p);
      break;
    }
    case ISpan: {
      printcharset((p+1)->buff);
      break;
    }
    case IOpenCall: {
      printf("-> %d", (p + 1)->offset);
      break;
    }
    case IBehind: {
      printf("%d", p->i.aux);
      break;
    }
    case IJmp: case ICall: case ICommit: case IChoice:
    case IPartialCommit: case IBackCommit: case ITestAny: {
      printjmp(op, p);
      break;
    }
    default: break;
  }
  printf("\n");
}
コード例 #2
0
ファイル: lpprint.c プロジェクト: 109383670/skynet
void printtree (TTree *tree, int ident) {
  int i;
  for (i = 0; i < ident; i++) printf(" ");
  printf("%s", tagnames[tree->tag]);
  switch (tree->tag) {
    case TChar: {
      int c = tree->u.n;
      if (isprint(c))
        printf(" '%c'\n", c);
      else
        printf(" (%02X)\n", c);
      break;
    }
    case TSet: {
      printcharset(treebuffer(tree));
      printf("\n");
      break;
    }
    case TOpenCall: case TCall: {
      printf(" key: %d\n", tree->key);
      break;
    }
    case TBehind: {
      printf(" %d\n", tree->u.n);
        printtree(sib1(tree), ident + 2);
      break;
    }
    case TCapture: {
      printf(" cap: %d  key: %d  n: %d\n", tree->cap, tree->key, tree->u.n);
      printtree(sib1(tree), ident + 2);
      break;
    }
    case TRule: {
      printf(" n: %d  key: %d\n", tree->cap, tree->key);
      printtree(sib1(tree), ident + 2);
      break;  /* do not print next rule as a sibling */
    }
    case TGrammar: {
      TTree *rule = sib1(tree);
      printf(" %d\n", tree->u.n);  /* number of rules */
      for (i = 0; i < tree->u.n; i++) {
        printtree(rule, ident + 2);
        rule = sib2(rule);
      }
      assert(rule->tag == TTrue);  /* sentinel */
      break;
    }
    default: {
      int sibs = numsiblings[tree->tag];
      printf("\n");
      if (sibs >= 1) {
        printtree(sib1(tree), ident + 2);
        if (sibs >= 2)
          printtree(sib2(tree), ident + 2);
      }
      break;
    }
  }
}
void permute(char * charset, int length)
{
  int changing_length = length;//create a new variable to be changed after every recursively calling

  printf("permute ");
  printcharset(charset, length);//first print out the character set needed to be permutated
  permutation(charset, changing_length, length);//call the recursive function
}
void permutation(char* charset, int length, int real_length)
{
  int lcv;//loop control variable to check all the elements in the array

  if(length == 1)//base case: if we reach the point that length equaling to 1 means all the characters in the array have been swapped into the right positions
  {
    printcharset(charset, real_length);//print the character array
  }
  else//Inductive case
  {
    for(lcv = 1; lcv <= length; lcv ++)//for a character array containing n characters, print all the n! possible permutations
    {
      swap(charset, length, lcv, real_length);//first swap the array to get a new permutation
      permutation(charset, length - 1, real_length);//recursively call itself
      swap(charset, length, lcv, real_length);//swap back to get ready for the new swap after
    }
  }
}