diff --git a/examples/c/README.md b/examples/c/README.md index 26110cf1..893dfe2f 100644 --- a/examples/c/README.md +++ b/examples/c/README.md @@ -54,6 +54,7 @@ This example demonstrates the best practices when using Bison. - It uses a custom syntax error with location tracking, lookahead correction and token internationalization. - It enables debug trace support with formatting of semantic values. +- It uses named references instead of the traditional $1, $2, etc. It also uses Flex to generate the scanner. diff --git a/examples/c/bistromathic/README.md b/examples/c/bistromathic/README.md index 764866e3..25411867 100644 --- a/examples/c/bistromathic/README.md +++ b/examples/c/bistromathic/README.md @@ -4,6 +4,7 @@ This example demonstrates the best practices when using Bison. - It uses a custom syntax error with location tracking, lookahead correction and token internationalization. - It enables debug trace support with formatting of semantic values. +- It uses named references instead of the traditional $1, $2, etc. It also uses Flex to generate the scanner. diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y index 1fa9c081..373bc074 100644 --- a/examples/c/bistromathic/parse.y +++ b/examples/c/bistromathic/parse.y @@ -107,30 +107,30 @@ input: line: EOL -| exp EOL { printf ("%.10g\n", $1); } -| error EOL { yyerrok; } +| exp EOL { printf ("%.10g\n", $exp); } +| error EOL { yyerrok; } ; exp: NUM -| VAR { $$ = $1->value.var; } -| VAR "=" exp { $$ = $3; $1->value.var = $3; } -| FUN "(" exp ")" { $$ = $1->value.fun ($3); } -| exp "+" exp { $$ = $1 + $3; } -| exp "-" exp { $$ = $1 - $3; } -| exp "*" exp { $$ = $1 * $3; } -| exp "/" exp +| VAR { $$ = $VAR->value.var; } +| VAR "=" exp { $$ = $3; $VAR->value.var = $3; } +| FUN "(" exp ")" { $$ = $FUN->value.fun ($3); } +| exp[l] "+" exp[r] { $$ = $l + $r; } +| exp[l] "-" exp[r] { $$ = $l - $r; } +| exp[l] "*" exp[r] { $$ = $l * $r; } +| exp[l] "/" exp[r] { - if ($3 == 0) + if ($r == 0) { yyerror (&@$, "division by zero"); YYERROR; } else - $$ = $1 / $3; + $$ = $l / $r; } | "-" exp %prec NEG { $$ = -$2; } -| exp "^" exp { $$ = pow ($1, $3); } +| exp[l] "^" exp[r] { $$ = pow ($l, $r); } | "(" exp ")" { $$ = $2; } ;