Spracherkennung für: .lark vermutete Sprache: SPS {SPS[36] ABAP[28] C[12]} [Methode: maximale Elemente, drei Dimensionen]
%ignore /[ \t\n\r]+/
%ignore "\\," | "\\thinspace" | "\\:" | "\\medspace" | "\\;" | "\\thickspace"
%ignore "\\quad" | "\\qquad"
%ignore "\\!" | "\\negthinspace" | "\\negmedspace" | "\\negthickspace"
%ignore "\\vrule" | "\\vcenter" | "\\vbox" | "\\vskip" | "\\vspace" | "\\hfill"
%ignore "\\*" | "\\-" | "\\." | "\\/" | "\\(" | "\\="
%ignore "\\left" | "\\right"
%ignore "\\limits" | "\\nolimits"
%ignore "\\displaystyle"
///////////////////// tokens ///////////////////////
// basic binary operators
ADD: "+"
SUB: "-"
MUL: "*"
DIV: "/"
// tokens with distinct left and right symbols
L_BRACE: "{"
R_BRACE: "}"
L_BRACE_LITERAL: "\\{"
R_BRACE_LITERAL: "\\}"
L_BRACKET: "["
R_BRACKET: "]"
L_CEIL: "\\lceil"
R_CEIL: "\\rceil"
L_FLOOR: "\\lfloor"
R_FLOOR: "\\rfloor"
L_PAREN: "("
R_PAREN: ")"
// limit, integral, sum, and product symbols
FUNC_LIM: "\\lim"
LIM_APPROACH_SYM: "\\to" | "\\rightarrow" | "\\Rightarrow" | "\\longrightarrow" | "\\Longrightarrow"
FUNC_INT: "\\int" | "\\intop"
FUNC_SUM: "\\sum"
FUNC_PROD: "\\prod"
// common functions
FUNC_EXP: "\\exp"
FUNC_LOG: "\\log"
FUNC_LN: "\\ln"
FUNC_LG: "\\lg"
FUNC_MIN: "\\min"
FUNC_MAX: "\\max"
// \operatorname{name}: a builtin function when the name is one, an undefined function otherwise
FUNC_OPERATORNAME: /\\operatorname\s*\{\s*[a-zA-Z]+\s*\}/
// trigonometric functions
FUNC_SIN: "\\sin"
FUNC_COS: "\\cos"
FUNC_TAN: "\\tan"
FUNC_CSC: "\\csc"
FUNC_SEC: "\\sec"
FUNC_COT: "\\cot"
// inverse trigonometric functions
FUNC_ARCSIN: "\\arcsin"
FUNC_ARCCOS: "\\arccos"
FUNC_ARCTAN: "\\arctan"
FUNC_ARCCSC: "\\arccsc"
FUNC_ARCSEC: "\\arcsec"
FUNC_ARCCOT: "\\arccot"
// hyperbolic trigonometric functions
FUNC_SINH: "\\sinh"
FUNC_COSH: "\\cosh"
FUNC_TANH: "\\tanh"
FUNC_COTH: "\\coth"
FUNC_SECH: "\\sech"
FUNC_CSCH: "\\csch"
FUNC_ARSINH: "\\arsinh" | "\\arcsinh"
FUNC_ARCOSH: "\\arcosh" | "\\arccosh"
FUNC_ARTANH: "\\artanh" | "\\arctanh"
// Commands sharing a prefix, like \tan and \tanh, are alternatives of a single terminal,
// so that the lexer matches the longest one: \tanh is never read as \tan followed by h.
FUNC_TRIG: 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_COTH | FUNC_SECH | FUNC_CSCH
| FUNC_ARSINH | FUNC_ARCOSH | FUNC_ARTANH
FUNC_SQRT: "\\sqrt"
// miscellaneous symbols
CMD_TIMES: "\\times"
CMD_CDOT: "\\cdot"
CMD_DIV: "\\div"
CMD_FRAC: "\\frac" | "\\dfrac" | "\\tfrac" | "\\nicefrac"
CMD_BINOM: "\\binom" | "\\dbinom" | "\\tbinom"
CMD_OVERLINE: "\\overline"
CMD_LANGLE: "\\langle"
CMD_RANGLE: "\\rangle"
CMD_MATHIT: "\\mathit"
CMD_MATHBB: "\\mathbb"
CMD_IN: "\\in"
CMD_NOTIN: "\\notin"
CMD_PI: "\\pi"
EULER_NUMBER: "\\mathrm{e}"
CMD_INFTY: "\\infty"
CMD_CIRC: "\\circ"
BANG: "!"
BAR: "|"
CARET: "^"
COLON: ":"
UNDERSCORE: "_"
// relational symbols
EQUAL: "="
NOT_EQUAL: /\\neq(?![a-zA-Z])/ | /\\ne(?![a-zA-Z])/
LT: "<"
// "\le" requires a negative lookahead to avoid matching as a prefix of "\left". Without the negative lookahead,
// LTE was matching the "\le" part of "\left", leaving behind "ft" as garbage tokens.
LTE: "\\leq" | /\\le(?![a-zA-Z])/ | "\\leqslant"
GT: ">"
GTE: "\\geq" | "\\ge" | "\\geqslant"
DIV_SYMBOL: CMD_DIV | DIV
MUL_SYMBOL: MUL | CMD_TIMES | CMD_CDOT
%import .greek_symbols.GREEK_SYMBOL
UPRIGHT_DIFFERENTIAL_SYMBOL: "\\text{d}" | "\\mathrm{d}"
DIFFERENTIAL_SYMBOL: "d" | UPRIGHT_DIFFERENTIAL_SYMBOL
// disallow "d" as a variable name because we want to parse "d" as a differential symbol.
SYMBOL: /[a-zA-Z]'*/
// \pi on its own is the constant, it is a symbol with primes or with a subscript
GREEK_SYMBOL_WITH_PRIMES: /(?!\\pi(?!'))/ GREEK_SYMBOL "'"*
SUBSCRIPTED_GREEK_SYMBOL: GREEK_SYMBOL "'"*
LATIN_SYMBOL_WITH_LATIN_SUBSCRIPT: /([a-zA-Z]'*)_(([A-Za-z0-9]|[a-zA-Z]+)|\{[A-Za-z0-9,+\- ]+'*\})/
LATIN_SYMBOL_WITH_GREEK_SUBSCRIPT: /([a-zA-Z]'*)_/ GREEK_SYMBOL | /([a-zA-Z]'*)_/ L_BRACE SUBSCRIPTED_GREEK_SYMBOL R_BRACE
// best to define the variant with braces like that instead of shoving it all into one case like in
// /([a-zA-Z])_/ L_BRACE? GREEK_SYMBOL R_BRACE? because then we can easily error out on input like
// r"h_{\theta"
GREEK_SYMBOL_WITH_LATIN_SUBSCRIPT: SUBSCRIPTED_GREEK_SYMBOL /_(([A-Za-z0-9]|[a-zA-Z]+)|\{[A-Za-z0-9,+\- ]+'*\})/
// a subscript after a power, as in x^2_1
SUBSCRIPT: /_([A-Za-z0-9]|\{[A-Za-z0-9,+\- ]+\})/
GREEK_SYMBOL_WITH_GREEK_SUBSCRIPT: SUBSCRIPTED_GREEK_SYMBOL /_/ (GREEK_SYMBOL | L_BRACE SUBSCRIPTED_GREEK_SYMBOL R_BRACE)
MULTI_LETTER_SYMBOL: /[a-zA-Z]+(\s+[a-zA-Z]+)*'*/
%import common.DIGIT -> DIGIT
CMD_PRIME: "\\prime"
CMD_ASTERISK: "\\ast"
PRIMES: "'"+
STARS: "*"+
PRIMES_VIA_CMD: CMD_PRIME+
STARS_VIA_CMD: CMD_ASTERISK+
CMD_IMAGINARY_UNIT: "\\imaginaryunit"
CMD_BEGIN: "\\begin"
CMD_END: "\\end"
// matrices
IGNORE_L: /[ \t\n\r]*/ L_BRACE* /[ \t\n\r]*/
IGNORE_R: /[ \t\n\r]*/ R_BRACE* /[ \t\n\r]*/
ARRAY_MATRIX_BEGIN: L_BRACE "array" R_BRACE L_BRACE /[^}]*/ R_BRACE
ARRAY_MATRIX_END: L_BRACE "array" R_BRACE
AMSMATH_MATRIX: L_BRACE "matrix" R_BRACE
AMSMATH_PMATRIX: L_BRACE "pmatrix" R_BRACE
AMSMATH_BMATRIX: L_BRACE "bmatrix" R_BRACE
// Without the (L|R)_PARENs and (L|R)_BRACKETs, a matrix defined using
// \begin{array}...\end{array} or \begin{matrix}...\end{matrix} must
// not qualify as a complete matrix expression; this is done so that
// if we have \begin{array}...\end{array} or \begin{matrix}...\end{matrix}
// between BAR pairs, then they should be interpreted as determinants as
// opposed to sympy.Abs (absolute value) applied to a matrix.
CMD_BEGIN_AMSPMATRIX_AMSBMATRIX: CMD_BEGIN (AMSMATH_PMATRIX | AMSMATH_BMATRIX)
CMD_BEGIN_ARRAY_AMSMATRIX: (L_PAREN | L_BRACKET) IGNORE_L CMD_BEGIN (ARRAY_MATRIX_BEGIN | AMSMATH_MATRIX)
CMD_MATRIX_BEGIN: CMD_BEGIN_AMSPMATRIX_AMSBMATRIX | CMD_BEGIN_ARRAY_AMSMATRIX
CMD_END_AMSPMATRIX_AMSBMATRIX: CMD_END (AMSMATH_PMATRIX | AMSMATH_BMATRIX)
CMD_END_ARRAY_AMSMATRIX: CMD_END (ARRAY_MATRIX_END | AMSMATH_MATRIX) IGNORE_R "\\right"? (R_PAREN | R_BRACKET)
CMD_MATRIX_END: CMD_END_AMSPMATRIX_AMSBMATRIX | CMD_END_ARRAY_AMSMATRIX
MATRIX_COL_DELIM: "&"
MATRIX_ROW_DELIM: "\\\\"
FUNC_MATRIX_TRACE: "\\trace"
FUNC_MATRIX_ADJUGATE: "\\adjugate"
// determinants
AMSMATH_VMATRIX: L_BRACE "vmatrix" R_BRACE
CMD_DETERMINANT_BEGIN_SIMPLE: CMD_BEGIN AMSMATH_VMATRIX
CMD_DETERMINANT_BEGIN_VARIANT: BAR IGNORE_L CMD_BEGIN (ARRAY_MATRIX_BEGIN | AMSMATH_MATRIX)
CMD_DETERMINANT_BEGIN: CMD_DETERMINANT_BEGIN_SIMPLE | CMD_DETERMINANT_BEGIN_VARIANT
CMD_DETERMINANT_END_SIMPLE: CMD_END AMSMATH_VMATRIX
CMD_DETERMINANT_END_VARIANT: CMD_END (ARRAY_MATRIX_END | AMSMATH_MATRIX) IGNORE_R "\\right"? BAR
CMD_DETERMINANT_END: CMD_DETERMINANT_END_SIMPLE | CMD_DETERMINANT_END_VARIANT
FUNC_DETERMINANT: "\\det"
//////////////////// grammar //////////////////////
latex_string: _relation | _expression | relation_chain | membership
_one_letter_symbol: SYMBOL
| LATIN_SYMBOL_WITH_LATIN_SUBSCRIPT
| LATIN_SYMBOL_WITH_GREEK_SUBSCRIPT
| GREEK_SYMBOL_WITH_LATIN_SUBSCRIPT
| GREEK_SYMBOL_WITH_GREEK_SUBSCRIPT
| GREEK_SYMBOL_WITH_PRIMES
// LuaTeX-generated outputs of \mathit{foo'} and \mathit{foo}'
// seem to be the same on the surface. We allow both styles.
multi_letter_symbol: CMD_MATHIT L_BRACE MULTI_LETTER_SYMBOL R_BRACE
| CMD_MATHIT L_BRACE MULTI_LETTER_SYMBOL R_BRACE /'+/
number: /\d+(\.\d*)?|(?<![\d.])\.\d+/ | CMD_IMAGINARY_UNIT
_numeric: number | CMD_INFTY | CMD_PI | EULER_NUMBER
// an argument without braces
_single_token: DIGIT | SYMBOL | GREEK_SYMBOL_WITH_PRIMES | CMD_PI
_atomic_expr: _one_letter_symbol
| multi_letter_symbol
| _numeric
group_round_parentheses: L_PAREN _expression R_PAREN
group_square_brackets: L_BRACKET _expression R_BRACKET
group_curly_parentheses: L_BRACE _expression R_BRACE
group_curly_literal_parentheses: L_BRACE_LITERAL _expression R_BRACE_LITERAL
_relation: eq | ne | lt | lte | gt | gte
eq: _expression EQUAL _expression
ne: _expression NOT_EQUAL _expression
lt: _expression LT _expression
lte: _expression LTE _expression
gt: _expression GT _expression
gte: _expression GTE _expression
relation_chain: _expression _relation_symbol _expression (_relation_symbol _expression)+
_relation_symbol: EQUAL | NOT_EQUAL | LT | LTE | GT | GTE
membership: _expression (CMD_IN | CMD_NOTIN) number_set
number_set: CMD_MATHBB L_BRACE SYMBOL R_BRACE
_expression_core: _atomic_expr | group_curly_parentheses
add: _expression ADD (_expression_mul | unary_sign)
| ADD _expression_mul
sub: _expression SUB (_expression_mul | unary_sign)
| SUB _expression_mul
unary_sign: (ADD | SUB) _expression_mul
mul: _expression_mul MUL_SYMBOL (_expression_power | adjacent_expressions)
div: _expression_mul DIV_SYMBOL (_expression_power | adjacent_expressions)
adjacent_expressions: (_one_letter_symbol | _numeric | power_with_subscript | _delimited) _expression_mul
| _function adjacent_functions
| fraction _expression_mul
| superscript _expression_mul
// an expression whose end is marked, so that what follows can only be another factor
_delimited: group_round_parentheses | group_square_brackets | group_curly_parentheses
| group_curly_literal_parentheses | square_root | abs | floor | ceil | factorial | binomial
| matrix
?adjacent_functions: _function
| _function adjacent_functions -> adjacent_expressions
// The argument of a function command binds tighter than any operator, and it contains no
// function unless it is one: \sin x \cos y is sin(x)*cos(y), \sin \cos x is sin(cos(x)).
// Followed by more factors, the function applies either to all of them or to the first one
// alone: \sin xyz is sin(x*y*z) or sin(x)*y*z. A product starting with a number is the
// argument: \sin 2x is sin(2*x). An argument in parentheses ends there.
?function_argument: _argument_factor
| _argument_head argument_factors -> adjacent_expressions
| (_unnumbered_head | group_round_parentheses) argument_factors -> factors_after_argument
| _builtin_function
| ADD function_argument -> add
| SUB function_argument -> sub
?argument_factors: _argument_factor
| _argument_factor argument_factors -> adjacent_expressions
_argument_factor: _argument_head | group_round_parentheses
_argument_head: _numeric | _unnumbered_head
_unnumbered_head: _one_letter_symbol
| multi_letter_symbol
| group_curly_parentheses
| group_square_brackets
| group_curly_literal_parentheses
| fraction
| binomial
| function_applied
| _integral
| limit
| matrix
| superscript
| matrix_prime
| symbol_prime
| summation
| product
_expression_func: _expression_core
| group_round_parentheses
| group_square_brackets
| group_curly_literal_parentheses
| fraction
| binomial
| _function
| _integral// | derivative
| limit
| matrix
_expression_power: _expression_func | superscript | matrix_prime | symbol_prime | power_with_subscript
_expression_mul: _expression_power
| mul | div | adjacent_expressions
| summation | product
_expression: _expression_mul | add | sub
_limit_dir: "+" | "-" | L_BRACE ("+" | "-") R_BRACE
limit_dir_expr: _expression CARET _limit_dir
group_curly_parentheses_lim: L_BRACE _expression LIM_APPROACH_SYM (limit_dir_expr | _expression) R_BRACE
limit: FUNC_LIM UNDERSCORE group_curly_parentheses_lim _expression
differential: DIFFERENTIAL_SYMBOL _one_letter_symbol
//_derivative_operator: CMD_FRAC L_BRACE DIFFERENTIAL_SYMBOL R_BRACE L_BRACE differential R_BRACE
//derivative: _derivative_operator _expression
_integral: normal_integral | integral_with_special_fraction
normal_integral: FUNC_INT _expression DIFFERENTIAL_SYMBOL _one_letter_symbol
| FUNC_INT (CARET _expression_core UNDERSCORE _expression_core)? _expression? DIFFERENTIAL_SYMBOL _one_letter_symbol
| FUNC_INT (UNDERSCORE _expression_core CARET _expression_core)? _expression? DIFFERENTIAL_SYMBOL _one_letter_symbol
group_curly_parentheses_int: L_BRACE _expression? differential R_BRACE
special_fraction: CMD_FRAC group_curly_parentheses_int group_curly_parentheses
integral_with_special_fraction: FUNC_INT special_fraction
| FUNC_INT (CARET _expression_core UNDERSCORE _expression_core)? special_fraction
| FUNC_INT (UNDERSCORE _expression_core CARET _expression_core)? special_fraction
group_curly_parentheses_special: UNDERSCORE L_BRACE _atomic_expr EQUAL _expression R_BRACE CARET _expression_core
| CARET _expression_core UNDERSCORE L_BRACE _atomic_expr EQUAL _expression R_BRACE
summation: FUNC_SUM group_curly_parentheses_special _expression
| FUNC_SUM group_curly_parentheses_special _expression
product: FUNC_PROD group_curly_parentheses_special _expression
| FUNC_PROD group_curly_parentheses_special _expression
superscript: _expression_func CARET (_expression_power | CMD_PRIME | CMD_ASTERISK)
| _expression_func CARET L_BRACE (PRIMES | STARS | PRIMES_VIA_CMD | STARS_VIA_CMD) R_BRACE
| _expression_func CARET L_BRACE CMD_CIRC R_BRACE -> degree
| _expression_func CARET CMD_CIRC -> degree
power_with_subscript: (SYMBOL | SUBSCRIPTED_GREEK_SYMBOL) CARET _expression_core SUBSCRIPT
matrix_prime: (matrix | group_round_parentheses) PRIMES
symbol_prime: (LATIN_SYMBOL_WITH_LATIN_SUBSCRIPT
| LATIN_SYMBOL_WITH_GREEK_SUBSCRIPT
| GREEK_SYMBOL_WITH_LATIN_SUBSCRIPT
| GREEK_SYMBOL_WITH_GREEK_SUBSCRIPT) PRIMES
fraction: _basic_fraction
| _simple_fraction
| _general_fraction
_basic_fraction: CMD_FRAC _single_token _single_token
_simple_fraction: CMD_FRAC _single_token group_curly_parentheses
| CMD_FRAC group_curly_parentheses _single_token
_general_fraction: CMD_FRAC group_curly_parentheses group_curly_parentheses
binomial: _basic_binomial
| _simple_binomial
| _general_binomial
_basic_binomial: CMD_BINOM _single_token _single_token
_simple_binomial: CMD_BINOM _single_token group_curly_parentheses
| CMD_BINOM group_curly_parentheses _single_token
_general_binomial: CMD_BINOM group_curly_parentheses group_curly_parentheses
list_of_expressions: _expression ("," _expression)*
function_applied: _one_letter_symbol L_PAREN list_of_expressions R_PAREN
inverse_function_applied: _one_letter_symbol CARET L_BRACE SUB "1" R_BRACE L_PAREN list_of_expressions R_PAREN
min: FUNC_MIN L_PAREN list_of_expressions R_PAREN
max: FUNC_MAX L_PAREN list_of_expressions R_PAREN
bra: CMD_LANGLE _expression BAR
ket: BAR _expression CMD_RANGLE
inner_product: CMD_LANGLE _expression BAR _expression CMD_RANGLE
_function: function_applied | inverse_function_applied | _builtin_function
_builtin_function: abs | floor | ceil
| trigonometric_function | trigonometric_function_power
| named_function
| exponential
| function_power
| log
| square_root
| factorial
| conjugate
| max | min
| bra | ket | inner_product
| determinant
| trace
| adjugate
exponential: FUNC_EXP function_argument
function_power: (FUNC_EXP | FUNC_LOG | FUNC_LN | FUNC_LG) CARET _expression_core function_argument
log: FUNC_LOG function_argument
| FUNC_LN function_argument
| FUNC_LG function_argument
| FUNC_LOG UNDERSCORE (DIGIT | _one_letter_symbol | CMD_PI) function_argument
| FUNC_LOG UNDERSCORE group_curly_parentheses function_argument
square_root: FUNC_SQRT group_curly_parentheses
| FUNC_SQRT group_square_brackets group_curly_parentheses
| FUNC_SQRT _single_token
factorial: _expression_func BANG
conjugate: CMD_OVERLINE group_curly_parentheses
| CMD_OVERLINE DIGIT
trigonometric_function: FUNC_TRIG function_argument
trigonometric_function_power: FUNC_TRIG CARET _expression_core function_argument
named_function: FUNC_OPERATORNAME function_argument
| FUNC_OPERATORNAME CARET _expression_core function_argument
| FUNC_OPERATORNAME L_PAREN _expression ("," _expression)+ R_PAREN
abs: BAR _expression BAR
floor: L_FLOOR _expression R_FLOOR
ceil: L_CEIL _expression R_CEIL
matrix: CMD_MATRIX_BEGIN matrix_body CMD_MATRIX_END
matrix_body: matrix_row (MATRIX_ROW_DELIM matrix_row)* (MATRIX_ROW_DELIM)?
matrix_row: _expression (MATRIX_COL_DELIM _expression)*
determinant: (CMD_DETERMINANT_BEGIN matrix_body CMD_DETERMINANT_END)
| FUNC_DETERMINANT function_argument
trace: FUNC_MATRIX_TRACE function_argument
adjugate: FUNC_MATRIX_ADJUGATE function_argument
[Dauer der Verarbeitung: 0.11 Sekunden, vorverarbeitet 2026-11-20]