adjacency as an operator - can any lexer handle it?

前端 未结 3 803
眼角桃花
眼角桃花 2021-01-21 17:08

Say a language defines adjacency of two mathematical unicode alphanumerical symbols as an operator. Say,

3条回答
  •  不要未来只要你来
    2021-01-21 17:59

    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']]
    

提交回复
热议问题