Pavel Tomasko
Pavel Tomasko

Reputation: 170

Add function parsing to simple pyparsing arithmetics grammar

I have this code:

    import pyparsing as pp

    point = pp.Literal(".")

    number = pp.Combine(pp.Word(pp.nums) + pp.Optional(point + pp.Word(pp.nums)))

    lpar = pp.Literal("(").suppress()
    rpar = pp.Literal(")").suppress()

    plus = pp.Literal("+")
    minus = pp.Literal("-")
    mult = pp.Literal("*")
    div = pp.Literal("/")
    exp = pp.Literal("^")

    mod = pp.Literal("%")
    idiv = pp.Literal("//")

    comma = pp.Literal(',')

    ident = pp.Combine(pp.Word(pp.alphanums) + pp.Literal("_") + pp.Word(pp.nums))

    expr = pp.Forward()

    atom = number | ident | pp.Group(lpar + expr + rpar) 

    factor = pp.Forward()
    factor <<= pp.Group(atom + pp.ZeroOrMore(exp + factor))

    term = pp.Group(factor + pp.ZeroOrMore((mult | idiv | div | mod | exp) + factor))

    expr <<= pp.Optional(plus | minus) + term + pp.ZeroOrMore((plus | minus) + term)

which gives parser for simple arithmetic expressions in expr. How can I change it so that the parser accepts also function calls (e.g. 1 + max(1 + 2, var_14 / 3)) as a part of mathematical expressions on its input? I think about including something like this in the grammar:

    arglist = atom | pp.Combine(arglist + comma + atom)
    func = ident + lpar + arglist + rpar

Upvotes: 1

Views: 174

Answers (1)

PaulMcG
PaulMcG

Reputation: 63762

In this grammar, a function call is at the same level as a variable reference or numeric literal, so I would add it as part of the definition of atom:

function_call = Group(ident + lpar + Group(Optional(delimitedList(expr))) + rpar)

atom = number | function_call | ident | pp.Group(lpar + expr + rpar)

Also note the use of delimitedList in place of expr + ZeroOrMore(comma + expr).

Upvotes: 1

Related Questions