home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 342a.lha / Yacc_v1.0a / test / dc.y < prev    next >
Encoding:
Text File  |  1990-02-05  |  1.4 KB  |  80 lines

  1. /* example from p378 of vol 2, V7 Programmer's Manual */
  2. %{
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. int regs[26];
  7. int base;
  8.  
  9. %}
  10.  
  11. %start list
  12.  
  13. %token DIGIT LETTER
  14. %left '|'
  15. %left '&'
  16. %left '+' '-'
  17. %left '*' '/' '%'
  18. %left UMINUS
  19.  
  20. %%
  21.  
  22. list :   /* empty */
  23.     |  list stat '\n'
  24.     |  list error '\n'
  25.             { yyerrok; }
  26.     ;
  27.  
  28. stat    :   expr
  29.                 { printf("%d\n",$1); }
  30.         |   LETTER '=' expr
  31.                 { regs[$1] = $3; }
  32.         ;
  33.  
  34. expr    :   '(' expr ')'
  35.                 { $$ = $2; }
  36.         |   expr '+' expr
  37.                 { $$ = $1 + $3; }
  38.         |   expr '-' expr
  39.                 { $$ = $1 - $3; }
  40.         |   expr '*' expr
  41.                 { $$ = $1 * $3; }
  42.         |   expr '/' expr
  43.                 { $$ = $1 / $3; }
  44.         |   expr '%' expr
  45.                 { $$ = $1 % $3; }
  46.         |   expr '&' expr
  47.                 { $$ = $1 & $3; }
  48.         |   expr '|' expr
  49.                 { $$ = $1 | $3; }
  50.         |   '-' expr   %prec UMINUS
  51.                 { $$ = - $2; }
  52.         |   LETTER
  53.                 { $$ = regs[$1]; }
  54.         |   number
  55.         ;
  56.  
  57. number:     DIGIT
  58.                 {  $$ = $1; base = ($1==0) ? 8 : 10;  }
  59.         |   number DIGIT
  60.             {   $$ = base * $1 + $2; }
  61.         ;
  62.  
  63. %%
  64.  
  65. yylex()
  66. {
  67.     int c;
  68.     while ((c=getchar()) == ' ');
  69.     if (islower(c)) {
  70.         yylval = c - 'a';
  71.         return (LETTER);
  72.     }
  73.     if (isdigit(c)) {
  74.         yylval = c - '0';
  75.         return(DIGIT);
  76.     }
  77.     return (c);
  78. }
  79.  
  80.