-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path4calc.g
More file actions
30 lines (25 loc) · 706 Bytes
/
Copy path4calc.g
File metadata and controls
30 lines (25 loc) · 706 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
{
#include <stdio.h>
#include <stdlib.h>
typedef struct My_ParseNode {
int value;
} My_ParseNode;
#define D_ParseNode_User My_ParseNode
}
translation_unit :
statement*
;
statement :
expr ';' { printf("%d\n", $0.value); }
;
expr :
integer { $$.value = atoi($n0.start_loc.s); }
| expr '+' expr $right 10 { $$.value = $0.value + $2.value; }
| expr '-' expr $right 10 { $$.value = $0.value - $2.value; }
| expr '*' expr $right 20 { $$.value = $0.value * $2.value; }
| expr '/' expr $right 20 { $$.value = $0.value / $2.value; }
| '(' expr ')' { $$.value = $1.value; }
;
integer :
"-?[0-9]+"
;