-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
132 lines (93 loc) · 2.64 KB
/
parser.mly
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* ************************** PARSER ********************************* */
/* ************************************************************************** */
%{
open Lexing;;
open Ast;;
let fst l = if List.length l = 0 then [] else [List.hd l] ;;
let snd l = if List.length l = 0 then [] else [List.hd l; List.hd (List.tl l)] ;;
%}
%token <string> CONST
%token <string> ID
%token PLUS MINUS TIMES DIV
%token LPAR RPAR
%token SQRT POW
%token EQUAL
%token REV IREV NMAREV NMAIREV
%token SEMI
%token EOF
%token EQN INIT PARAMS UNSCALABLE
%start file
%type <Ast.file> file
%%
file:
| EQN; p = reaction_list; PARAMS; r = rate_def; INIT; i = init_cond; UNSCALABLE; s = us_list; EOF
{List.append (p:file) (List.append (r:file) (List.append (i:file) (s:file)))}
;
us_list:
{[]}
| i=us; l = us_list {i :: l}
;
us:
| i = ID {([(i,"1")],6,[],[])}
;
init_cond:
{[]}
| i = init; l = init_cond {i :: l}
;
init:
| i = ID; c = CONST {([(i,c)], 0, [], [])}
| i = ID; EQUAL; c = CONST {([(i,c)], 0, [], [])}
;
rate_def:
{[]}
| i = rate_val; l = rate_def {i :: l}
;
rate_val:
| i = ID; EQUAL; c = CONST {([], 1, [], [(i, c)])}
| i = ID; EQUAL; s = expr {([], 1, [], [(i, s)])}
| i = ID; EQUAL; j = ID {([], 1, [], [(i, j)])}
;
reaction_list:
{[]}
| i = reaction; l = reaction_list; {i :: l}
;
reaction:
| r = rp_list; REV; p = rp_list; SEMI; rt = rate_list {(r,2,p,(snd rt))}
| r = rp_list; IREV; p = rp_list; SEMI; rt = rate_list {(r, 3, p, (fst rt))}
| r = rp_list; NMAREV; p = rp_list; SEMI; rt = rate_list {(r, 4,p,(snd rt))}
| r = rp_list; NMAIREV; p = rp_list; SEMI; rt = rate_list {(r, 5, p, (fst rt))}
;
rp_list:
| s = species {[s]}
| s = species; PLUS; l = rp_list {s :: l}
;
species:
| c = CONST; i = ID {(i, c)}
| c = CONST; TIMES; i=ID {(i,c) }
| c = ID; TIMES; i=ID {(i,c)}
| i = ID {(i, "1")}
;
rate_list:
{[]}
| r = rate {[r]}
| r = rate; SEMI; l = rate_list {r :: l}
;
rate:
| i = ID {(i, "nondef")}
| i = ID; EQUAL; c = CONST {(i, c)}
| i = ID; EQUAL; s = expr {(i, s)}
| i = ID; EQUAL; j = ID {(i, j)}
;
expr:
| i = ID { i }
| i = CONST { i }
| LPAR; e = expr; RPAR { "("^e^")" }
| e1= expr; PLUS; e2 = expr { e1 ^ "+"^e2}
| e1= expr; MINUS; e2 = expr { e1 ^ "-"^e2}
| e1= expr; TIMES; e2 = expr { e1 ^ "*"^e2}
| e1= expr; DIV; e2 = expr { e1 ^ "/"^e2}
| e1= expr; POW; e2 = expr { e1 ^ "^"^e2}
| SQRT; e = expr { "square " ^ e }
| MINUS; e = expr { "-" ^ e }
;