home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CGAZV5N3.ZIP / TOPDOWN3.C < prev   
C/C++ Source or Header  |  1991-02-27  |  5KB  |  190 lines

  1. /* Listing 6. TOPDOWN3.C - Expression Evaluator */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. #include "lex.h"
  7.  
  8. int  statement                  ( void );
  9. int  expression_and_predicate   ( void );
  10. int  mult_expr_and_predicate    ( void );
  11. int  factor                     ( void );
  12. void error                      (char *fmt,...);
  13.  
  14. #ifdef PTRACE
  15.      static int Rdepth = 0;
  16.      int        rdepth(void)    {return( Rdepth * 8 );}
  17. #    define     trace(name)     printf("%*s%s\n", Rdepth++ * 8, "",  name)
  18. #    define     untrace(name)   (--Rdepth)
  19. #else
  20. #    define trace(name)         /* empty */
  21. #    define untrace(name)       /* empty */
  22. #endif
  23. /*----------------------------------------------------------------------*/
  24. int statement( void )
  25. {
  26.      /* statement->expression SEMI              LP SEMI NUMBER
  27.       */
  28.  
  29.     int value;
  30.  
  31.     trace("statement");
  32.  
  33.     if( match(LP) || match(SEMI) || match(NUMBER) )
  34.     {
  35.         value = expression_and_predicate();
  36.  
  37.         if( match(SEMI) )       advance();
  38.         else            error("Inserting missing semicolon.");
  39.  
  40.         return value;
  41.     }
  42.     else
  43.     {
  44.         error("Illegal expression\n");
  45.         return 0;
  46.     }
  47.  
  48.     untrace("statement");
  49. }
  50. /*----------------------------------------------------------------------*/
  51. int expression_and_predicate( void )
  52. {
  53.      /* expression->mult_expr predicate            LP NUMBER
  54.       * expression->(epsilon)                      RP SEMI
  55.       *
  56.       * Modified to incorporate the predicate productions and also
  57.       * to eliminate the tail recursion in the original predicate();
  58.       */
  59.  
  60.     int value;          /* accumulated value of subexpression */
  61.  
  62.     trace("expression_and_predicate");
  63.  
  64.     if( match(RP) || match(SEMI) )
  65.         ;                                       /* epsilon */
  66.  
  67.     else if( !(match(LP) || match(NUMBER)) )
  68.         error( "expression expected\n" );
  69.     else
  70.     {
  71.         value = mult_expr_and_predicate();
  72.  
  73.          /* predicate->PLUS  mult_expr predicate   PLUS
  74.           * predicate->MINUS mult_expr predicate   MINUS
  75.           * predicate->(epsilon)                   RP SEMI
  76.           */
  77.  
  78.         while( 1 )
  79.         {
  80.             if( match(RP) || match(SEMI) )
  81.                 break;                              /* epsilon */
  82.  
  83.             else if( match(PLUS) ){     advance();
  84.                                         value += mult_expr_and_predicate();
  85.                                   }
  86.             else if( match(MINUS)){     advance();
  87.                                         value -= mult_expr_and_predicate();
  88.                                   }
  89.             else
  90.             {
  91.                 error("operator or statement-terminator expected\n");
  92.                 break;
  93.             }
  94.         }
  95.     }
  96.  
  97.     untrace("expression_and_predicate");
  98.     return value;
  99. }
  100. /*----------------------------------------------------------------------*/
  101. int mult_expr_and_predicate( void )
  102. {
  103.      /* mult_expr->factor mult_predicate        LP NUMBER
  104.       *
  105.       * Modified to eliminate chain to mult_predicate() and also
  106.       * eliminate tail recursion in mult_predicate();
  107.       */
  108.  
  109.     static int  value;          /* value of subexpression           */
  110.  
  111.     trace("mult_expr_and_predicate");
  112.  
  113.     if( !(match(LP) || match(NUMBER)) )
  114.         error( "Expected number identifier or open parenthesis\n" );
  115.     else
  116.     {
  117.         value = factor();
  118.  
  119.          /* mult_predicate->STAR factor mult_predicate  STAR
  120.           * mult_predicate->SLASH factor mult_predicate SLASH
  121.           * mult_predicate->(epsilon)     RP PLUS MINUS SEMI
  122.           */
  123.  
  124.         while( 1 )
  125.         {
  126.             if( match(RP) || match(PLUS) || match(MINUS) || match(SEMI) )
  127.                 break;          /* epsilon */
  128.  
  129.             else if( match(STAR) ) { advance(); value *= factor() ; }
  130.             else if( match(SLASH)) { advance(); value /= factor() ; }
  131.             else
  132.             {
  133.                 error("operator expected\n");
  134.                 break;
  135.             }
  136.         }
  137.     }
  138.  
  139.     return value;
  140.     untrace("mult_expr_and_predicate");
  141. }
  142. /*----------------------------------------------------------------------*/
  143. int factor( void )
  144. {
  145.     /* factor->NUMBER                           NUMBER
  146.      * factor->LP expression RP                 LP
  147.      *
  148.      * Returns value of NUMBER or subexpression
  149.      */
  150.  
  151.     int value;
  152.  
  153.     trace( "factor" );
  154.  
  155.     if( match(NUMBER) )
  156.     {
  157.         value = atoi(yytext);
  158.         advance();
  159.     }
  160.     else if( match(LP) )
  161.     {
  162.         advance();
  163.         value = expression_and_predicate();
  164.  
  165.         if( match(RP) ) advance();
  166.         else            error("Inserting missing right parenthesis.");
  167.     }
  168.     else
  169.         error("Number or parenthesized subexpression expected\n");
  170.  
  171.     untrace( "factor" );
  172.     return value;
  173. }
  174. /*----------------------------------------------------------------------*/
  175. void error( char *fmt, ... )
  176. {
  177.     va_list     args;
  178.     va_start( args, fmt );
  179.     vfprintf( stderr, fmt, args );
  180.     va_end( args );
  181. }
  182. /*----------------------------------------------------------------------*/
  183. int main()
  184. {
  185.  
  186.     printf("Enter Expression: ");
  187.     printf("= %d\n", statement() );
  188.  
  189.     return 0;
  190. }