home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume15 / reversi2 / part01 / makeedge.y < prev    next >
Text File  |  1993-01-27  |  9KB  |  444 lines

  1. %{
  2. /*
  3.  *    ex:set ts=8 sw=8:
  4.  */
  5. int    score;
  6. extern int    position;
  7. %}
  8. %union {
  9.     struct {
  10.         int    width;
  11.         int    position;
  12.         int    base;
  13.     } field;
  14.     int    ival;
  15. }
  16. %type  <field>    line whites blacks empties oempties
  17. %type  <field>    type1 type2 otype3e type3e type3 otype4 type4 type4.w type4.b
  18. %token <field>    WHITE BLACK EMPTY
  19. %token <ival>    NL
  20. %%
  21. lines    :    lines line
  22.             { printf ("\t%5d,\t/*%s */\n", $2.base, line); }
  23.     |
  24.     ;
  25. line    :    whites type1 NL
  26.             { $$.base = 20 * $1.width + $2.base; }
  27.     |    blacks type2 NL
  28.             { $$.base = -20 * $1.width + $2.base; }
  29.     |    EMPTY type3 NL
  30.             { $$.base = $2.base; }
  31.     |    EMPTY empties otype4 NL
  32.             { $$.base = $3.base; }
  33.     ;
  34. type1    :    blacks whites empties otype4
  35.             {
  36.                 $$.base = $4.base;
  37.                 switch ($2.position) {
  38.                 case 7:
  39.                     $$.base -= ($2.width + $1.width+1) * 15;
  40.                     break;
  41.                 default:
  42.                     if ($3.width == 1)
  43.                         $$.base -=
  44.                         ($1.width + $2.width+1) * 15;
  45.                     else
  46.                         $$.base +=
  47.                         ($2.width - $1.width) * 20;
  48.                     break;
  49.                 }
  50.                 $$.position = $4.position;
  51.                 $$.width = $1.width + $2.width + $3.width;
  52.             }
  53.     |    blacks whites type1
  54.             {
  55.                 $$.base = $3.base;
  56.                 $$.base -= ($1.width - $2.width) * 20;
  57.                 $$.position = $3.position;
  58.                 $$.width = $1.width + $2.width + $3.width;
  59.             }
  60.     |    blacks empties otype4
  61.             {
  62.                 $$.base = ($1.width + 1) * 15 + $3.base;
  63.                 $$.width = $1.width + $2.width + $3.width;
  64.                 $$.position = $3.position;
  65.             }
  66.     |    blacks
  67.             {
  68.                 $$ = $1;
  69.                 $$.base = - $1.width * 20;
  70.             }
  71.     |    empties otype4
  72.             {
  73.                 $$.position = $2.position;
  74.                 $$.width = $1.width+$2.width;
  75.                 $$.base = $2.base;
  76.             }
  77.     |
  78.             { $$.position = position; $$.width = 0; $$.base = 0; }
  79.     ;
  80. type2    :    whites blacks empties otype4
  81.             {
  82.                 $$.base = $4.base;
  83.                 switch ($2.position) {
  84.                 case 7:
  85.                     $$.base += ($2.width + $1.width+1) * 15;
  86.                     break;
  87.                 default:
  88.                     if ($3.width == 1)
  89.                         $$.base +=
  90.                         ($1.width + $2.width+1) * 15;
  91.                     else
  92.                         $$.base -=
  93.                         ($2.width - $1.width) * 20;
  94.                     break;
  95.                 }
  96.                 $$.position = $4.position;
  97.                 $$.width = $1.width + $2.width + $3.width
  98.                      + $4.width;
  99.             }
  100.     |    whites blacks type2
  101.             {
  102.                 $$.base = $3.base;
  103.                 $$.base += ($1.width - $2.width) * 20;
  104.                 $$.position = $3.position;
  105.                 $$.width = $1.width + $2.width + $3.width;
  106.             }
  107.     |    whites empties otype4
  108.             {
  109.                 $$.base = - ($1.width + 1) * 15 + $3.base;
  110.                 $$.width = $1.width + $2.width + $3.width;
  111.                 $$.position = $3.position;
  112.             }
  113.     |    whites
  114.             {
  115.                 $$ = $1;
  116.                 $$.base = $1.width * 20;
  117.             }
  118.     |    empties otype4
  119.             {
  120.                 $$.position = $2.position;
  121.                 $$.width = $1.width+$2.width;
  122.                 $$.base = $2.base;
  123.             }
  124.     |
  125.         { $$.position = 0; $$.width = 0; $$.base = 0; }
  126.     ;
  127. otype4    :    type4
  128.             { $$ = $1; }
  129.     |
  130.             { $$.position = position; $$.width = 0; $$.base = 0; }
  131.     ;
  132. whites    :    whites WHITE
  133.             {
  134.                 $$.position = $2.position;
  135.                 $$.width = $1.width + $2.width;
  136.                 $$.base = $1.base + $2.base;
  137.             }
  138.     |    WHITE
  139.             { $$ = $1; }
  140.     ;
  141. blacks    :    blacks BLACK
  142.             {
  143.                 $$.position = $2.position;
  144.                 $$.width = $1.width + $2.width;
  145.                 $$.base = $1.base + $2.base;
  146.             }
  147.     |    BLACK
  148.             { $$ = $1; }
  149.     ;
  150. empties    :    empties EMPTY
  151.             {
  152.                 $$.position = $2.position;
  153.                 $$.width = $1.width + $2.width;
  154.                 $$.base = $1.base + $2.base;
  155.             }
  156.     |    EMPTY
  157.             { $$ = $1; }
  158.     ;
  159. otype3e    :    type3e
  160.             { $$ = $1; }
  161.     |
  162.             { $$.position = position; $$.width = 0; $$.base = 0; }
  163.     ;
  164. type3    :    whites EMPTY whites oempties otype3e
  165.             {
  166.                 $$.base = -($1.width + $3.width + 2) * 15 +
  167.                     $5.base;
  168.                 $$.width = $1.width + $2.width + $3.width 
  169.                      + $4.width + $5.width;
  170.                 $$.position = $5.position;
  171.             }
  172.     |    blacks EMPTY blacks oempties otype3e
  173.             {
  174.                 $$.base = ($1.width + $3.width + 2) * 15 +
  175.                     $5.base;
  176.                 $$.width = $1.width + $2.width + $3.width 
  177.                      + $4.width + $5.width;
  178.                 $$.position = $5.position;
  179.             }
  180.     |    type3e
  181.     ;
  182. type3e    :    whites blacks type2
  183.             {
  184.                 $$.base = -15 * ($1.width + $2.width + 1);
  185.                 $$.width = $1.width + $2.width + $3.width;
  186.                 $$.position = $3.position;
  187.             }
  188.     |    blacks whites type1
  189.             {
  190.                 $$.base = 15 * ($1.width + $2.width + 1);
  191.                 $$.width = $1.width + $2.width + $3.width;
  192.                 $$.position = $3.position;
  193.             }
  194.     |    whites empties otype3e
  195.             {
  196.                 if ($1.position - $1.width == 1) {
  197.                     switch ($1.width) {
  198.                     case 1:
  199.                         $$.base = -30;
  200.                         break;
  201.                     case 6:
  202.                         $$.base = -20;
  203.                         break;
  204.                     case 2:
  205.                         $$.base = -15;
  206.                         break;
  207.                     case 3:
  208.                         $$.base = -10;
  209.                         break;
  210.                     case 4:
  211.                         $$.base = -5;
  212.                         break;
  213.                     case 5:
  214.                         $$.base = 10;
  215.                         break;
  216.                     default:
  217.                         yyerror ("weirdo");
  218.                         break;
  219.                     }
  220.                 } else {
  221.                     $$.base = $1.base;
  222.                 }
  223.                 $$.base += $3.base;
  224.                 $$.position = $3.position;
  225.                 $$.width = $1.width + $2.width + $3.width;
  226.             }
  227.     |    blacks empties otype3e
  228.             {
  229.                 if ($1.position - $1.width == 1) {
  230.                     switch ($1.width) {
  231.                     case 1:
  232.                         $$.base = 30;
  233.                         break;
  234.                     case 6:
  235.                         $$.base = 20;
  236.                     break;
  237.                     case 2:
  238.                         $$.base = 15;
  239.                         break;
  240.                     case 3:
  241.                         $$.base = 10;
  242.                         break;
  243.                     case 4:
  244.                         $$.base = 5;
  245.                         break;
  246.                     case 5:
  247.                         $$.base = -10;
  248.                         break;
  249.                     default:
  250.                         yyerror ("weirdo");
  251.                         break;
  252.                     }
  253.                 } else {
  254.                     $$.base = $1.base;
  255.                 }
  256.                 $$.base += $3.base;
  257.                 $$.position = $3.position;
  258.                 $$.width = $1.width + $2.width + $3.width;
  259.             }
  260.     |    whites
  261.             {
  262.                 $$.base = 20 * $1.width;
  263.                 $$.position = $1.position;
  264.                 $$.width = $1.width;
  265.             }
  266.     |    blacks
  267.             {
  268.                 $$.base = -20 * $1.width;
  269.                 $$.position = $1.position;
  270.                 $$.width = $1.width;
  271.             }
  272.     ;
  273. type4    :    whites EMPTY whites oempties otype4
  274.         {
  275.             if ($4.position == 8) {
  276.                 $$.base = -($1.width + $3.width + 2) * 15;
  277.             } else {
  278.                 $$.base = 0;
  279.                 if ($1.position - $1.width + 1 == 3)
  280.                     $$.base = $1.width * 15;
  281.                 else
  282.                     $$.base = $1.base;
  283.                 if ($3.position == 6)
  284.                     $$.base += $3.width * 15;
  285.                 else
  286.                     $$.base += $3.base;
  287.                 $$.base += $5.base;
  288.             }
  289.             $$.width = $1.width + $2.width + $3.width
  290.                  + $4.width + $5.width;
  291.             $$.position = $5.position;
  292.         }
  293.     |    whites empties otype4
  294.             {
  295.                 if ($1.position - $1.width + 1 == 3)
  296.                     $$.base = $1.width * 15 + $3.base;
  297.                 else if ($1.position == 6)
  298.                     $$.base = $1.width * 15 + $3.base;
  299.                 else
  300.                     $$.base = $1.base + $3.base;
  301.                 $$.position = $3.position;
  302.                 $$.width = $1.width + $2.width + $3.width;
  303.             }
  304.     |    blacks EMPTY blacks oempties otype4
  305.         {
  306.             if ($4.position == 8) {
  307.                 $$.base = ($1.width + $3.width + 2) * 15;
  308.             } else {
  309.                 $$.base = 0;
  310.                 if ($1.position - $1.width + 1 == 3)
  311.                     $$.base = -$1.width * 15;
  312.                 else
  313.                     $$.base = $1.base;
  314.                 if ($3.position == 6)
  315.                     $$.base +=  -$3.width * 15;
  316.                 else
  317.                     $$.base += $3.base;
  318.                 $$.base += $5.base;
  319.             }
  320.             $$.width = $1.width + $2.width + $3.width
  321.                  + $4.width + $5.width;
  322.             $$.position = $5.position;
  323.         }
  324.     |    blacks empties otype4
  325.             {
  326.                 if ($1.position - $1.width + 1 == 3)
  327.                     $$.base = -$1.width * 15 + $3.base;
  328.                 else if ($1.position == 6)
  329.                     $$.base = -$1.width * 15 + $3.base;
  330.                 else
  331.                     $$.base = $1.base + $3.base;
  332.                 $$.position = $3.position;
  333.                 $$.width = $1.width + $2.width + $3.width;
  334.             }
  335.     |    whites
  336.             {
  337.                 $$.base = 20 * $1.width;
  338.                 $$.position = $1.position;
  339.                 $$.width = $1.width;
  340.             }
  341.     |    blacks
  342.             {
  343.                 $$.base = -20 * $1.width;
  344.                 $$.position = $1.position;
  345.                 $$.width = $1.width;
  346.             }
  347.     |    type4.w
  348.             { $$ = $1; }
  349.     |    type4.b
  350.             { $$ = $1; }
  351.     ;
  352. type4.w    :    whites blacks oempties otype4
  353.         {
  354.             if ($2.position == 8)
  355.                 $$.base = - ($1.width + $2.width + 1) * 15;
  356.             else if ($3.position == 8 && $3.width == 1)
  357.                 $$.base = ($1.width + $2.width + 1) * 10;
  358.             else
  359.                 $$.base = $1.base + $2.base + $4.base;
  360.             $$.position = $4.position;
  361.             $$.width = $1.width + $2.width + $3.width + $4.width;
  362.         }
  363.     ;
  364. type4.b    :    blacks whites oempties otype4
  365.         {
  366.             if ($2.position == 8)
  367.                 $$.base = ($1.width + $2.width + 1) * 15;
  368.             else if ($3.position == 8 && $3.width == 1)
  369.                 $$.base = - ($1.width + $2.width + 1) * 10;
  370.             else
  371.                 $$.base = $1.base + $2.base + $4.base;
  372.             $$.position = $4.position;
  373.             $$.width = $1.width + $2.width + $3.width + $4.width;
  374.         }
  375.     ;
  376. oempties:    empties
  377.             { $$ = $1; }
  378.     |
  379.             { $$.position = position; $$.width = 0; $$.base = 0; }
  380.     ;
  381. %%
  382.  
  383. # include    <stdio.h>
  384.  
  385. main ()
  386. {
  387.     return yyparse ();
  388. }
  389.  
  390. char    line[80];
  391. char    *lp = line;
  392.  
  393. yyerror (s)
  394. char *s;
  395. {
  396.     fprintf (stderr, "%s in %s\n", s, line);
  397. }
  398.  
  399. yywrap ()
  400. {
  401.     return 1;
  402. }
  403.  
  404. int position = 1;
  405.  
  406. int base[] = { 0, 20, -30, 15, -5, -5, 15, -30, 20, 0 };
  407.  
  408. yylex ()
  409. {
  410.     char *gets();
  411.  
  412.     if (*lp == '\0')
  413.         if (fgets (line, 80, stdin) == 0)
  414.             return -1;
  415.         else
  416.             lp = line;
  417.     for (;;) {
  418.         switch (*lp++) {
  419.         case ' ':
  420.         case '\t':
  421.             break;
  422.         case '\n':
  423.             lp[-1] = '\0';
  424.             position = 1;
  425.             return NL;
  426.         case 'O':
  427.             yylval.field.base = -
  428.                 base[yylval.field.position = position++];
  429.             yylval.field.width = 1;
  430.             return BLACK;
  431.         case '*':
  432.             yylval.field.base =
  433.                 base[yylval.field.position = position++];
  434.             yylval.field.width = 1;
  435.             return WHITE;
  436.         case '-':
  437.             yylval.field.base = 0;
  438.             yylval.field.position = position++;
  439.             yylval.field.width = 1;
  440.             return EMPTY;
  441.         }
  442.     }
  443. }
  444.