123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- /*
- 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 = Python2;
- }
- 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';
- FUNC_SUM: '\\sum';
- FUNC_PROD: '\\prod';
- FUNC_EXP: '\\exp';
- 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';
- 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';
- 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];
- fragment DIGIT: [0-9];
- NUMBER:
- DIGIT+ (',' DIGIT DIGIT DIGIT)*
- | DIGIT* (',' DIGIT DIGIT DIGIT)* '.' DIGIT+;
- 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: '!';
- 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
- | frac
- | binom
- | floor
- | ceil;
- comp_nofunc:
- group
- | abs_group
- | atom
- | frac
- | binom
- | 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;
- atom: (LETTER | SYMBOL) subexpr?
- | NUMBER
- | DIFFERENTIAL
- | mathit
- | 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 L_BRACE upper = expr R_BRACE 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_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_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
- )? 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;
|