## [Pyparsing] Function parameters that may have brackets

 [Pyparsing] Function parameters that may have brackets From: happybrowndog - 2008-05-18 06:44:57 ```Hi, I have been trying to solve this, but just can't figure it out. I have a function with parameters that I want to parse. The function is of this form: NUMBERTOSTR(param) where param is any expression that evaluates to an integer or floating point number, for example: 3 + 2 (3 + 2) / 4 ((3 + 2) / 4) + 7 param thus can have any number of brackets. So I coded param using the following pyparsing expression: param = Forward() param << ZeroOrMany("(") + Operand + Operator + Operand + ZeroOrMany(")") + ZeroOrMany("(") + Optional(param) + ZeroOrMany(")") and NUMBERTOSTR is coded as follows: NUMBERTOSTR = Literal("NUMBERTOSTR(") + param + Literal(")") Here is the problem: NUMBERTOSTR.parseString("NUMBERTOSTR(( ( (3+2)/4) + 7 ) )") returns an exception of not being able to find a ")" character. The reason appears to be that ZeroOrMany(")") consumes all the right brackets so that Literal(")") in NUMBERTOSTR is left wanting. How do I solve this? ```

 [Pyparsing] Function parameters that may have brackets From: happybrowndog - 2008-05-18 06:44:57 ```Hi, I have been trying to solve this, but just can't figure it out. I have a function with parameters that I want to parse. The function is of this form: NUMBERTOSTR(param) where param is any expression that evaluates to an integer or floating point number, for example: 3 + 2 (3 + 2) / 4 ((3 + 2) / 4) + 7 param thus can have any number of brackets. So I coded param using the following pyparsing expression: param = Forward() param << ZeroOrMany("(") + Operand + Operator + Operand + ZeroOrMany(")") + ZeroOrMany("(") + Optional(param) + ZeroOrMany(")") and NUMBERTOSTR is coded as follows: NUMBERTOSTR = Literal("NUMBERTOSTR(") + param + Literal(")") Here is the problem: NUMBERTOSTR.parseString("NUMBERTOSTR(( ( (3+2)/4) + 7 ) )") returns an exception of not being able to find a ")" character. The reason appears to be that ZeroOrMany(")") consumes all the right brackets so that Literal(")") in NUMBERTOSTR is left wanting. How do I solve this? ```
 Re: [Pyparsing] Function parameters that may have brackets From: Paul McGuire - 2008-05-18 07:18:18 ```-----Original Message----- From: pyparsing-users-bounces@... [mailto:pyparsing-users-bounces@...] On Behalf Of happybrowndog Sent: Saturday, May 17, 2008 3:25 PM To: pyparsing-users@... Subject: [Pyparsing] Function parameters that may have brackets Hi, I have been trying to solve this, but just can't figure it out. I have a function with parameters that I want to parse. The function is of this form: NUMBERTOSTR(param) where param is any expression that evaluates to an integer or floating point number, for example: 3 + 2 (3 + 2) / 4 ((3 + 2) / 4) + 7 param thus can have any number of brackets. >>>>>>>>>>>>>>>>>>>>>>>>>> Check out fourFn.py or simpleArith.py for examples on how to parse arithmetic expressions. You are on the right track in that Forward() is required, but you aren't really using it correctly. If you model your grammar after simpleArith, then your code will use a call to operatorPrecedence, but the implementation of operatorPrecedence uses Forward to define the nested expressions. The grammar in fourFn.py will show you how Forward should be used. If you don't have these in your pyparsing examples directory, you can get them directly from the pyparsing wiki Examples page: http://pyparsing.wikispaces.com/Examples. Or write back for more help. -- Paul ```