home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / doc / yacc / ssa < prev    next >
Encoding:
Text File  |  1975-06-26  |  2.6 KB  |  135 lines

  1. .SH
  2. Appendix A:  A Simple Example
  3. .PP
  4. This example gives the complete Yacc specification for a small desk calculator;
  5. the desk calculator has 26 registers, labeled a through z, and accepts
  6. arithmetic expressions made up of the operators +, \-, *, /,
  7. % (mod operator), & (bitwise and), | (bitwise or), and assignment.
  8. If an expression is an assignment at the top level, the value is not
  9. printed; otherwise it is.
  10. As in C, an integer which begins with 0 (zero) is assumed to be octal;
  11. otherwise, it is assumed to be decimal.
  12. .PP
  13. As an example of a Yacc specification, the desk calculator
  14. does a reasonable job of showing the way that precedences and ambiguities
  15. are used, as well as showing how simple error recovery operates.
  16. The major oversimplifications are that the
  17. lexical analysis phase is much simpler than for most applications, and the
  18. output is produced immediately, line by line.
  19. Note the way that decimal and octal integers are read in by the grammar rules;
  20. frequently, this job is better done by the lexical analyzer.
  21. .LD
  22.  
  23.  
  24. %token DIGIT LETTER    /* these are token names */
  25. %left \'|\'                /* declarations of operator precedences */
  26. %left \'&\'
  27. %left \'+\' \'\-\'
  28. %left \'*\' \'/\' \'%\'
  29. %left UMINUS            /* supplies precedence for unary minus */
  30. %{                    /* declarations used by the actions */
  31.     int base;
  32.     int regs[26];
  33. %}
  34.  
  35. %%   /* beginning of rules section */
  36.  
  37. list :        /* list is the start symbol */
  38.     |    /* empty */
  39.     list stat \'\en\' |
  40.     list error \'\en\' =
  41.     {
  42.         yyerrok ;
  43.     } ;
  44.  
  45. stat :
  46.     expr =
  47.     {
  48.         printf("%d\en", $1) ;
  49.     } |
  50.     LETTER \'=\' expr =
  51.     {
  52.         regs[$1] = $3 ;
  53.     } ;
  54.  
  55. expr :
  56.     \'(\' expr \')\' =
  57.     {
  58.         $$ = $2 ;
  59.     } |
  60.     expr \'+\' expr =
  61.     {
  62.         $$ = $1 + $3 ;
  63.     } |
  64.     expr \'\-\' expr =
  65.     {
  66.         $$ = $1 \- $3 ;
  67.     } |
  68.     expr \'*\' expr =
  69.     {
  70.         $$ = $1 * $3 ;
  71.     } |
  72.     expr \'/\' expr =
  73.     {
  74.         $$ = $1 / $3 ;
  75.     } |
  76.     expr \'%\' expr =
  77.     {
  78.         $$ = $1 % $3 ;
  79.     } |
  80.     expr \'&\' expr
  81.     {
  82.         $$ = $1 & $3 ;
  83.     } |
  84.     expr \'|\' expr
  85.     {
  86.         $$ = $1 | $3 ;
  87.     } |
  88.     \'\-\' expr  %prec UMINUS
  89.     {
  90.         $$ = \- $2 ;
  91.     } |
  92.     LETTER
  93.     {
  94.         $$ = regs[$1] ;
  95.     } |
  96.     number ;
  97.  
  98. number :
  99.     DIGIT =
  100.     {
  101.         $$ = $1 ;
  102.         base = 10 ;
  103.         if( $1 == 0 )
  104.             base = 8 ;
  105.     } |
  106.     number DIGIT =
  107.     {
  108.         $$ = base * $1 + $2 ;
  109.     } ;
  110.  
  111. %%        /* start of programs */
  112.  
  113. yylex( ) /* lexaical analysis routine */
  114. {
  115.     /* returns LETTER for a lower case letter, yylval = 0 through 25 */
  116.     /* return DIGIT for a digit, yylval = 0 through 9 */
  117.     /* all other characters are returned immediately */
  118.  
  119.     int c ;
  120.  
  121.     while( (c=getchar( )) == \' \' )
  122.         ;
  123.     if( c >= \'a\' && c <= \'z\' ) {
  124.         yylval = c \- \'a\' ;
  125.         return( LETTER ) ;
  126.     }
  127.     if( c >= \'0\' && c <= \'9\' ) {
  128.         yylval = c \- \'0\' ;
  129.         return( DIGIT ) ;
  130.     }
  131.     return( c ) ;
  132. }
  133. .DE
  134. .bp
  135.