root/polish.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. polishbuild

   1 /*
   2  *   Compound Text word statistics and search report
   3  *
   4  *   polish.c : reverse polish conversion 
   5  *
   6  *                                           Copyright(c) isao yasuda, 1998 
   7  */
   8 static char *rcs_id = "$Id: polish.c,v 1.2 2007/09/09 11:19:56 isao Exp $";
   9 
  10 #include <stdio.h>
  11 #include "staslova.h"
  12 
  13 unsigned char **
  14 polishbuild(unsigned char **wp)
  15 {
  16   int i, nest;
  17   unsigned char *tempc;                   /* temp pointer                   */
  18   unsigned char *stack[MAXNEST][MAXSTACK];/* stack for operators            */
  19   unsigned char **sp;                     /* current stack pointer          */ 
  20   unsigned char **sp_save[MAXNEST];       /* stack pointer for each nesting */
  21   unsigned char *polish[MAXWORD+1];       /* polish convert pointer array   */
  22   unsigned char **wda = wp;               /* word array pointer             */
  23   sp = stack[0];
  24   i = nest = 0;
  25 
  26   if (wda == NULL)
  27     return NULL;
  28   while (*wda != NULL) {
  29     switch (**wda) {
  30     case EXP : /* unary-expression simply copy */
  31       polish[i++] = *wda;
  32       break;
  33     case '*' : /* `*' stack check & if there is `*' pop it & push */
  34       if (sp != (unsigned char **) &stack[nest]) {
  35         if (*(tempc = *--sp) == '*')
  36           polish[i++] = tempc;
  37         else
  38           *sp++ = tempc;
  39       }
  40       *sp++ = *wda;
  41       break;
  42     case '+' : /* `+'`#' stack pop & copy these oprators */
  43     case '#' :
  44       while (sp != (unsigned char **) &stack[nest])
  45         polish[i++] = *--sp;
  46       *sp++ = *wda;
  47       break;
  48     case '(' : /* `(' current stack point save & point new stack */
  49       sp_save[nest] = sp;
  50       if (nest++ == MAXNEST) {
  51         fprintf(stderr, "Nesting of `( )\' is too many.\n");
  52         return NULL;
  53       }
  54       sp = stack[nest];
  55       break;
  56     case ')' : /* `)' pop all in current stack & recover the old stack */
  57       while (sp != (unsigned char **) &stack[nest])
  58         polish[i++] = *--sp;
  59       if (nest == 0) {
  60         fprintf(stderr, "Expression error : pair`(\'not found.\n");
  61         return NULL;
  62       }
  63       sp = sp_save[--nest];
  64       break;
  65     default :
  66       fprintf(stderr, "Unexpected type of condition.\n");
  67       return NULL;
  68     }
  69     wda++;
  70   }
  71 
  72   if (nest != 0) {
  73     fprintf(stderr, "Expression error : pair`)\'not found.\n");
  74     return NULL;
  75   }
  76   while (sp != (unsigned char **) &stack[nest])
  77     polish[i++] = *--sp;
  78   polish[i] = NULL;
  79   for (i=0; polish[i] != NULL; i++)
  80     *(wp + i) = polish[i];
  81   *(wp + i) = NULL;
  82   return wp;
  83 }
  84 
  85 
  86 
  87 
  88 

/* [previous][next][first][last][top][bottom][index][help] */