2
0
mirror of https://gitlab.com/ita1024/waf.git synced 2024-11-15 22:48:47 +01:00
waf/demos/bisonflex/calc.y
2011-09-10 11:13:51 +02:00

34 lines
750 B
Plaintext

%{
#define YYSTYPE double
#include <stdio.h>
// yylex() is generated by flex
int yylex(void);
// we have to define yyerror()
int yyerror (char const *);
%}
%token NUMBER
%left '+' '-'
%left '*' '/'
%right '('
%%
stmtlist: statement '\n' stmtlist {
printf("done with statement equal to [%g]\n", $1);
} | // EMPTY RULE i.e. stmtlist -> nil
{ printf("DONE\n") ;};
statement: expression { printf("VALUE=%g\n",$1); };
expression: expression '+' expression { $$ = $1 + $3; } |
expression '-' expression { $$ = $1 - $3; } |
expression '*' expression { $$ = $1 * $3; } |
expression '/' expression {
if($3 !=0) { $$ = $1 / $3; } else
{ printf("div by zero\n"); $$=0;} } |
'(' expression ')' { $$ = $2; } |
NUMBER { $$ = $1; } ;
%%