home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_40.arc / C-REVIEW.ARC / FXREF.C < prev    next >
Text File  |  1987-10-21  |  6KB  |  218 lines

  1. /*
  2.      Program:  FXREF (File Cross-Reference)
  3.      
  4.      Version:  1.00
  5.      Date:     September 24, 1987
  6.      
  7.      Language: ANSI C
  8.      
  9.      Reads a file from standard input, sorts and organizes each token
  10.      (word) found using a binary tree, keeping track of the number of
  11.      occurences of each token and their location by line and column,
  12.      and then prints a report to stdout.
  13.      
  14.      Copyright 1987 Scott Robert Ladd.
  15.      Released into the public domain.
  16.      Created for Micro Cornucopia C compiler review.
  17. */
  18.  
  19. #include "stdio.h"
  20. #include "string.h"
  21. #include "ctype.h"
  22.  
  23. #ifdef __TURBOC__
  24.      #include "alloc.h"
  25. #else
  26.      #include "stdlib.h"
  27. #endif
  28.  
  29. struct location
  30.      {
  31.      unsigned lineno;
  32.      struct location *next;
  33.      };
  34.  
  35. struct token
  36.      {
  37.      struct token *less, *more;
  38.      char *text;
  39.      struct location *loc, *last;
  40.      };
  41.  
  42. struct token *root;
  43.  
  44. char *errmsg[] = {
  45.                  "Cannot allocated space for token table root",
  46.                  "Cannot allocate space for token text",
  47.                  "Cannot allocate space for location references",
  48.                  "Cannot allocate space for token record"
  49.                  };
  50.  
  51. void parse_tokens(char *, unsigned);
  52. void add_tree(char *, unsigned);
  53. struct token *find_tree(char *);
  54. void show_tree(struct token *);
  55. void error(int);
  56. void exit(int);
  57.  
  58. int main()
  59.      {
  60.      char buf[256];
  61.      unsigned line=0;
  62.  
  63.      if (NULL == (root = (struct token *)malloc(sizeof(struct token))))
  64.           error(0);
  65.  
  66.      root->less = NULL;
  67.      root->more = NULL;
  68.      root->text = NULL;
  69.      root->loc  = NULL;
  70.  
  71.      while (NULL != (gets(buf)))
  72.           {
  73.           ++line;
  74.           printf("%5u: %s\n",line,buf);
  75.           parse_tokens(buf,line);
  76.           }
  77.           
  78.      printf("\x0C\n");
  79.  
  80.      show_tree(root);
  81.  
  82.      return 0;
  83.      }
  84.      
  85. void parse_tokens(buf,line)
  86. char *buf;
  87. unsigned line;
  88.      {
  89.      char tok[256];
  90.      unsigned pos;
  91.      
  92.      while (1)
  93.           {
  94.           while ((!isalpha(*buf)) && (*buf != 0))
  95.                ++buf;
  96.           if (*buf == 0)
  97.                return;
  98.           pos = 0;
  99.           while (isalpha(*buf))
  100.                tok[pos++] = *buf++;
  101.           tok[pos] = 0;
  102.           add_tree(tok,line);
  103.           }
  104.      }
  105.  
  106. void add_tree(tok,line)
  107. char *tok;
  108. unsigned line;
  109.      {
  110.      struct token *temp_tok, *new_tok;
  111.      struct location *temp_loc;
  112.      int comp;
  113.      
  114.      if (root->text == NULL)
  115.           {
  116.           if (NULL == (root->text = (char *)malloc((unsigned)strlen(tok)+1)))
  117.                error(1);
  118.           strcpy(root->text,tok);
  119.           if (NULL == (root->loc = (struct location *)malloc(sizeof(struct location))))
  120.                error(2);
  121.           root->loc->lineno = line;
  122.           root->loc->next = NULL;
  123.           root->last = root->loc;
  124.           return;
  125.           }
  126.           
  127.      temp_tok = find_tree(tok);
  128.      
  129.      if (comp = strcmp(tok,temp_tok->text))
  130.           /* comp is true (non-zero) if they don't match */
  131.           {
  132.           if (NULL == (new_tok = (struct token *)malloc(sizeof(struct token))))
  133.                error(3);
  134.           if (NULL == (new_tok->text = (char *)malloc((unsigned)strlen(tok)+1)))
  135.                error(1);
  136.           new_tok->less = NULL;
  137.           new_tok->more = NULL;
  138.           strcpy(new_tok->text,tok);
  139.           if (NULL == (new_tok->loc = (struct location *)malloc(sizeof(struct location))))
  140.                error(2);
  141.           new_tok->loc->lineno = line;
  142.           new_tok->loc->next = NULL;
  143.           new_tok->last = new_tok->loc;
  144.           if (comp < 0)
  145.                temp_tok->less = new_tok;
  146.             else
  147.                temp_tok->more = new_tok;
  148.           }
  149.        else
  150.           /* if comp is false (0), the tokens match */
  151.           {
  152.           if (NULL == (temp_loc = (struct location *)malloc(sizeof(struct location))))
  153.                error(2);
  154.           temp_loc->lineno = line;
  155.           temp_loc->next = NULL;
  156.           temp_tok->last->next = temp_loc;
  157.           temp_tok->last = temp_loc;
  158.           }
  159.      }
  160.  
  161. struct token *find_tree(tok)
  162. char *tok;
  163.      {
  164.      int comp;
  165.      struct token *node;
  166.      
  167.      node = root;
  168.      
  169.      while (1)
  170.           {
  171.           if (0 == (comp = strcmp(tok,node->text)))
  172.                return node;
  173.           if (comp < 0)
  174.                if (node->less == NULL)
  175.                     return node;
  176.                  else
  177.                     node = node->less;
  178.             else
  179.                if (node->more == NULL)
  180.                     return node;
  181.                  else
  182.                     node = node->more;
  183.           }
  184.      }
  185.  
  186. void show_tree(node)
  187. struct token *node;
  188.      {
  189.      struct location *lloc;
  190.      int pos;
  191.      
  192.      if (NULL == node) return;
  193.      
  194.      show_tree(node->less);
  195.      printf("%-32s: ",node->text);
  196.      pos = 0;
  197.      lloc = node->loc;
  198.      while (lloc != NULL)
  199.           {
  200.           if (++pos == 7)
  201.                {
  202.                pos = 0;
  203.                printf("\n%32s: "," ");
  204.                }
  205.           printf("%5d ",lloc->lineno);
  206.           lloc = lloc->next;
  207.           }
  208.      printf("\n");
  209.      show_tree(node->more);
  210.      }
  211.  
  212. void error(errno)
  213. int errno;
  214.      {
  215.      printf("\nFXREF ERROR: %s\n",errmsg[errno]);
  216.      exit(errno+1);
  217.      }
  218.