mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-19 01:03:04 +00:00
examples: add a simple infix calculator in C
Currently we have no simple example: rpcalc in reverse Polish, mfcalc has functions, and lexcalc is using lex. * examples/c/calc/Makefile, examples/c/calc/calc.y, * examples/c/calc/calc.test, examples/c/calc/local.mk: New.
This commit is contained in:
93
examples/c/calc/calc.y
Normal file
93
examples/c/calc/calc.y
Normal file
@@ -0,0 +1,93 @@
|
||||
%code top {
|
||||
#include <ctype.h> /* isdigit. */
|
||||
#include <stdio.h> /* For printf, etc. */
|
||||
#include <string.h> /* strcmp. */
|
||||
|
||||
int yylex (void);
|
||||
void yyerror (char const *);
|
||||
}
|
||||
|
||||
%define api.value.type union /* Generate YYSTYPE from these types: */
|
||||
%token <double> NUM "number"
|
||||
%type <double> expr term fact
|
||||
|
||||
/* Generate the parser description file. */
|
||||
%verbose
|
||||
/* Enable run-time traces (yydebug). */
|
||||
%define parse.trace
|
||||
|
||||
/* Formatting semantic values. */
|
||||
%printer { fprintf (yyo, "%g", $$); } <double>;
|
||||
|
||||
%% /* The grammar follows. */
|
||||
input:
|
||||
%empty
|
||||
| input line
|
||||
;
|
||||
|
||||
line:
|
||||
'\n'
|
||||
| expr '\n' { printf ("%.10g\n", $1); }
|
||||
| error '\n' { yyerrok; }
|
||||
;
|
||||
|
||||
expr:
|
||||
expr '+' term { $$ = $1 + $3; }
|
||||
| expr '-' term { $$ = $1 - $3; }
|
||||
| term
|
||||
;
|
||||
|
||||
term:
|
||||
term '*' fact { $$ = $1 * $3; }
|
||||
| term '/' fact { $$ = $1 / $3; }
|
||||
| fact
|
||||
;
|
||||
|
||||
fact:
|
||||
"number"
|
||||
| '(' expr ')' { $$ = $2; }
|
||||
;
|
||||
|
||||
%%
|
||||
|
||||
int
|
||||
yylex (void)
|
||||
{
|
||||
int c;
|
||||
|
||||
/* Ignore white space, get first nonwhite character. */
|
||||
while ((c = getchar ()) == ' ' || c == '\t')
|
||||
continue;
|
||||
|
||||
if (c == EOF)
|
||||
return 0;
|
||||
|
||||
/* Char starts a number => parse the number. */
|
||||
if (c == '.' || isdigit (c))
|
||||
{
|
||||
ungetc (c, stdin);
|
||||
scanf ("%lf", &yylval.NUM);
|
||||
return NUM;
|
||||
}
|
||||
|
||||
/* Any other character is a token by itself. */
|
||||
return c;
|
||||
}
|
||||
|
||||
/* Called by yyparse on error. */
|
||||
void
|
||||
yyerror (char const *s)
|
||||
{
|
||||
fprintf (stderr, "%s\n", s);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char const* argv[])
|
||||
{
|
||||
int i;
|
||||
/* Enable parse traces on option -p. */
|
||||
for (i = 1; i < argc; ++i)
|
||||
if (!strcmp(argv[i], "-p"))
|
||||
yydebug = 1;
|
||||
return yyparse ();
|
||||
}
|
||||
Reference in New Issue
Block a user