I am writing an equation parser for fun and I kinda thought parsing absolute value signs would be like parsing ordinary bracktets - but this is not so as there is no 'open' or 'close' version of $|$. In particular there are two potential interpretations of the expression $$ |x|x|x|,$$ namely $$ |x|x|x| = |x|\cdot x \cdot |x| = x^3,$$ or $$|x|x|x| = {\large|} x\cdot |x| \cdot x{ \large |} = |x^3|. $$
I think there is no agreed upon convention that determines which one of these should be correct. But I was wondering if there are any best practices that we can give some kind of arguments for.
I realize that the expression $|x|x|x|$ looks ridiculous, but we can also have for example $$ |x|f(x) + g(y)|y| . $$ I would certainly parse this as $(|x|f(x)) + (|y| g(y))$ but who am I to say it does not equal $|x \cdot |f(x)+g(y)| \cdot y|$.
Edit: The parser I wrote already accepts $\operatorname{abs}(x)$ and the like. But I want to allow the user to write the very natural $|x|$ as well. But then I can't prevent them for also writing $|x|x|x|$, even though this is silly and borderline criminal. So I do have to make some choice on how to handle this and am just wondering if anyone has any preference for one of the interpretations and maybe some reasons :)
|x|x|x|is parsed to $|x| \cdot x \cdot |x|$ so "closing first" is prefered by WA here. But if you just omit the last x and write|x|x||then it is parsed to $|x \cdot |x||$ so in this example "opening first" is prefered by WA, presumably because prefering the closing interpretation would give an invalid expression namely $|x| \cdot x \cdot ||$ which makes no sense (the last absolute value group would have an empty argument). I would probably prefer issuing a syntax error instead. – SampleTime Aug 20 '21 at 14:56