home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnurx.zip / rx / rxdbug.c < prev    next >
C/C++ Source or Header  |  1996-02-08  |  5KB  |  227 lines

  1. /***********************************************************
  2.  
  3. Copyright 1995 by Tom Lord
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the name of the copyright holder not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. Tom Lord DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. EVENT SHALL TOM LORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  19. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  20. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25.  
  26.  
  27.  
  28. #include "rxall.h"
  29. #include "rxgnucomp.h"
  30. #include "rxnfa.h"
  31.  
  32. #include <stdio.h>
  33.  
  34.  
  35. #ifdef __GNUC__
  36. #define AT(X) [X] =
  37. #else
  38. #define AT(X)
  39. #endif
  40.  
  41.  
  42. char *node_type_names[] =
  43. {
  44.   AT(r_cset) "r_cset",
  45.   AT(r_concat) "r_concat",
  46.   AT(r_alternate) "r_alternate",
  47.   AT(r_opt) "r_opt",
  48.   AT(r_star) "r_star",
  49.   AT(r_plus) "r_plus",
  50.   AT(r_interval) "r_interval",
  51.   AT(r_parens) "r_parens",
  52.   AT(r_context) "r_conext"
  53. };
  54.  
  55. void
  56. print_cset (cset_size, cs)
  57.      int cset_size;
  58.      rx_Bitset cs;
  59. {
  60.   int x;
  61.   if (!cs)
  62.       printf ("nil");
  63.   else
  64.     {
  65.       putchar ('[');
  66.       for (x = 0; x < cset_size; ++x)
  67.     if (RX_bitset_member (cs, x))
  68.       {
  69.         if (isprint(x))
  70.           putchar (x);
  71.         else
  72.           printf ("\\0%o ", x);
  73.       }
  74.       putchar (']');
  75.     }
  76. }
  77.  
  78. void
  79. spaces (n)
  80.      int n;
  81. {
  82.   while (n--)
  83.     putchar (' ');
  84. }
  85.  
  86. void
  87. print_rexp (cset_size, indent, rexp)
  88.      int cset_size;
  89.      int indent;
  90.      struct rexp_node * rexp;
  91. {
  92.   spaces (indent);
  93.   if (!rexp)
  94.     printf ("nil\n");
  95.   else
  96.     {
  97.       printf ("Node %d type %d (%s), iv=%d(%c), iv2=%d, len=%d obs=%d cs=",
  98.           rexp->id, rexp->type, node_type_names[rexp->type],
  99.           rexp->params.intval,
  100.           (isprint (rexp->params.intval)
  101.            ? rexp->params.intval
  102.            : ' '),
  103.           rexp->params.intval2,
  104.           rexp->len,
  105.           rexp->observed);
  106.       print_cset (cset_size, rexp->params.cset);
  107.       putchar ('\n');
  108.       if (rexp->params.pair.left || rexp->params.pair.right)
  109.     {
  110.       print_rexp (cset_size, indent + 2, rexp->params.pair.left);
  111.       print_rexp (cset_size, indent + 2, rexp->params.pair.right);
  112.     }
  113.     }
  114. }
  115.  
  116.  
  117.  
  118.  
  119. void
  120. unparse_print_rexp (cset_size, rexp)
  121.      int cset_size;
  122.      struct rexp_node * rexp;
  123. {
  124.   if (!rexp)
  125.     return;
  126.   else
  127.     switch (rexp->type)
  128.       {
  129.       case r_cset:
  130.     if (1 != rx_bitset_population (cset_size, rexp->params.cset))
  131.       print_cset (cset_size, rexp->params.cset);
  132.     else
  133.       {
  134.         int x;
  135.         rx_Bitset cs;
  136.         
  137.         cs = rexp->params.cset;
  138.         for (x = 0; x < cset_size; ++x)
  139.           if (RX_bitset_member (cs, x))
  140.         {
  141.           if (isprint(x))
  142.             putchar (x);
  143.           else
  144.             printf ("\\0%o ", x);
  145.         }
  146.       }
  147.     break;
  148.  
  149.       case r_parens:
  150.     putchar ('(');
  151.     unparse_print_rexp (cset_size, rexp->params.pair.left);
  152.     putchar (')');
  153.     break;
  154.  
  155.       case r_context:
  156.     putchar ('\\');
  157.     putchar (rexp->params.intval);
  158.     break;
  159.  
  160.       case r_concat:
  161.     unparse_print_rexp (cset_size, rexp->params.pair.left);
  162.     unparse_print_rexp (cset_size, rexp->params.pair.right);
  163.     break;
  164.  
  165.       case r_alternate:
  166.     unparse_print_rexp (cset_size, rexp->params.pair.left);
  167.     putchar ('|');
  168.     unparse_print_rexp (cset_size, rexp->params.pair.right);
  169.     break;
  170.  
  171.       case r_opt:
  172.     unparse_print_rexp (cset_size, rexp->params.pair.left);
  173.     putchar ('?');
  174.     break;
  175.  
  176.       case r_star:
  177.     unparse_print_rexp (cset_size, rexp->params.pair.left);
  178.     putchar ('*');
  179.     break;
  180.  
  181.       case r_plus:
  182.     unparse_print_rexp (cset_size, rexp->params.pair.left);
  183.     putchar ('+');
  184.     break;
  185.  
  186.       case r_interval:
  187.     unparse_print_rexp (cset_size, rexp->params.pair.left);
  188.     printf ("{%d,%d}", rexp->params.intval, rexp->params.intval2);
  189.     break;
  190.       }
  191. }
  192.  
  193.  
  194. void
  195. print_nfa_state (rx, state)
  196.      struct rx * rx;
  197.      struct rx_nfa_state * state;
  198. {
  199.   struct rx_nfa_edge * e;
  200.   printf ("state %d, is_final %d, is_start %d\n",
  201.       state->id, state->is_final, state->is_start);
  202.   for (e = state->edges; e; e = e->next)
  203.     {
  204.       printf ("\tEdge %s to %d ",
  205.           (e->type == ne_cset
  206.            ? "cset"
  207.            : (e->type == ne_epsilon
  208.           ? "epsilon"
  209.           : "side effect")),
  210.           e->dest->id);
  211.       if (e->type == ne_cset)
  212.     print_cset (rx->local_cset_size, e->params.cset);
  213.       else
  214.     printf ("%d", (int)e->params.side_effect);
  215.       putchar ('\n');
  216.     }
  217. }
  218.  
  219. void
  220. print_nfa (rx)
  221.      struct rx * rx;
  222. {
  223.   struct rx_nfa_state * state;
  224.   for (state = rx->nfa_states; state; state = state->next)
  225.     print_nfa_state (rx, state);
  226. }
  227.