home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / CataniaB / teach-act / esempi / Comp-Sep / getsym.c < prev    next >
C/C++ Source or Header  |  1999-05-13  |  1KB  |  48 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "getsym.h"
  5.  
  6.  
  7. const char tab[NOFSYM] = {'+','-','*','/','%','=','(',')'};
  8.  
  9. symbol getsym()
  10. {
  11.   symbol s;
  12.   static char c = ' ';
  13.   
  14.  
  15.   s.kind = NOTREC; /* pessimistic view! */
  16.  
  17.   while (c != EOF && (c == ' ' || c == '\n' || c == '\t') )
  18.     c = getchar(); /* skip blank, new line and tabbing */
  19.  
  20.   if (c == EOF) /* unexpected end of input */
  21.     {
  22.       printf("\n Error: unexpected end of file \n");
  23.       exit(EXIT_FAILURE);
  24.     }  
  25.   if (c >= '0' && c <= '9') /* number */
  26.     {
  27.       s.kind = NUMBER; 
  28.       s.value = 0;
  29.       do
  30.     s.value = s.value * 10 + c - '0';
  31.       while ((c = getchar()) != EOF && c >= '0' && c <= '9');
  32.     }
  33.   else /* operation or unrecognized symbol */
  34.     {
  35.       symkind i;     
  36.       for (i = PLUS; i <= RPAR; i++)
  37.     if (tab[i] == c)
  38.       s.kind = i;
  39.       if (s.kind == NOTREC)
  40.     {
  41.       printf("\n Error: unrecognized character: %c \n",c);
  42.       exit(EXIT_FAILURE);
  43.     }
  44.       c = getchar(); /* input synchronization */
  45.     }
  46.   return s;
  47. }
  48.