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K!$r07$&%Q!<(B $B%5$r@8@.$7$F$/$l$k!#$D$^$j;EMM$rEA$($k$Hyacc$B$NJXMx$J$H$3$m$G$"$k!#(B

$B$G$O$I$&$d$C$F;EMM$rEA$($k$N$@$m$&$+!#(B yacc$B$N>l9g$O(BBNF$B!J(BBackus-Naur Form$B!K(B $B$H$$$&5-=RJ}K!$r;H$&!#$4$/$4$/4JC1$JNc$r8+$F$_$h$&!#(B

if_stmt: IF expr THEN stmt END

$B!V(B:$B!W$N:81&$GJ,$1$F8+$k!#:8B&$N$b$N!"$D$^$j(Bif_stmt$B$O!"1&B&$NJB$S$KEy$7(B $B$$!D!D$H$$$&$N$,!"$3$3$G8@$C$F$$$k$3$H$@!#$D$^$j(B

if_stmt$B$O(BIF expr THEN stmt END$B$H$$$&JB$S$HEy$7$$(B

$B$H8@$C$F$$$k$N$@!#$3$3$G(Bif_stmt$B$d(BIF$B$d(Bexpr$B$d!D!D$OA4$F!V5-9f!W$G$"$k!#(B expr$B$O(Bexpression$B!J<0!K!"(Bstmt$B$O(Bstatement$B!JJ8!K$NN,$N$D$b$j$@!#$3$l$O(B $B$-$C$H(Bif$BJ8$NDj5A$J$s$@$m$&!#(B

$B$3$NDj5A0l$D$N$3$H$r5,B'$H8@$&!#$=$7$F!V(B:$B!W$N:8B&$r!V:8JU!W!"(B $B1&B&$r!V1&JU!W$H8F$V!#

$B$5$F$7$+$7!"$3$l$@$1$G$O$A$g$C$H$b$N$?$j$J$$!#(Bif$BJ8$J$i(Belse$B$bIU$1$i$l$J(B $B$$$H7y$@!#$=$l$b!"$$$D$b(Belse$B$r=q$/$N$OLLE]$J$N$G(Belse$B$,I,MW$J$$$H$-$O(B $B>JN,$G$-$k$h$&$K$7$?$$!#$=$&$$$&$H$-$O

if_stmt: IF expr THEN stmt END
       | IF expr THEN stmt ELSE stmt END

$B!V(B|$B!W$O!V(Bor$B!J$^$?$O!K!W$N0UL#$G$"$k!#(B

if_stmt$B$O!"!V(BIF expr THEN stmt END$B!W$H$$$&JB$S$+(B $B$^$?$O!V(BIF expr THEN stmt ELSE stmt END$B!W$H$$$&JB$S$G$"$k!#(B

$B$H$$$&$3$H$@!#(B

$B$3$3$GCm0U$7$F$[$7$$$N$O!"(B|$B$G6h@Z$k$H$=$l$@$1$G5,B'$O0l$DA}$($F$$(B $B$k$H$$$&$3$H$@!#|$B$G6h@Z$k$H$$$&$N$O:8JU$N5-=R$r>JN,$7$F$$$k$@$1$G(B $B$"$j!"O@M}E*$K$O@hDx$NNc$O

if_stmt: IF expr THEN stmt END
if_stmt: IF expr THEN stmt ELSE stmt END

$B$D$^$j@hDx$NNc$GDj5A$7$?5,B'$OFs$D$G$"$k!#(B

$B$3$l$@$1$G(Bif$BJ8$NDj5A$,407k$7$?$o$1$G$O$J$$!#(Bexpr$B$d(Bstmt$B$J$s$F5-9f(B $B$O%9%-%c%J$+$iAw$i$l$F$/$k$O$:$,$J$$$+$i!"$=$l$,$I$&$$$&JB$S$J$N$+$r$A$c(B $B$s$HDj5A$7$J$$$H$$$1$J$$!#(BRuby$B$K6aIU$1$k$Y$/!"BgC@$K5,B'$rB-$7$F$_$h$&!#(B

stmt   : if_stmt
       | IDENTIFIER '=' expr   /* $BBeF~(B */
       | expr

if_stmt: IF expr THEN stmt END
       | IF expr THEN stmt ELSE stmt END

expr   : IDENTIFIER       /* $BJQ?t;2>H(B */
       | NUMBER           /* $B@0?tDj?t(B */
       | funcall          /* $B4X?t8F$S=P$7!J(BFUNction CALL$B!K(B */

funcall: IDENTIFIER '(' args ')'

args   : expr             /* $B$H$j$"$($:0z?t$O0l$D$@$1(B */

$B?7$7$$MWAG$rFs$D;H$C$?!#$^$:(BC$B8@8l$HF1$87A<0$N%3%a%s%H!"$=$l$+$i(B'='$B$N$h(B $B$&$JJ8;z$NI=8=$@!#$3$N(B'='$B$b$d$O$j5-9f$G$"$k!#(B $B!V(B=$B!W$N$h$&$J5-9f$O?t$J$I$H0c$C$F$?$@0l'='$B$G$$$$$N$@!#$HMh$l$PM=Ls8l$J$I$K$bJ8;zNs$r(B $B$=$N$^$^;H$$$?$/$J$k$,!"(BC$B8@8l$N@)Ls$N$?$a$=$l$O$G$-$J$$!#(B

$B$3$&$$$&$U$&$K5,B'$rA}$d$7$F$$$C$F!"=>$&$Y$-5,B'A4BN$,=q$1$l$PJ8K!$N$G(B $B$-$"$,$j$@!#(Byacc$B$G$O0lHV>e$K=q$$$?5,B'$N:8JU$,!VI=8=$7$?$$J8K!A4BN!W$r(B $BI=$9$3$H$K$J$C$F$$$k$N$G!"$3$NNc$J$i(Bstmt$B$,%W%m%0%i%`A4BN$rI=$9$3$H$K$J(B $B$k!#(B

$B$A$g$C$HCj>]E*$K$9$.$?!#$b$&>/$76qBNE*$K@bL@$7$h$&!#!V(Bstmt$B$,%W%m%0%i%`(B $BA4BN$rI=$9!W$H$$$&$N$O!"(Bstmt$B$H5,B'>e!VEy$7$$!W5-9f$NJB$SA4$F$,J8K!$H$7(B $B$FG'$a$i$l$k!"$H$$$&$3$H$G$"$k!#Nc$($P$^$:!"(Bstmt$B$O(Bstmt$B$HEy$7$$!#$"$?$j(B $B$^$($@!#expr$B$O(Bstmt$B$HEy$7$$!#$3$l$O5,B'$=$N$^$^$@!#$=$7$FNUMBER$B$O(Bstmt$B$HEy$7$$!#$J$<$J$i(BNUMBER$B$O(Bexpr$B$G(Bexpr$B$O(Bstmt$B$@$+$i$@!#(B

$B$b$A$m$s$b$C$HJ#;($J$b$N$bEy$7$$$H8@$($k!#(B

              stmt
               $B"-(B
             if_stmt
               $B"-(B
      IF expr THEN stmt END
          $B"-(B        $B"-(B
IF IDENTIFIER THEN expr END
                    $B"-(B
IF IDENTIFIER THEN NUMBER END

$B$3$3$^$GE83+$7$?$H$3$m$GA4It%9%-%c%J$+$iAw$i$l$F$/$k5-9f$K$J$C$?$h$&$@!#(B $B$D$^$j$3$&$$$&5-9f$NJB$S$O%W%m%0%i%`$H$7$F@5$7$$$H$$$&$3$H$G$"$k!#5U$K(B $B8@$($P!"$3$&$$$&5-9f$NJB$S$,%9%-%c%J$+$iAw$i$l$F$/$k$H%Q!<%5$OE83+$N5U(B $B$N=g=x$GF~NO$rM}2r$7$F$$$1$k!#(B

IF IDENTIFIER THEN NUMBER END
                    $B"-(B
IF IDENTIFIER THEN expr END
          $B"-(B        $B"-(B
      IF expr THEN stmt END
               $B"-(B
             if_stmt
               $B"-(B
              stmt

$B$=$7$F(Bstmt$B$O%W%m%0%i%`A4BN$rI=$95-9f$@!#$@$+$i$3$N5-9fNs$O%W%m%0%i%`$H(B $B$7$F@5$7$$!#$H!"%Q!<%5$O;W$&$3$H$@$m$&!#$=$&$J$k$H%Q!<%9%k!<%A%s(B yyparse()mail protected]$7$?$H$$$&0u$K(B0$B$rJV$7$F=*N;$9$k!#(B

$B$A$J$_$K@lLgMQ8l$@$H%Q!<%[email protected]$N$3$H$r

$B=*C<5-9f$HHs=*C<5-9f(B

$B$5$F!"$I$5$/$5$KJ6$l$F!V%9%-%c%J$+$iMh$k5-9f!W$J$s$F8@MU$r@bL@$J$7$GF~(B $B$l$F$_$?$o$1$@$,!"2~$a$F@bL@$7$h$&!#

$B0l$D$O%9%-%c%J$+$iAw$i$l$F$/$k5-9f!"Nc$($P(BIF$B!"(BTHEN$B!"(BEND$B!"(B'='$B$J$I(B $B$G$"$k!#$3$l$r=*C<5-9f!J(Bterminal symbol$B!K$H8@$&!#@hDx$N$h$&$K$I$s$I$s(B $BE83+$7$?$H$-$K0lHVC<$C$3$KJB$V$+$i$@!#K\>O$G$O=*C<5-9f$O>o$KA4$FBgJ8;z(B $B$G5-=R$9$k!#$?$@$7(B'='$B$N$h$&$K%/%*!<%H$G$/$/$C$F$"$k$b$N$OFCJL$@!#$3$N(B $BN`$N5-9f$ONc30$J$/=*C<5-9f$G$"$k!#(B

$B$b$&0l$D$O%9%-%c%J$+$i$O@dBP$KMh$J$$5-9f!"Nc$($P(Bif_stmt$B!"(Bexpr$B!"(B stmt$B$G$"$k!#$3$&$$$&5-9f$rHs=*C<5-9f!J(Bnonterminal symbol$B!K$H(B $B8@$&!#Hs=*C<5-9f$O%9%-%c%J$+$iMh$J$$$o$1$@$+$i!"%Q!<%5$NCf$@$1$NB8:_$@!#(B $B$=$7$FHs=*C<5-9f$O$$$D$+I,$:5,B'$N:8JU$K8=$l$k!#K\>O$G$OHs=*C<5-9f$OA4(B $B$F>.J8;z$G5-=R$9$k!#(B

$B%F%9%H$NJ}K!(B

$B$3$3$i$GJ8K!%U%!%$%k$r(Byacc$B$G

%token A B C D E
%%
list: A B C
    | de

de  : D E

$B$^$:;H$C$F$$$k=*C<5-9f$r(B%token$B$N$"$H$KA4$FJB$Y$k!#(B $B$?$@$7%/%*!<%H$r;H$C$?5-9f!J(B'='$B$J$I!K$O=q$+$J$/$F$h$$!#(B $B$=$7$F(B%%$B$G6h@Z$C$FJ8K!$r=q$/!#$3$l$@$1$@!#(B

$B$G$O=hM}$7$F$_$h$&!#(B

% yacc first.y
% ls
first.y  y.tab.c
%

UNIX$B%D!<%k$N>o$H$7$F!VD@L[[email protected]$N0U!W$G$"$k!#(B

$B$=$l$+$i!"5,B'!J72!K$N:G8e$K%;%_%3%m%s$,I,MW$J(Byacc$B$bB8:_$9$k!#(B $B$=$N$H$-$O

%token A B C D E
%%
list: A B C
    | de
    ;

de  : D E
    ;

$BI.o$K>JN,$9$k!#(B

$B6u5,B'(B

$B$G$O$b$&>/$7(Byacc$B$NJ8K!5-=R$NDj@P$r$$$/$D$+8+$F$*$/$3$H$K$9$k!#(B $B$^$:$O6u5,B'$+$i>R2p$7$h$&!#(B

void:

$B1&JU$K2?$b$J$$!#$H$$$&$3$H$O!"5-9f(Bvoid$B$O!VL5!W$HEy$7$$$H$$$&$3$H$G$"(B $B$k!#Nc$($Ptarget$B$OA4$/F1$80UL#$K$J$k!#(B

target: A B C

target: A void B void C
void  :

$B$3$s$J$b$N$,2?$NLr$KN)$D$N$@$m$&$+!#$=$l$,$H$F$bLr$KN)$D$N$@!#(B $BNc$($P

if_stmt : IF expr THEN stmts opt_else END

opt_else:
        | ELSE stmts

$B6u5,B'$r;H$&$3$H$G!V(Belse$B@a$O>JN,$5$l$k!JL5!K$+$b$7$l$J$$!W$3$H$r(B $B$&$^$/I=8=$G$-$?!#@hDx$N$h$&$KF1$8$h$&$J5,B'$rFs$DJB$Y$F=q$/$h$j$b(B $B$3$A$i$N$[$&$,4J7i$@$7!"@UG$$,J,;6$7$J$/$F$h$$!#(B

$B:F5"Dj5A(B

$B/$7$o$+$j$K$/$$!#(B

list: ITEM         /*  $B5,B'(B1  */
    | list ITEM    /*  $B5,B'(B2  */

$B$3$l$O0l8D0J>e$N(BITEM$B$rJB$Y$?%j%9%H$rI=8=$7$F$$$k!#(B $B$D$^$j0J2<$N5-9fNs$N$$$:$l$+$r<($9!#(B

ITEM
ITEM ITEM
ITEM ITEM ITEM
ITEM ITEM ITEM ITEM
      $B!'(B

$B$I$&$7$F$+$o$+$k$@$m$&$+!#$^$:5,B'(B1$B$K$h$j(B list$B$O(BITEM$B$HFI$_49$($k$3$H$,$G$-$k!#(B $B$9$k$H:#EY$O5,B'(B2$B$+$i(Blist$B$O(BITEM ITEM$B$@$H8@$($k$h$&$K$J$k!#(B

list: list ITEM
    = ITEM ITEM

$B$3$l$G(BITEM ITEM$B$H$$$&5-9fNs$b(Blist$B$HEy$7$$$H$o$+$k!#(B $B$3$l$r$^$?$5$i$K5,B'(B2$B$N(Blist$B$K$"$F$O$a$l$P(BITEM$B;0$D$b(Blist$B$KEy$7$$$H8@$(!D!D(B $B$3$l$r$I$s$I$sB3$1$l$P(BITEM$B$r$$$/$i$G$bA}$d$7$F$$$1$k!#(B $B?t3XE*5"G

$BITEM$B$r%<%m8D0J>eJB$Y$?%j%9%H$rI=$9!#(B

list:
    | list ITEM

$B$^$:0l9TL\$O!V(Blist$B$O!JL5!K$HEy$7$$!W$H$$$&0UL#$G$"$k!#(B $BL5$H$OB($A(BITEM$B%<%m8D$N%j%9%H$G$"$k!#(B $Blist ITEM$B!W$O(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FJ8K!$N$$$:$l$+$N1&JU$K$"$kJB$S$,%9%?%C%/$N@hC<$KB7$&$H!"(B $B$3$l$r!VM}2r!W$9$k!#!VM}2r!W$9$k$H1&JU$NJB$S$,:8JU$N5-9f$KJQ$o$k!#(B

[ A B C ]
    $B"-(B         $B4T85(B
[ program ]

$B$3$NF0:n$r!V(BA B C$B$r(Bprogram$B$K4T85!J(Breduce$B!K$9$k!W$H8@$&!#(B $B8@MU$O0N$=$&$@$,$h$&$9$k$KGrb$Cf$,B7$&$HBg;085$K$J$k$h$&$J$b$N$@!#(B $B!D!D$=$l$O0c$&$+!#(B

$B$=$7$F(Bprogram$B$O%W%m%0%i%`A4BN$rI=$9$+$i!"%9%?%C%/$K(Bprogram$B$@$1$,$"$k$H(B $B$$$&$3$H$O%W%m%0%i%`A4BN$r8+IU$1$?$N$+$b$7$l$J$$!#$@$+$i$3$3$G$A$g$&$I(B $BF~NO$,=*$o$l$P

$B$b$&>/$7$@$1J#;($JJ8K!$G;n$7$F$_$h$&!#(B

%token IF E S THEN END
%%
program : if

if      : IF expr THEN stmts END

expr    : E

stmts   : S
        | stmts S

$B%9%-%c%J$+$i$NF~NO$O$3$&$@!#(B

IF  E  THEN  S  S  S  END

$B$3$N$H$-$N%;%^%s%F%#%C%/%9%?%C%/$NA+0\$r0J2<$K<($9!#(B

$B%9%?%C%/(B$BF0:n(B
$B:G=i$O6u(B
IFIF$B$r%7%U%H(B
IF EE$B$r%7%U%H(B
IF exprE$B"*(Bexpr$B$G4T85(B
IF expr THENTHEN$B$r%7%U%H(B
IF expr THEN SS$B$r%7%U%H(B
IF expr THEN stmtsS$B"*(Bstmts$B$G4T85(B
IF expr THEN stmts SS$B$r%7%U%H(B
IF expr THEN stmtsstmts S$B"*(Bstmts$B$G4T85(B
IF expr THEN stmts SS$B$r%7%U%H(B
IF expr THEN stmtsstmts S$B"*(Bstmts$B$G4T85(B
IF expr THEN stmts ENDEND$B$r%7%U%H(B
ifIF expr THEN stmts END$B"*(B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$+(B"name"$B$H$+(B "sym"$B$+$b$7$l$J$$!#$J$s$G$b$$$$$N$@!#$=$NCM$O5-9f$H$$$C$7$g$K%;%^%s(B $B%F%#%C%/%9%?%C%/$K@Q$^$l$k!#S$B$rCM$H0l=o$K%7%U%H$7(B $B$?$H$3$m$@!#(B

IF    expr   THEN   stmts   S
$BCM(B    $BCM(B     $BCM(B     $BCM(B     $BCM(B

$B@hDx$N5,B'$K$h$l$P(Bstmts S$B$O(Bstmts$B$K4T85$G$-$k!#$b$7$=$N5,B'$K%"%/%7%g%s(B $B$,=q$$$F$"$l$P$=$l$,

IF    expr   THEN   stmts  S      /* $B%9%?%C%/(B */
$BCM(B1   $BCM(B2    $BCM(B3    $BCM(B4    $BCM(B5
                    $B"-(B     $B"-(B
            stmts:  stmts  S      /* $B5,B'(B */
                    $B"-(B     $B"-(B
                  { $1  +  $2; }  /* $B%"%/%7%g%s(B */

$B$H!"$3$N$h$&$K%"%/%7%g%s$G$O(B$1$B!"(B$2$B!"(B$3$B!D!D(B $B$G5,B'1&JU$KAjEv$9$k5-9f$NCM$r$1$B$H$+(B$2$B$O%9%?%C%/$r;X$9I=8=$K(Byacc$B$,=q$-49$($F$/$l$k$o$1$@!#(B $B$b$C$H$b(BC$B8@8l$J$iK\Ev$O7?$N$3$H$H$+$$$m$$$m$"$k$N$@$1$l$I!"(B $BLLE]$J$N$GEvLL(Bint$B$H2>Dj$7$F$*$3$&!#(B

$B$=$7$F$$$B$HI=8=$5$l!"%"%/%7%g%s$rH4$1$?$H$-$N(B$$$B$NCM$,:8JU$N5-9f$NCM$H$J$k!#(B

IF    expr   THEN   stmts  S      /* $B4T85D>A0$N%9%?%C%/(B */
$BCM(B1   $BCM(B2    $BCM(B3    $BCM(B4    $BCM(B5
                    $B"-(B     $B"-(B
            stmts:  stmts  S      /* $B1&JU$,KvHx$K%^%C%A$7$?5,B'(B */
              $B",(B    $B"-(B     $B"-(B
            { $$  = $1  +  $2; }  /* $B$=$N%"%/%7%g%s(B */


IF    expr   THEN   stmts         /* $B4T858e$N%9%?%C%/(B */
$BCM(B1   $BCM(B2    $BCM(B3    ($BCM(B4+$BCM(B5)

$B:G8e$K

yacc$B$H7?(B

$B$5$FYYSTYPE$B$H$$$&7?$K$J$k!#$-$C$H$3$l$O(B YY Stack TYPE$B$+!"$O$?$^$?(BSemantic value TYPE$B$+!"$I$A$i$+$NN,$K0c$$$J$$!#(B $B$=$7$F(BYYSTYPE$B$OEvA32?$+JL$N7?$N(Btypedef$B$G$"$k!#$=$N7?$H$O!"Dj5AIt$G(B %union$B$H$$$&L?Na$G;XDj$7$?6&MQBN$@!#(B

$B$@$,:#$^$G$O(B%union$B$J$s$F=q$$$F$$$J$+$C$?!#$=$l$J$N$K%(%i!<$K$J$i$J$+$C(B $B$?$N$O$I$&$$$&$o$1$@$m$&!#$=$l$O(Byacc$B$,5$$r$-$+$;$F>!int$B$@!#$H$$$&$3$H(B $B$G(BYYSTYPE$B$N%G%U%)%k%H$O(Bint$B$G$"$k!#(B

yacc$B$NK\$K=P$9Nc$dEEBn%W%m%0%i%`$/$i$$$J$i(Bint$B$N$^$^$G$b9=$o$J$$$N$@$,!"(B $B9=J8LZ$r:n$k$K$O9=B$BN$d%]%$%s%?$d$=$NB>$$$m$$$m$r;H$$$?$$!#$=$3$GNc$((B $B$P%union$B$r;H$&!#(B

%union {
    struct node {
        int type;
        struct node *left;
        struct node *right;
    } *node;
    int num;
    char *str;
}

$B:#$O$OE,Ev$@!#IaDL$N(BC$B$H0c$C$F(B %union$B$N%V%m%C%/$N:G8e$K$O%;%_%3%m%s$,I,MW$J$$$N$GCm0U!#(B

$B$=$l$G!"$3$&=q$/$H(By.tab.c$B$G$O

typedef union {
    struct node {
        int type;
        struct node *left;
        struct node *right;
    } *node;
    int num;
    char *str;
} YYSTYPE;

$B$=$&$9$k$H%;%^%s%F%#%C%/%9%?%C%/$O(B

YYSTYPE yyvs[256];       /* $B%9%?%C%/$N

$B$H$$$&46$8$@$m$&$J!"$HM=A[$,IU$/!#(B $B$=$l$J$i%"%/%7%g%s$K=P$F$/$k5-9f$NCM$b!D!D(B

/* yacc$B=hM}A0$N%"%/%7%g%s(B */
target: A B C { func($1, $2, $3); }

/* $BJQ498e!"(By.tab.c$B$G$NMM;R(B */
{ func(yyvsp[-2], yyvsp[-1], yyvsp[0]); ;

$BEvA3$3$&$J$k!#(B

$B$3$N>l9g$O%G%U%)%k%H$N(Bint$B$r;H$C$?$N$G%9%?%C%/$r;2>H$9$k$@$1$G$h$$$,!"(B YYSTYPE$B$,6&MQBN$N>l9g$OF1;~$K$=$N%a%s%P$b;XDj$7$J$1$l$P%"%/%;%9$G$-(B $B$J$$$O$:$G$"$k!#$=$l$K$O5-9fC10L$G7k$SIU$1$kJ}K!$H$=$NETEY;XDj$9$k(B $BJ}K!$NFsDL$j$,$"$k!#(B

$B$^$:$O0lHLE*$J!"5-9fC10L$G;XDj$9$kJ}K!$+$i!#=*C<5-9f$N>l9g$O(B %token$B$r!"Hs=*C<5-9f$N>l9g$O(B%type$B$r;H$C$F

%token<num> A B C    /* $BA4$F$N(BA B C$B$NCM$O(Bint$B7?(B */
%type<str> target    /* $BA4$F$N(Btarget$B$NCM$O(Bchar*$B7?(B */

$B0lJ}!"Kh2s;XDj$9$k>l9g$O$$B$N$r3d$j9~$^$;$k!#(B

%union { char *str; }
%%
target: { $<str>$ = "$B$h$&$9$k$K%-%c%9%H$_$?$$$J$b$N$5(B"; }

$B$3$A$i$NJ}K!$O$G$-$k$@$1;H$o$J$$$[$&$,$$$$!#(B $B5-9fC10L$G%a%s%P$r7h$a$k$N$,4pK\$@!#(B

$B%Q!<%5$H%9%-%c%J$NO"7k(B

$B$3$l$G%Q!<%5$NCf$NCM$N%"%l%3%l$K$D$$$F$OA4$FOC$7$?!#$"$H$O%9%-%c%J(B $B$H$NO"7k%W%m%H%3%k$rOC$;$P3K$H$J$k;v9`$OA4$F$*$7$^$$$@!#(B

$B$^$:3NG'$9$k$H!"%9%-%c%J$O4X?t(Byylex()$B$G$"$C$?!#(B $B!J=*Cint$B$G!KJV$9!#(Byacc$B$,5-(B $B9f$HF1$8L>A0$GDj?t$r(B#define$B$7$F$/$l$F$$$k$N$G!"5-9f(BNUMBER$B$J$i(BNUMBER$B$H(B $B=q$/$@$1$G$$$$!#$=$7$F$=$NCM$O(Byylval$B$H$$$&%0%m!<%P%kJQ?t$KF~$l$FEO$9!#(B $B$3$N(Byylval$B$b$^$?(BYYSTYPE$B7?$G!"%Q!<%5$N$H$-$HA4$/F1$8$3$H$,8@$($k!#$D$^(B $B$j(B%union$B$GDj5A$9$k$H6&MQBN$K$J$k!#$7$+$7:#2s$O%a%s%P$r>!$r=q$+$J$$$H$@$a$@!#$D$^$jHs>o$K4JC1$J(B $BNc$@$H

static int
yylex()
{
    yylval.str = next_token();
    return STRING;
}

$B$3$3$^$G$N4X78$r?^(B2$B$K$^$H$a$?$N$G0l$D0l$D3NG'$7$F$_$F$[$7$$!#(B yylval$B!"(B$$$B!"(B$1$B!"(B$2$B!D!D$J$I!"%$%s%?!<%U%'%$%9$H$J$kJQ?t$OA4$F(B YYSTYPE$B7?$G$"$k!#(B

(yaccvars)
$B?^(B2: yacc$B4XO"$NJQ?t!&4X?t$N4X78(B

$BKd$a9~$_%"%/%7%g%s(B

$B%"%/%7%g%s$O5,B'$N:G8e$K=q$/$b$N!"$H@bL@$7$?$,!"

target: A B { puts("embedded action"); } C D

$B$3$l$rKd$a9~$_%"%/%7%g%s$H8@$&!#(B $BKd$a9~$_%"%/%7%g%s$O

target: A B dummy C D

dummy :     /* $B6u5,B'(B */
        {
            puts("embedded action");
        }

$B$3$B$H$7$F=P$F$/$k!#(B

$B8=

$B>WFM(B

$B$b$&$3$l$G(Byacc$B$J$s$F62$/$J$$!#(B

$B$H;W$C$?$H$7$?$i$=$l$O$+$J$j4E$$!#$J$<(Byacc$B$,$3$l$[$I$^$G$K(B $B62$l$i$l$k$N$+!"$=$NM}M3$O$3$N8e$K$"$k$N$@!#(B

$B$3$l$^$G$O!V5,B'$N1&JU$,%9%?%C%/@hC<$K%^%C%A$7$?$i!W$H2?5$$J$/=q$$$F(B $B$-$?$,!"

target  : A B C
        | A B C

$BA B C$B$H$$$&5-9fNs$,=P$F$-$?$H$-!"$I$A$i$N5,B'$,%^%C%A$9$k$N$+(B $B$o$+$i$J$/$J$k$O$:$G$"$k!#$3$s$J$b$N$O?M4V$K$@$C$FM}2r$G$-$J$$!#(B $B=>$C$F(Byacc$B$b$o$+$i$J$$!#$3$&$$$&JQ$JJ8K!$rH/8+$9$k$H(Byacc$B$O(B reduce/reduce conflict$B!J4T85!&4T85>WFM!K$,5/$-$?!"$HJ86g$r(B $B8@$C$F$/$k!#J#?t$N5,B'$,F1;~$K4T852DG=$G$"$k$H$$$&0UL#$@!#(B

% yacc rrconf.y
conflicts:  1 reduce/reduce

$B$H$O$$$(IaDL$J$i$P;v8N0J30$K$3$s$J$3$H$O$7$J$$$H;W$&$,!"(B $B

target  : abc
        | A bc

abc     : A B C

bc      :   B C

$B$3$l$J$i$PHf3SE*$"$j$&$k!#FC$K5,B'$r9M$($J$,$i%0%A%c%0%A%c0\(B $BF0$7$F$$$k$HCN$i$:CN$i$:$N$&$A$K$3$s$J5,B'$,$G$-$F$7$^$&$b$N$@!#(B

$B;w$?%Q%?!<%s$G

target  : abc
        | ab C

abc     : A B C

ab      : A B

A B C$B$H$$$&5-9fNs$,8=$l$?>l9g!"(Babc$B0l$D$rA*$V$Y$-$+(Bab$B$H(BC$B$NAH$_(B $B9g$o$;$K$9$Y$-$+$o$+$i$J$$!#$3$&$$$&$H$-(Byacc$B$O(B shift/reduce conflict$B!J%7%U%H!&4T85>WFM!K$,5/$-$?$>!"$HJ86g$r?b$l$k!#(B $B$3$A$i$O!"F1;~$K%7%U%H$G$-$k5,B'$H4T85$G$-$k5,B'$,$"$k$H$$$&0UL#$@!#(B

% yacc srconf.y
conflicts:  1 shift/reduce

shift/reduce conflict$B$NM-L>$JNc$,!V$V$i$5$,$j(Belse$BLdBj!W$G$"$k!#(B $BNc$($P(BC$B8@8l$N(Bif$BJ8$G$3$NLdBj$,5/$3$k!#OC$rC1=c2=$7$F=q$$$F$_$h$&!#(B

stmt     : expr ';'
         | if

expr     : IDENTIFIER

if       : IF '(' expr ')' stmt
         | IF '(' expr ')' stmt  ELSE stmt

$B<0$O(BIDENTIFIER$B!JJQ?t!K$@$1!"(Bif$B$NK\BN$OJ80l$D$@$1$H$7$F5,B'$r:n$C$F$_$?!#(B $B$5$F!"$3$NJ8K!$G

if (cond)
    if (cond)
        true_stmt;
    else
        false_stmt;

$B$3$&=q$$$F$7$^$&$H$J$s$H$J$/0lL\NFA3$K8+$($k$N$@$,!"(B $B

if (cond) {
    if (cond)
        true_stmt;
}
else {
    false_stmt;
}

$B$D$^$j30B&$HFbB&$I$A$i$N(Bif$B$K(Belse$B$rIU$1$k$+$H$$$&LdBj$@!#(B

$B$?$@$7(Bshift/reduce conflict$B$O(Breduce/reduce conflict$B$KHf$Y$l$PHf3SE*L5(B $B32$J>WFM$G$"$k!#$J$<$+$H$$$&$H!"$?$$$F$$$N>l9g$O%7%U%H$rA*$Y$P$&$^$/$$(B $B$/$+$i$@!#%7%U%H$rA*$V$H$$$&$N$O!V$G$-$k$@$16a$$MWAGF1;N$rO"7k$9$k!W$H(B $B$@$$$?$$F15A8l$G$"$j!"?M4V$ND>46$K%^%C%A$7$d$9$$!#else$B$b%7%U%H$7$F$*$1$P$&$^$/$$$/!#$=$&$$$&$o$1$G(Byacc$B$b$=$NN.$l$K=>$$(B shift/reduce conflict$B$,5/$-$?$H$-$K$O%G%U%)%k%H$G%7%U%H$rA*$V$h$&$K$J$C(B $B$F$$$k!#(B

$B@hFI$_(B

$B;n$7$Kyacc$B$K$+$1$F$_$F$[$7$$!#(B

%token A B C
%%
target  : A B C   /* $B5,B'(B1 */
        | A B     /* $B5,B'(B2 */

$B$I$&9M$($F$b>WFM$7$=$&$G$O$J$$$@$m$&$+!#(BA B$B$^$GFI$s$@;~E@$G(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8K!$r;H$C$?@hFI$_$G(B $B$O$J$/

$B1i;;;RM%@h=g0L(B

$B$7$P$i$/Cj>]E*$JOC$,B3$$$?$N$G$3$3$i$G$b$&>/$76qBNE*$JOC$r$9$k!#(B+$B$d(B *$B$J$I$NFs9`1i;;;R!J%$%s%U%#%C%/%97?1i;;;R!K$N5,B'$rDj5A$7$F$_$k$3$H(B $B$K$7$h$&!#$3$l$K$bDj@P$,$"$k$N$G!"$*$H$J$7$/$=$l$K=>$C$F$*$1$P$$$$!#0J(B $B2<$K;MB'1i;;$,;H$($kEEBn$N$h$&$J$b$N$rDj5A$7$?!#(B

expr    : expr '+' expr
        | expr '-' expr
        | expr '*' expr
        | expr '/' expr
        | primary

primary : NUMBER
        | '(' expr ')'

primary$B$O!V9`!W$H$+Lu$5$l$k!#0lHV>.$5$JJ8K!C10L$N$3$H$G$"$k!#(B expr$B$r3g8L$G$/$/$k$H(Bprimary$B$K$J$k$H$$$&$H$3$m$,%]%$%s%H$@!#(B

$B$5$F!"$3$NJ8K!$rE,Ev$K%U%!%$%k$K=q$$$F%3%s%Q%$%k$9$k$H!"$3$&$J$k!#(B

% yacc infix.y
16 shift/reduce conflicts

$B7c$7$/>WFM$7$F$7$^$C$?!#8^J,$P$+$j9M$($F$$$l$P$o$+$k$H;W$&$,!"(B $B$3$N5,B'$G$Ol9g$K:$$k$N$G$"$k!#(B

1 - 1 - 1

$B$3$l$O

(1 - 1) - 1
1 - (1 - 1)

$B?t<0$H$7$F<+A3$J$N$O$b$A$m$sA0yacc$B$,$d$k$N$O$"$/$^$G8+$?$a(B $B$N=hM}$G$"$C$F!"$=$3$K0UL#$OA4$/F~$i$J$$!#(B-$B$H$$$&5-9f$N;}$D0UL#$J$s$F$3$l$C(B $B$]$A$b9MN8$7$F$O$/$l$J$$$N$@!#?M4V$N0U?^$r@5$7$/H?1G$5$;$k$K$O!"$d$j$?(B $B$$$3$H$rCOF;$K;X<($7$F$d$i$J$$$H$$$1$J$$!#(B

$B$G$O$I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%(%i!<(B

$B$b$C$H$b(BPython$B$J$I$G$O;09`$NHf3S$,2DG=$J$N$G$3$N8B$j$G$O$J$$!#(B

$B$=$l$G@hDx$N(B%left$B!&(B%right$B!&(B%nonassoc$B$H$$$&L?Na$O!"(B $BL>A0DL$j$N7k9g@-$r<($9$?$a$K;H$o$l$k!#$=$7$FM%@h=g0L$OJB$Y$k=g$G<($9!#(B $B2<$K$"$k1i;;;R$[$IM%@h=g0L$,9b$$!#F1$8Ns$K$"$l$PF1$8=g0L$G$"$k!#(B

%left  '+' '-'    /* $B:87k9g$GM%@h=g0L(B3 */
%left  '*' '/'    /* $B:87k9g$GM%@h=g0L(B2 */
%right '!'        /* $B1&7k9g$GM%@h=g0L(B1 */

$B8f0U8+!&8f46A[!&8m?#$N;XE&$J$I$O(B $B@DLZJvO:(B <[email protected]> $B$^$G$*4j$$$7$^$9!#(B

$B!X(BRuby$B%=!<%9%3!<%I40A42r@b!Y(B $B$O%$%s%W%l%9%@%$%l%/%H$G8fM=Ls!&8f9XF~$$$?$@$1$^$9(B ($B=q@R>R2p%Z!<%8$XHt$S$^$9(B)$B!#(B

Copyright (c) 2002-2004 Minero Aoki, All rights reserved.