-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.y
75 lines (55 loc) · 1.49 KB
/
parser.y
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
64
65
66
67
68
69
70
71
72
73
74
75
%{
#include <stdio.h>
#include <stdlib.h>
#include "num.h"
enum unit g_output_format;
int g_print_unit;
int yylex(void);
int yyerror(char* s);
%}
%union {
int64_t intval;
double floatval;
struct lexunit lexunit;
struct num num;
}
%token<intval> T_INT
%token<floatval> T_FLOAT
%token<lexunit> T_UNIT
%token T_PLUS T_MINUS T_MULTIPLY T_DIVIDE T_LEFT T_RIGHT T_IN
%token T_NEWLINE T_QUIT
%left T_PLUS T_MINUS
%left T_MULTIPLY T_DIVIDE
%type<num> expr unit_number number mul_expr div_expr
%start calc
%%
calc:
| calc line
;
line: T_NEWLINE
| expr T_NEWLINE { num_print(&$1, g_output_format, NULL, g_print_unit); }
| expr T_IN T_UNIT T_NEWLINE {
num_print(&$1, $3.unit, $3.name, g_print_unit);
}
;
unit_number:
T_INT T_UNIT { num_init_int(&$$, $1, $2.unit); }
| T_FLOAT T_UNIT { num_init_float(&$$, $1, $2.unit); }
;
number: T_INT { num_init_int(&$$, $1, UNIT_NONE); }
| T_FLOAT { num_init_float(&$$, $1, UNIT_NONE); }
;
mul_expr: number T_MULTIPLY expr { num_mul(&$$, &$3, &$1); }
| expr T_MULTIPLY number { num_mul(&$$, &$1, &$3); }
;
div_expr: /* number T_DIVIDE expr { num_div(&$$, &$1, &$3); } */
expr T_DIVIDE number { num_div(&$$, &$1, &$3); }
;
expr: unit_number
| expr T_PLUS expr { num_add(&$$, &$1, &$3); }
| expr T_MINUS expr { num_sub(&$$, &$1, &$3); }
| mul_expr
| div_expr
| T_LEFT expr T_RIGHT { num_init(&$$); num_assign(&$$, &$2); }
;
%%