313 lines
5.7 KiB
Plaintext
313 lines
5.7 KiB
Plaintext
|
/*
|
||
|
ANTLR4 LaTeX Math Grammar
|
||
|
|
||
|
Ported from latex2sympy by @augustt198 https://github.com/augustt198/latex2sympy See license in
|
||
|
LICENSE.txt
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
After changing this file, it is necessary to run `python setup.py antlr` in the root directory of
|
||
|
the repository. This will regenerate the code in `sympy/parsing/latex/_antlr/*.py`.
|
||
|
*/
|
||
|
|
||
|
grammar LaTeX;
|
||
|
|
||
|
options {
|
||
|
language = Python3;
|
||
|
}
|
||
|
|
||
|
WS: [ \t\r\n]+ -> skip;
|
||
|
THINSPACE: ('\\,' | '\\thinspace') -> skip;
|
||
|
MEDSPACE: ('\\:' | '\\medspace') -> skip;
|
||
|
THICKSPACE: ('\\;' | '\\thickspace') -> skip;
|
||
|
QUAD: '\\quad' -> skip;
|
||
|
QQUAD: '\\qquad' -> skip;
|
||
|
NEGTHINSPACE: ('\\!' | '\\negthinspace') -> skip;
|
||
|
NEGMEDSPACE: '\\negmedspace' -> skip;
|
||
|
NEGTHICKSPACE: '\\negthickspace' -> skip;
|
||
|
CMD_LEFT: '\\left' -> skip;
|
||
|
CMD_RIGHT: '\\right' -> skip;
|
||
|
|
||
|
IGNORE:
|
||
|
(
|
||
|
'\\vrule'
|
||
|
| '\\vcenter'
|
||
|
| '\\vbox'
|
||
|
| '\\vskip'
|
||
|
| '\\vspace'
|
||
|
| '\\hfil'
|
||
|
| '\\*'
|
||
|
| '\\-'
|
||
|
| '\\.'
|
||
|
| '\\/'
|
||
|
| '\\"'
|
||
|
| '\\('
|
||
|
| '\\='
|
||
|
) -> skip;
|
||
|
|
||
|
ADD: '+';
|
||
|
SUB: '-';
|
||
|
MUL: '*';
|
||
|
DIV: '/';
|
||
|
|
||
|
L_PAREN: '(';
|
||
|
R_PAREN: ')';
|
||
|
L_BRACE: '{';
|
||
|
R_BRACE: '}';
|
||
|
L_BRACE_LITERAL: '\\{';
|
||
|
R_BRACE_LITERAL: '\\}';
|
||
|
L_BRACKET: '[';
|
||
|
R_BRACKET: ']';
|
||
|
|
||
|
BAR: '|';
|
||
|
|
||
|
R_BAR: '\\right|';
|
||
|
L_BAR: '\\left|';
|
||
|
|
||
|
L_ANGLE: '\\langle';
|
||
|
R_ANGLE: '\\rangle';
|
||
|
FUNC_LIM: '\\lim';
|
||
|
LIM_APPROACH_SYM:
|
||
|
'\\to'
|
||
|
| '\\rightarrow'
|
||
|
| '\\Rightarrow'
|
||
|
| '\\longrightarrow'
|
||
|
| '\\Longrightarrow';
|
||
|
FUNC_INT:
|
||
|
'\\int'
|
||
|
| '\\int\\limits';
|
||
|
FUNC_SUM: '\\sum';
|
||
|
FUNC_PROD: '\\prod';
|
||
|
|
||
|
FUNC_EXP: '\\exp';
|
||
|
FUNC_LOG: '\\log';
|
||
|
FUNC_LG: '\\lg';
|
||
|
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';
|
||
|
|
||
|
L_FLOOR: '\\lfloor';
|
||
|
R_FLOOR: '\\rfloor';
|
||
|
L_CEIL: '\\lceil';
|
||
|
R_CEIL: '\\rceil';
|
||
|
|
||
|
FUNC_SQRT: '\\sqrt';
|
||
|
FUNC_OVERLINE: '\\overline';
|
||
|
|
||
|
CMD_TIMES: '\\times';
|
||
|
CMD_CDOT: '\\cdot';
|
||
|
CMD_DIV: '\\div';
|
||
|
CMD_FRAC:
|
||
|
'\\frac'
|
||
|
| '\\dfrac'
|
||
|
| '\\tfrac';
|
||
|
CMD_BINOM: '\\binom';
|
||
|
CMD_DBINOM: '\\dbinom';
|
||
|
CMD_TBINOM: '\\tbinom';
|
||
|
|
||
|
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];
|
||
|
DIGIT: [0-9];
|
||
|
|
||
|
EQUAL: (('&' WS_CHAR*?)? '=') | ('=' (WS_CHAR*? '&')?);
|
||
|
NEQ: '\\neq';
|
||
|
|
||
|
LT: '<';
|
||
|
LTE: ('\\leq' | '\\le' | LTE_Q | LTE_S);
|
||
|
LTE_Q: '\\leqq';
|
||
|
LTE_S: '\\leqslant';
|
||
|
|
||
|
GT: '>';
|
||
|
GTE: ('\\geq' | '\\ge' | GTE_Q | GTE_S);
|
||
|
GTE_Q: '\\geqq';
|
||
|
GTE_S: '\\geqslant';
|
||
|
|
||
|
BANG: '!';
|
||
|
|
||
|
SINGLE_QUOTES: '\''+;
|
||
|
|
||
|
SYMBOL: '\\' [a-zA-Z]+;
|
||
|
|
||
|
math: relation;
|
||
|
|
||
|
relation:
|
||
|
relation (EQUAL | LT | LTE | GT | GTE | NEQ) 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
|
||
|
| floor
|
||
|
| ceil;
|
||
|
|
||
|
comp_nofunc:
|
||
|
group
|
||
|
| abs_group
|
||
|
| atom
|
||
|
| floor
|
||
|
| ceil;
|
||
|
|
||
|
group:
|
||
|
L_PAREN expr R_PAREN
|
||
|
| L_BRACKET expr R_BRACKET
|
||
|
| L_BRACE expr R_BRACE
|
||
|
| L_BRACE_LITERAL expr R_BRACE_LITERAL;
|
||
|
|
||
|
abs_group: BAR expr BAR;
|
||
|
|
||
|
number: DIGIT+ (',' DIGIT DIGIT DIGIT)* ('.' DIGIT+)?;
|
||
|
|
||
|
atom: (LETTER | SYMBOL) (subexpr? SINGLE_QUOTES? | SINGLE_QUOTES? subexpr?)
|
||
|
| number
|
||
|
| DIFFERENTIAL
|
||
|
| mathit
|
||
|
| frac
|
||
|
| binom
|
||
|
| bra
|
||
|
| ket;
|
||
|
|
||
|
bra: L_ANGLE expr (R_BAR | BAR);
|
||
|
ket: (L_BAR | BAR) expr R_ANGLE;
|
||
|
|
||
|
mathit: CMD_MATHIT L_BRACE mathit_text R_BRACE;
|
||
|
mathit_text: LETTER*;
|
||
|
|
||
|
frac: CMD_FRAC (upperd = DIGIT | L_BRACE upper = expr R_BRACE)
|
||
|
(lowerd = DIGIT | L_BRACE lower = expr R_BRACE);
|
||
|
|
||
|
binom:
|
||
|
(CMD_BINOM | CMD_DBINOM | CMD_TBINOM) L_BRACE n = expr R_BRACE L_BRACE k = expr R_BRACE;
|
||
|
|
||
|
floor: L_FLOOR val = expr R_FLOOR;
|
||
|
ceil: L_CEIL val = expr R_CEIL;
|
||
|
|
||
|
func_normal:
|
||
|
FUNC_EXP
|
||
|
| FUNC_LOG
|
||
|
| FUNC_LG
|
||
|
| 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? SINGLE_QUOTES? | SINGLE_QUOTES? subexpr?) // e.g. f(x), f_1'(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_OVERLINE 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) | ADD | SUB)
|
||
|
)? 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;
|