home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR9 / SYACC131.ZIP / YACC.259 < prev    next >
Text File  |  1993-11-11  |  919b  |  59 lines

  1.  
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <conio.h>
  5. #include <string.h>
  6.  
  7. #define PA_STACK_TYPE int
  8.  
  9. #define LEXER lexer
  10. char* lexer ();
  11.  
  12. %%
  13. // this is the precedences and associativity declarations area
  14. %%
  15. //
  16. // A simple calculator
  17. //
  18. line : expr '\n'
  19.          { printf ( "\nTHE RESULT: %d\n", *$1 ); }
  20.      ;
  21. expr : expr '+' term
  22.          { *$$ = *$1 + *$3; }
  23.      | term
  24.      ;
  25. term : term '*' factor
  26.          { *$$ = *$1 * *$3; }
  27.      | factor
  28.      ;
  29. factor : '(' expr ')'
  30.          { *$$ = *$2; }
  31.      | DIGIT
  32.      ;
  33. %%
  34.  
  35.  
  36. char token[128];
  37.  
  38. char* lexer()
  39.    {
  40.    int c;
  41.    int value;
  42.  
  43.    c = getche ();
  44.  
  45.    /* using Renter key to signify the end of input */
  46.    if ( c == '\r' )
  47.       return "";
  48.    else if ( isdigit (c) )
  49.       {
  50.       *token_value = c - '0';
  51.       return "DIGIT";
  52.       };
  53.    token[0] = c;
  54.    token[1] = '\0';
  55.  
  56.    return token;
  57.    };
  58.  
  59.