Initial commit
[ccc.git] / parse.y
1 %{
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 #include "ast.h"
7 #define YYSTYPE struct ast *
8
9 #include "y.tab.h"
10
11 int yylex(void);
12
13 void yyerror(struct ast **result, const char *str)
14 {
15 fprintf(stderr, "error: %s\n", str);
16 }
17
18 int yywrap()
19 {
20 return 1;
21 }
22
23 %}
24
25 %token INTEGER PLUS MINUS TIMES DIVIDE BOPEN BCLOSE SEMICOLON
26 %parse-param { struct ast **result }
27
28 %%
29
30 start : exprs { *result = $1; } ;
31
32 exprs
33 : { $$ = NULL; }
34 | exprs expr SEMICOLON { $$ = ast_cons($2, $1); }
35 ;
36
37 expr
38 : expr PLUS fact { $$ = ast_binop($1, plus, $3); }
39 | expr MINUS fact { $$ = ast_binop($1, minus, $3); }
40 | fact
41 ;
42
43 fact
44 : fact TIMES basic { $$ = ast_binop($1, times, $3); }
45 | fact DIVIDE basic { $$ = ast_binop($1, divide, $3); }
46 | basic
47 ;
48
49 basic
50 : INTEGER
51 | BOPEN expr BCLOSE { $$ = $2; }
52 ;