Search code examples
pythonparsingpyparsing

Parsing nested function calls using pyparsing


I'm trying to use pyparsing to parse function calls in the form:

f(x, y)

That's easy. But since it's a recursive-descent parser, it should also be easy to parse:

f(g(x), y)

That's what I can't get. Here's a boiled-down example:

from pyparsing import Forward, Word, alphas, alphanums, nums, ZeroOrMore, Literal

lparen = Literal("(")
rparen = Literal(")")

identifier = Word(alphas, alphanums + "_")
integer  = Word( nums )

functor = identifier

# allow expression to be used recursively
expression = Forward()

arg = identifier | integer | expression
args = arg + ZeroOrMore("," + arg)

expression << functor + lparen + args + rparen

print expression.parseString("f(x, y)")
print expression.parseString("f(g(x), y)")

And here's the output:

['f', '(', 'x', ',', 'y', ')']
Traceback (most recent call last):
  File "tmp.py", line 14, in <module>
    print expression.parseString("f(g(x), y)")
  File "/usr/local/lib/python2.6/dist-packages/pyparsing-1.5.6-py2.6.egg/pyparsing.py", line 1032, in parseString
    raise exc
pyparsing.ParseException: Expected ")" (at char 3), (line:1, col:4)

Why does my parser interpret the functor of the inner expression as a standalone identifier?


Solution

  • The definition of arg should be arranged with the item that starts with another at the left, so it is matched preferentially:

    arg = expression | identifier | integer