examples: bistromathic: demonstrate named references

* examples/c/bistromathic/parse.y: here.
This commit is contained in:
Akim Demaille
2020-02-12 00:18:47 +01:00
parent 11e2b755f0
commit e7a202d762
3 changed files with 14 additions and 12 deletions

View File

@@ -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.

View File

@@ -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.

View File

@@ -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; }
; ;