# How do you define and parse variables (free or bound) from user-entered strings?

I'm writing an application in which the user might enter expressions such as $$\text{lim}_{i \in I} \beta(i)$$ where $$\beta$$ is a functor. That's just an example, the expressions, which contain variables, are going to be much more complicated.

Was wondering how do I go about defining what a variable should be and then "parsing" and registering the variables for sake of substitution algorithms later on? Substitution will be type-theoretical (if that makes any sense).

For example, should $$x_1$$ be considered an atomic variable or only single letters such as $$x$$, because if I include $$x_1$$ as an atomic variable, what if $$x_i$$ is a function from $$i : \Bbb{N}$$? Confusion comes in here.

How can I be sure that the x in "\text{hom}" doesn't get treated like a variable?

What I certainly do not want is the user to have to firstly define the involved variables. The variables need to be auto-detected.

I think $$x', x'', x''', \dots$$ should all be atomic variables, right?

• Honestly, this seems more like a UX question than a formal language question. Mathematical notation is, ironically, usually not very precise; reading it requires a certain amount of common sense and contextual understanding. There are ways to make the notation precise, of course, but not everyone uses the same conventions, so again context and semantics become important for the human reader. If you're trying to suss out what the author meant, you'll probably need to do a speculative parse, like Wolfram. If you are specifying a language yourself, document it well :-) – rici Oct 8 '19 at 16:49
• @rici thank you for the advice, but it does not address my question at all. Regardless of whether it's a UX question, there is indeed parsing involved. I'm just looking for the best way to do that and for a typical definition of variable. Not to mention how to handle substitution. These kind of algorithms aren't documented well on the web. Why would I ask such a convoluted question? Because when I'm coding I find that the way I do a particular thing depends heavily on how I do another thing. So for example how variable substitution works will guide how I parse things. – BananaCats Author Oct 8 '19 at 19:03
• The only way out here is to document the language used for mathematical expressions by a formal grammar, having e.g. a non-terminal variable which gives a clear syntactic description of what a variable is. Beside that, as already noted, there's always ambiguity involved, because context may change semantics. $x'$ in some context might be a single variable denoting a new variable that is somehow related to another variable $x$. In another context, $'$ could be a postfix operator applied to the variable $x$ (think of Lagrange's notation for differentiation). – siracusa Oct 8 '19 at 23:30

Thanks to the commenters above. They suggest a grammar definition for parsing. Here's one in PyPEG2 (untested and incomplete), but shows how I'll handle variable and sub/superscripting.

from term import Term
from pypeg2 import *

alphabet = RegEx( r'\alpha|\beta|\gamma|\delta|\epsilon|' + \
r'\zeta|\eta|\theta|\vartheta|\iota|' + \
r'\kappa|\lambda|\mu|\nu|\xi|\pi|' + \
r'\rho|\varrho|\sigma|\tau|\upsilon|' + \
r'\phi|\varphi|\chi|\psi|\omega' + \
r'\Gamma|\Delta|\Theta|\Xi|\Pi|\Sigma' + \
r'\Upsilon|\Phi|\Psi|\Omega|.')  # Yes an "any char"

class Expression:
grammar = ([Variable, optional(Subscript)], optional(Expression))

class BracesExpr:
grammar = BracesExpr

class Subscript:
grammar = '_',

class Supscript:
grammar = '^', optional(BracesExpr)

class Script:
grammar = [(Subscript, Supscript), (Supscript, Subscript)]

class InlineFormula:
grammar = '$$', Expression, '$$'

class BlockFormula:
grammar = '$$', Expression, '$$'

class Variable:
grammar = [alphabet, optional(Script)]


So a variable has a base atom that's any LaTeX greek symbol or literally any single unicode char (the '.' at the end of alphabet regex).