Say a language defines adjacency of two mathematical unicode alphanumerical symbols as an operator. Say,
Here is one example using pyparsing in Python:
import pyparsing as pp
integer = pp.pyparsing_common.integer()
variable = pp.oneOf(list("abcdefghijklmnopqrstuvwxyz"))
base_operand = integer | variable
implied_multiplication = pp.Empty().addParseAction(lambda: "*")
expr = pp.infixNotation(base_operand,
[
("**", 2, pp.opAssoc.LEFT),
(implied_multiplication, 2, pp.opAssoc.LEFT),
(pp.oneOf("+ -"), 1, pp.opAssoc.RIGHT),
(pp.oneOf("* /"), 2, pp.opAssoc.LEFT),
(pp.oneOf("+ -"), 2, pp.opAssoc.LEFT),
])
This assumes that variables are just single characters. There is also some fudging of precedence of operations to make adjacency, exponentiation, and leading signs work. The parse action added to the implied_multiplication
expression is there to show the insertion of the multiplication operator.
Here is some test output:
tests = """
x-4
ax**2 + bx +c
ax**2-bx+c
mx+b
"""
expr.runTests(tests, fullDump=False)
prints:
x-4
[['x', '-', 4]]
ax**2 + bx +c
[[['a', '*', ['x', '**', 2]], '+', ['b', '*', 'x'], '+', 'c']]
ax**2-bx+c
[[['a', '*', ['x', '**', 2]], '-', ['b', '*', 'x'], '+', 'c']]
mx+b
[[['m', '*', 'x'], '+', 'b']]