diff options
Diffstat (limited to 'examples/hello_calc2/parser.y')
-rw-r--r-- | examples/hello_calc2/parser.y | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/examples/hello_calc2/parser.y b/examples/hello_calc2/parser.y new file mode 100644 index 0000000..67bef50 --- /dev/null +++ b/examples/hello_calc2/parser.y @@ -0,0 +1,53 @@ + +%{ +#include <stdio.h> +#include <math.h> +%} + +%union { + double value; + char* name; +} + +%token <value> NUMBER + +%type <value> expr + +%left '+' '-' +%right SQRT +%left '*' '/' +%right '^' +%right UMINUS + +%% +lines: lines expr '\n' { printf("%.10g\n", $2); } + | lines '\n' + | error '\n' { printf("Please re-enter last line: "); + yyerrok; } + | + ; + +expr: expr '+' expr { $$ = $1 + $3; } + | expr '-' expr { $$ = $1 - $3; } + | expr '*' expr { $$ = $1 * $3; } + | expr '/' expr { $$ = $1 / $3; } + | expr '^' expr { $$ = pow($1, $3); } + | '(' expr ')' { $$ = $2; } + | '-' expr %prec UMINUS { $$ = -$2; } + | NUMBER + ; + +%% +#include <ctype.h> +#include <stdio.h> + +int main (int argc, char **argv) +{ + return yyparse (); +} + +int yyerror (char* errstr) +{ + printf ("Error: %s\n", errstr); + return 1; +} |