-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday18.y
More file actions
63 lines (47 loc) · 1003 Bytes
/
day18.y
File metadata and controls
63 lines (47 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
%{
#include <stdio.h>
#include <stdlib.h>
extern int yylex();
extern int yyparse();
extern FILE* yyin;
void yyerror(long long*, const char* s);
%}
%parse-param { long long* result }
%union {
long long val;
}
%token<val> T_NUM
%token T_PLUS T_MULTIPLY T_LEFT T_RIGHT T_NEWLINE
// part 1
%left T_MULTIPLY T_PLUS
// part 2
// %left T_MULTIPLY
// %left T_PLUS
%type<val> expression
%start calculation
%%
calculation:
| calculation line
;
line: T_NEWLINE
| expression T_NEWLINE { *result += $1; }
;
expression: T_LEFT expression T_RIGHT { $$ = $2; }
| expression T_PLUS expression { $$ = $1 + $3; }
| expression T_MULTIPLY expression { $$ = $1 * $3; }
| T_NUM { $$ = $1; }
;
%%
int main() {
yyin = stdin;
do {
long long result = 0;
yyparse(&result);
printf("%lld\n", result);
} while(!feof(yyin));
return 0;
}
void yyerror(long long* answer, const char* s) {
fprintf(stderr, "Parse error: %s\n", s);
exit(1);
}