7 #define YYSTYPE struct ast *
14 void yyerror(struct ast **result, const char *str)
17 fprintf(stderr, "error: %s\n", str);
27 %define parse.error verbose
28 %token INTEGER PLUS MINUS TIMES DIVIDE BOPEN BCLOSE SEMICOLON POWER CONS MODULO
29 %token BINOR BINAND INVERSE VAR ASSIGN IDENT COMMA COPEN CCLOSE IF ELSE WHILE
32 %parse-param { struct ast **result }
36 %nonassoc EQ NEQ LEQ LE GEQ GE
39 %left TIMES DIVIDE MODULO
44 start : decls { *result = ast_list($1); } ;
48 | decls vardecl SEMICOLON { $$ = ast_cons($2, $1); }
49 | decls fundecl { $$ = ast_cons($2, $1); }
53 : VAR IDENT ASSIGN expr { $$ = ast_vardecl($2, $4); }
57 : IDENT BOPEN args BCLOSE COPEN body CCLOSE
58 { $$ = ast_fundecl($1, ast_list($3), ast_list($6)); }
66 : nargs COMMA IDENT { $$ = ast_cons($3, $1); }
67 | IDENT { $$ = ast_cons($1, NULL); }
71 | body vardecl SEMICOLON { $$ = ast_cons($2, $1); }
72 | body stmt { $$ = ast_cons($2, $1); }
76 : IF BOPEN expr BCLOSE COPEN body CCLOSE ELSE COPEN body CCLOSE
77 { $$ = ast_if($3, ast_list($6), ast_list($10)); }
78 | WHILE BOPEN expr BCLOSE COPEN body CCLOSE
79 { $$ = ast_while($3, ast_list($6)); }
80 | expr SEMICOLON { $$ = ast_stmt_expr($1); }
84 : expr BINOR expr { $$ = ast_binop($1, binor, $3); }
85 | expr BINAND expr { $$ = ast_binop($1, binand, $3); }
86 | expr EQ expr { $$ = ast_binop($1, eq, $3); }
87 | expr NEQ expr { $$ = ast_binop($1, neq, $3); }
88 | expr LEQ expr { $$ = ast_binop($1, leq, $3); }
89 | expr LE expr { $$ = ast_binop($1, le, $3); }
90 | expr GEQ expr { $$ = ast_binop($1, geq, $3); }
91 | expr GE expr { $$ = ast_binop($1, ge, $3); }
92 | expr CONS expr { $$ = ast_binop($1, cons, $3); }
93 | expr PLUS expr { $$ = ast_binop($1, plus, $3); }
94 | expr MINUS expr { $$ = ast_binop($1, minus, $3); }
95 | expr TIMES expr { $$ = ast_binop($1, times, $3); }
96 | expr DIVIDE expr { $$ = ast_binop($1, divide, $3); }
97 | expr MODULO expr { $$ = ast_binop($1, modulo, $3); }
98 | expr POWER expr { $$ = ast_binop($1, power, $3); }
99 | MINUS expr { $$ = ast_unop(negate, $2); }
100 | INVERSE expr { $$ = ast_unop(inverse, $2); }
101 | BOPEN expr BCLOSE { $$ = $2; }