forked from augustt198/latex2sympy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PS.g4
219 lines (168 loc) · 3.91 KB
/
PS.g4
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
grammar PS;
options {
language=Python2;
}
WS: [ \t\r\n]+ -> skip;
ADD: '+';
SUB: '-';
MUL: '*';
DIV: '/';
L_PAREN: '(';
R_PAREN: ')';
L_BRACE: '{';
R_BRACE: '}';
L_BRACKET: '[';
R_BRACKET: ']';
BAR: '|';
FUNC_LIM: '\\lim';
LIM_APPROACH_SYM: '\\to' | '\\rightarrow' | '\\Rightarrow' | '\\longrightarrow' | '\\Longrightarrow';
FUNC_INT: '\\int';
FUNC_SUM: '\\sum';
FUNC_PROD: '\\prod';
FUNC_LOG: '\\log';
FUNC_LN: '\\ln';
FUNC_SIN: '\\sin';
FUNC_COS: '\\cos';
FUNC_TAN: '\\tan';
FUNC_CSC: '\\csc';
FUNC_SEC: '\\sec';
FUNC_COT: '\\cot';
FUNC_ARCSIN: '\\arcsin';
FUNC_ARCCOS: '\\arccos';
FUNC_ARCTAN: '\\arctan';
FUNC_ARCCSC: '\\arccsc';
FUNC_ARCSEC: '\\arcsec';
FUNC_ARCCOT: '\\arccot';
FUNC_SINH: '\\sinh';
FUNC_COSH: '\\cosh';
FUNC_TANH: '\\tanh';
FUNC_ARSINH: '\\arsinh';
FUNC_ARCOSH: '\\arcosh';
FUNC_ARTANH: '\\artanh';
FUNC_SQRT: '\\sqrt';
CMD_TIMES: '\\times';
CMD_CDOT: '\\cdot';
CMD_DIV: '\\div';
CMD_FRAC: '\\frac';
CMD_MATHIT: '\\mathit';
UNDERSCORE: '_';
CARET: '^';
COLON: ':';
fragment WS_CHAR: [ \t\r\n];
DIFFERENTIAL: 'd' WS_CHAR*? ([a-zA-Z] | '\\' [a-zA-Z]+);
LETTER: [a-zA-Z];
fragment DIGIT: [0-9];
NUMBER:
DIGIT+ (',' DIGIT DIGIT DIGIT)*
| DIGIT* (',' DIGIT DIGIT DIGIT)* '.' DIGIT+;
EQUAL: '=';
LT: '<';
LTE: '\\leq';
GT: '>';
GTE: '\\geq';
BANG: '!';
SYMBOL: '\\' [a-zA-Z]+;
math: relation;
relation:
relation (EQUAL | LT | LTE | GT | GTE) relation
| expr;
equality:
expr EQUAL expr;
expr: additive;
additive:
additive (ADD | SUB) additive
| mp;
// mult part
mp:
mp (MUL | CMD_TIMES | CMD_CDOT | DIV | CMD_DIV | COLON) mp
| unary;
mp_nofunc:
mp_nofunc (MUL | CMD_TIMES | CMD_CDOT | DIV | CMD_DIV | COLON) mp_nofunc
| unary_nofunc;
unary:
(ADD | SUB) unary
| postfix+;
unary_nofunc:
(ADD | SUB) unary_nofunc
| postfix postfix_nofunc*;
postfix: exp postfix_op*;
postfix_nofunc: exp_nofunc postfix_op*;
postfix_op: BANG | eval_at;
eval_at:
BAR (eval_at_sup | eval_at_sub | eval_at_sup eval_at_sub);
eval_at_sub:
UNDERSCORE L_BRACE
(expr | equality)
R_BRACE;
eval_at_sup:
CARET L_BRACE
(expr | equality)
R_BRACE;
exp:
exp CARET (atom | L_BRACE expr R_BRACE) subexpr?
| comp;
exp_nofunc:
exp_nofunc CARET (atom | L_BRACE expr R_BRACE) subexpr?
| comp_nofunc;
comp:
group
| abs_group
| func
| atom
| frac;
comp_nofunc:
group
| abs_group
| atom
| frac;
group:
L_PAREN expr R_PAREN
| L_BRACKET expr R_BRACKET
| L_BRACE expr R_BRACE;
abs_group: BAR expr BAR;
atom: (LETTER | SYMBOL) subexpr? | NUMBER | DIFFERENTIAL | mathit;
mathit: CMD_MATHIT L_BRACE mathit_text R_BRACE;
mathit_text: LETTER*;
frac:
CMD_FRAC L_BRACE
upper=expr
R_BRACE L_BRACE
lower=expr
R_BRACE;
func_normal:
FUNC_LOG | FUNC_LN
| FUNC_SIN | FUNC_COS | FUNC_TAN
| FUNC_CSC | FUNC_SEC | FUNC_COT
| FUNC_ARCSIN | FUNC_ARCCOS | FUNC_ARCTAN
| FUNC_ARCCSC | FUNC_ARCSEC | FUNC_ARCCOT
| FUNC_SINH | FUNC_COSH | FUNC_TANH
| FUNC_ARSINH | FUNC_ARCOSH | FUNC_ARTANH;
func:
func_normal
(subexpr? supexpr? | supexpr? subexpr?)
(L_PAREN func_arg R_PAREN | func_arg_noparens)
| (LETTER | SYMBOL) subexpr? // e.g. f(x)
L_PAREN args R_PAREN
| FUNC_INT
(subexpr supexpr | supexpr subexpr)?
(additive? DIFFERENTIAL | frac | additive)
| FUNC_SQRT
(L_BRACKET root=expr R_BRACKET)?
L_BRACE base=expr R_BRACE
| (FUNC_SUM | FUNC_PROD)
(subeq supexpr | supexpr subeq)
mp
| FUNC_LIM limit_sub mp;
args: (expr ',' args) | expr;
limit_sub:
UNDERSCORE L_BRACE
(LETTER | SYMBOL)
LIM_APPROACH_SYM
expr (CARET L_BRACE (ADD | SUB) R_BRACE)?
R_BRACE;
func_arg: expr | (expr ',' func_arg);
func_arg_noparens: mp_nofunc;
subexpr: UNDERSCORE (atom | L_BRACE expr R_BRACE);
supexpr: CARET (atom | L_BRACE expr R_BRACE);
subeq: UNDERSCORE L_BRACE equality R_BRACE;
supeq: UNDERSCORE L_BRACE equality R_BRACE;