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 / laboratorio / lab-26-4-99 / parser+getsym.c < prev    next >
C/C++ Source or Header  |  1999-05-25  |  2KB  |  136 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define NOFSYM 8
  4.  
  5. typedef enum{NOTREC = -2,PLUS = 0, MINUS, TIMES, DIV, MOD, EQUAL, 
  6.          LPAR, RPAR, NUMBER} symkind;
  7.          
  8. typedef struct {symkind kind; int value;} symbol;
  9.  
  10. const char tab[NOFSYM] = {'+','-','*','/','%','=','(',')'};        
  11.  
  12.  
  13. void parse(void);
  14. void parseE(void);
  15. void parseT(void);
  16. void parseF(void);
  17.  
  18. symbol getsym();
  19.  
  20. static symbol sym;
  21.  
  22.  
  23. int main()
  24. {
  25.   parse();
  26. }
  27.  
  28. void parse(void)
  29. {
  30.   sym = getsym();
  31.   parseE();
  32.   if (sym.kind == EQUAL)
  33.     {
  34.       printf("\n Successful parsing \n");
  35.       exit(EXIT_SUCCESS);
  36.     }
  37.   else
  38.     {
  39.       printf("\n Error: expecting equal symbol \n");
  40.       exit(EXIT_FAILURE);
  41.     }
  42. }
  43.  
  44. void parseE(void)
  45. {
  46.  
  47.   parseT();
  48.   if (sym.kind == PLUS || sym.kind == MINUS)
  49.     {
  50.       sym = getsym();
  51.       parseE();
  52.     }
  53. }
  54.  
  55. void parseT(void)
  56. {
  57.  
  58.   parseF();
  59.   if (sym.kind == TIMES || sym.kind == DIV)
  60.     {
  61.       sym = getsym();
  62.       parseT();
  63.     }
  64. }
  65.  
  66. void parseF(void)
  67. {
  68.  
  69.   switch(sym.kind)
  70.     {
  71.     case LPAR:
  72.       sym = getsym();
  73.       parseE();
  74.       if (sym.kind != RPAR)
  75.     {
  76.       printf("\n Error: expecting right parenthesis \n");
  77.       exit(EXIT_FAILURE);
  78.     }
  79.       sym = getsym();      
  80.       break;
  81.     case MINUS:
  82.       sym = getsym();
  83.       parseF();
  84.       break;
  85.     case NUMBER:
  86.       sym = getsym();
  87.       break;
  88.     default:
  89.       printf("\n Error: expecting left parenthesis or minus symbol or a number \n");
  90.       exit(EXIT_FAILURE);
  91.       break;
  92.     }
  93. }
  94.  
  95.  
  96. symbol getsym()
  97. {
  98.   symbol s;
  99.   static char c = ' ';
  100.  
  101.   s.kind = NOTREC; /* pessimistic view! */
  102.  
  103.   while (c != EOF && (c == ' ' || c == '\n' || c == '\t') )
  104.     c = getchar(); /* skip blank, new line and tabbing */
  105.  
  106.   if (c == EOF) /* unexpected end of input */
  107.     {
  108.       printf("\n Error: unexpected end of file \n");
  109.       exit(EXIT_FAILURE);
  110.     }  
  111.   if (c >= '0' && c <= '9') /* number */
  112.     {
  113.       s.kind = NUMBER; 
  114.       s.value = 0;
  115.       do
  116.     s.value = s.value * 10 + c - '0';
  117.       while ((c = getchar()) != EOF && c >= '0' && c <= '9');
  118.     }
  119.   else /* operation or unrecognized symbol */
  120.     {
  121.       symKind i;
  122.  
  123.       for (i = PLUS; i <= RPAR; i++)
  124.     if (tab[i] == c)
  125.       s.kind = i;
  126.       if (s.kind == NOTREC)
  127.     {
  128.       printf("\n Error: unrecognized character: %c \n",c);
  129.       exit(EXIT_FAILURE);
  130.     }
  131.       c = getchar(); /* input synchronization */
  132.     }
  133.   return s;
  134. }
  135.  
  136.