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

  1. .SH
  2. Appendix D: An Example of Bundling
  3. .PP
  4. The following program is an example of the technique of
  5. bundling; this example is discussed in Section 7.
  6. .LP
  7.  
  8.  
  9. /* warnings:
  10. .IP 1.
  11. This works on Unix; the handling of functions with a
  12. variable number of arguments is different on different systems.
  13. .IP 2.
  14. A number of checks for array bounds have been left out
  15. to avoid obscuring the basic ideas, but should
  16. be there in a practical program.
  17. .PP
  18.   */
  19. .LD
  20.  
  21.  
  22. %token NAME
  23.  
  24. %right \'=\'
  25. %left \'+\' \'\-\'
  26. %left \'*\' \'/\'
  27.  
  28. %%
  29.  
  30. lines :
  31.     = /* empty */
  32.     {
  33.         bclear( ) ;
  34.     } |
  35.     lines expr \'\en\' =
  36.     {
  37.         bprint( $2 ) ;
  38.         printf( "\en" ) ;
  39.         bclear( ) ;
  40.     } |
  41.     lines error \'\en\' =
  42.     {
  43.         bclear( ) ;
  44.         yyerrok;
  45.     } ;
  46.  
  47. expr :
  48.     expr \'+\' expr =
  49.     {
  50.         $$ = bundle( "add(", $1, ",", $3, ")" );
  51.     } |
  52.     expr \'\-\' expr =
  53.     {
  54.         $$ = bundle( "sub(", $1, ",", $3, ")" );
  55.     } |
  56.     expr \'*\' expr =
  57.     {
  58.         $$ = bundle( "mul(", $1, ",", $3, ")" );
  59.     } |
  60.     expr \'/\' expr =
  61.     {
  62.         $$ = bundle( "div(", $1, ",", $3, ")" );
  63.     } |
  64.     \'(\' expr \')\' =
  65.     {
  66.         $$ = $2;
  67.     } |
  68.     NAME \'=\' expr =
  69.         $$ = bundle( "assign(", $1, ",", $3, ")" );
  70.     } |
  71.     NAME ;
  72.  
  73. %%
  74.  
  75. #define    nsize 200
  76. char    names[nsize], *nptr { names };
  77.  
  78. #define    bsize 500
  79. int    bspace[bsize], *bptr { bspace };
  80.  
  81. yylex( )
  82. {
  83.     int c;
  84.  
  85.     c = getchar( );
  86.     while( c == \' \' )
  87.         c = getchar( );
  88.     if( c>=\'a\' && c<=\'z\' ) {
  89.         yylval = nptr;
  90.         for( ; c>=\'a\' && c<=\'z\'; c=getchar( ) )
  91.             *nptr++ = c;
  92.         ungetc( c );
  93.         *nptr++ = \'\e0\';
  94.         return( NAME );
  95.     }
  96.     return( c );
  97. }
  98.  
  99. bclear( )
  100. {
  101.  
  102.  
  103.     nptr = names;
  104.     bptr = bspace;
  105. }
  106.  
  107. bundle( a1,a2,a3,a4,a5 )
  108. {
  109.     int i, j, *p, *obp;
  110.  
  111.     p = &a1;
  112.     i = nargs( );
  113.     obp = bptr;
  114.  
  115.     for( j=0; j<i; ++j )
  116.         *bptr++ = *p++;
  117.     *bptr++ = 0;
  118.     return( obp );
  119. }
  120.  
  121. bprint( p )
  122. int *p;
  123. {
  124.  
  125.     if( p>=bspace && p< &bspace[bsize] ) /* bundle */
  126.         while( *p != 0 )
  127.             bprint( *p++ );
  128.     else printf( "%s",  p );
  129. }
  130. .DE
  131.