mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
examples: bistromathic: demonstrate named references
* examples/c/bistromathic/parse.y: here.
This commit is contained in:
@@ -54,6 +54,7 @@ This example demonstrates the best practices when using Bison.
|
|||||||
- It uses a custom syntax error with location tracking, lookahead correction
|
- It uses a custom syntax error with location tracking, lookahead correction
|
||||||
and token internationalization.
|
and token internationalization.
|
||||||
- It enables debug trace support with formatting of semantic values.
|
- 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.
|
It also uses Flex to generate the scanner.
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ This example demonstrates the best practices when using Bison.
|
|||||||
- It uses a custom syntax error with location tracking, lookahead correction
|
- It uses a custom syntax error with location tracking, lookahead correction
|
||||||
and token internationalization.
|
and token internationalization.
|
||||||
- It enables debug trace support with formatting of semantic values.
|
- 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.
|
It also uses Flex to generate the scanner.
|
||||||
|
|
||||||
|
|||||||
@@ -107,30 +107,30 @@ input:
|
|||||||
|
|
||||||
line:
|
line:
|
||||||
EOL
|
EOL
|
||||||
| exp EOL { printf ("%.10g\n", $1); }
|
| exp EOL { printf ("%.10g\n", $exp); }
|
||||||
| error EOL { yyerrok; }
|
| error EOL { yyerrok; }
|
||||||
;
|
;
|
||||||
|
|
||||||
exp:
|
exp:
|
||||||
NUM
|
NUM
|
||||||
| VAR { $$ = $1->value.var; }
|
| VAR { $$ = $VAR->value.var; }
|
||||||
| VAR "=" exp { $$ = $3; $1->value.var = $3; }
|
| VAR "=" exp { $$ = $3; $VAR->value.var = $3; }
|
||||||
| FUN "(" exp ")" { $$ = $1->value.fun ($3); }
|
| FUN "(" exp ")" { $$ = $FUN->value.fun ($3); }
|
||||||
| exp "+" exp { $$ = $1 + $3; }
|
| exp[l] "+" exp[r] { $$ = $l + $r; }
|
||||||
| exp "-" exp { $$ = $1 - $3; }
|
| exp[l] "-" exp[r] { $$ = $l - $r; }
|
||||||
| exp "*" exp { $$ = $1 * $3; }
|
| exp[l] "*" exp[r] { $$ = $l * $r; }
|
||||||
| exp "/" exp
|
| exp[l] "/" exp[r]
|
||||||
{
|
{
|
||||||
if ($3 == 0)
|
if ($r == 0)
|
||||||
{
|
{
|
||||||
yyerror (&@$, "division by zero");
|
yyerror (&@$, "division by zero");
|
||||||
YYERROR;
|
YYERROR;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
$$ = $1 / $3;
|
$$ = $l / $r;
|
||||||
}
|
}
|
||||||
| "-" exp %prec NEG { $$ = -$2; }
|
| "-" exp %prec NEG { $$ = -$2; }
|
||||||
| exp "^" exp { $$ = pow ($1, $3); }
|
| exp[l] "^" exp[r] { $$ = pow ($l, $r); }
|
||||||
| "(" exp ")" { $$ = $2; }
|
| "(" exp ")" { $$ = $2; }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user