%{ #define YYDEBUG 1 #include "node.h" #include <stdio.h> %} %union { node* nd; double double_value; } %type<nd> primary_expression %type<double_value> DOUBLE_LITERAL %type<double_value> expression term %token DOUBLE_LITERAL %token ADD SUB MUL DIV CR %token COND_BEGIN COND_END %token IF ELSE ENDIF %% line_list : line | line_list line ; line : expression CR { printf(">>%lf\n", $1); } expression : term | expression ADD term { $$ = $1 + $3; } | expression SUB term { $$ = $1 - $3; } | IF COND_BEGIN expression COND_END expression ELSE expression ENDIF { $$ = ($3) ? ($5) : ($7); } term : primary_expression { $$ = ((node_float*)$1)->value; } | term MUL primary_expression { $$ = $1 * ((node_float*)$3)->value; } | term DIV primary_expression { $$ = $1 / ((node_float*)$3)->value; } ; primary_expression : DOUBLE_LITERAL { $$ = node_float_new($1); } ; %% int yyerror(char const *str) { extern char *yytext; fprintf(stderr, "parser error near %s\n", yytext); return 0; } int main(void) { extern int yyparse(void); extern FILE *yyin; yyin = stdin; if(yyparse()) { fprintf(stderr, "Error! Error! Error!\n"); exit(1); } }