home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / misc / 2997 < prev    next >
Encoding:
Text File  |  1992-09-10  |  2.2 KB  |  117 lines

  1. Newsgroups: comp.lang.misc
  2. Path: sparky!uunet!mcsun!Germany.EU.net!ifado!wb
  3. From: wb@arb-phys.uni-dortmund.de ()
  4. Subject: Dynamic conflict solving in Yacc/Bison
  5. Message-ID: <1992Sep10.113422.25625@arb-phys.uni-dortmund.de>
  6. Sender: wb@arb-phys.uni-dortmund.de (Wilhelm B. Kloke)
  7. Organization: Institut f. Arbeitsphysiologie a.d. Uni Dortmund
  8. Date: Thu, 10 Sep 92 11:34:22 GMT
  9. Lines: 106
  10.  
  11. In an attempt to parse a language which needs dynamic priority
  12. assignment (Algol68), I am trying the following hack. Does anybody know
  13. a less risky method (the Bison manual warns about using $0)?
  14.  
  15. %{
  16. #include    <stdio.h>
  17. #include    <ctype.h>
  18. %}
  19.  
  20. %start list
  21. %token    DIGIT OP REDUCE
  22. %right OP
  23.  
  24. %%
  25.  
  26. list:    /* empty */ 
  27.     |    list stat '\n'
  28.     |    list error '\n'
  29.         {    yyerrok;    }
  30.     ;
  31.  
  32. stat:    { $$='?'; } expr
  33.         {    printf( "%d\n", $2 );    }
  34.     ;
  35.  
  36. expr:    expr OP expr
  37.         {    $$ = binop($2, $1, $3);
  38.             if( yychar==REDUCE ) yychar=OP;
  39.             check( prio($0) );
  40.         }
  41.     |    number
  42.         {
  43.             check( prio($0) );
  44.             $$ = $1;
  45.         }
  46.     ;
  47.  
  48. number:    DIGIT
  49.         {    $$ = $1;    }
  50.     | number DIGIT
  51.         {    $$ = 10 * $1 + $2;    }
  52.     ;
  53.  
  54. %%
  55.  
  56. check(p)  int p; {
  57.     if( yychar == OP ) {
  58.         if( prio( yylval) <= p ) yychar = REDUCE;
  59.     }
  60. }
  61.  
  62. prio(o) int o; {
  63.     switch(o) {
  64.     case '?' :  return 0;
  65.     case '+' : return 5;
  66.     case '-' : return 5;
  67.     case '*' : return 6;
  68.     case '&' : return 4;
  69.     case '%' : return 6;
  70.     case '/' : return 6;
  71.     case '|' : return 4;
  72.     default: printf( "error\n" );
  73.     }
  74. }
  75. binop(o,x,y) int o,x,y; {
  76.     switch(o) {
  77.     case '+' : return x+y;
  78.     case '-' : return x-y;
  79.     case '*' : return x*y;
  80.     case '&' : return x&y;
  81.     case '%' : return x%y;
  82.     case '/' : return x/y;
  83.     case '|' : return x|y;
  84.     default: printf( "error\n" );
  85.     }
  86. }
  87.  
  88. yylex() {
  89.     int    c;
  90.     while( (c=getchar()) == ' ' )
  91.         ;
  92.     yylval = c;
  93.     if( isdigit(c) ) {
  94.         yylval = c - '0';
  95.         return( DIGIT );
  96.     }
  97.     else if( c == '+' )    return( OP );
  98.     else if( c == '-' )    return( OP );
  99.     else if( c == '*' )    return( OP );
  100.     else if( c == '&' )    return( OP );
  101.     else if( c == '%' )    return( OP );
  102.     else if( c == '|' )    return( OP );
  103.     else if( c == '/' )    return( OP );
  104.     return( c );
  105. }
  106.  
  107. main()
  108. {    return( yyparse() );    }
  109.  
  110. yyerror(s) char *s; {
  111.     fprintf( stderr, "%s\n", s ); }
  112. -- 
  113. -- 
  114. Dipl.-Math. Wilhelm Bernhard Kloke,
  115. Institut fuer Arbeitsphysiologie an der Universitaet Dortmund
  116. Ardeystrasse 67, D-4600 Dortmund 1, Tel. 0231-1084-257
  117.