Re: [Pyparsing] How to distinguish a variable from a integer
Brought to you by:
ptmcg
From: Gustavo N. <me...@gu...> - 2009-05-14 22:30:50
|
Paul said: > Ah, with that said, let me then suggest this as a starting point for > implementing operand (assuming that variables can not start with a digit): > > LBRACE = Suppress('{') > RBRACE = Suppress('}') > operand = Forward() > number = #...(as you have defined it in your original code) > string_ = quotedString.setParseAction(removeQuotes) > variable = #...(use your Unicode definition of choice) > set_ = Group(LBRACE + delimitedList(operand) + RBRACE) > operand << (number | string_ | variable | set_) > > delimitedList takes care of the repetition with intervening comma > delimited. Group packages the result in its own list, so that recursive set > definitions will maintain their nesting properly. The set-enclosing braces > are suppressed from the output - they are useful during parsing, but > unnecessary once the tokens have been grouped. > > This is the canonical form for defining a recursive expression like your > operand, mas o menos. Now you are free to include operand in other > expressions, or even operatorPrecedence. Thank you very much for that! I was going to talk about that, because the way I implemented it was more complex and operand.validate() raised an exception. But this fixes the problem. Thanks once again! -- Gustavo Narea <xri://=Gustavo>. | Tech blog: =Gustavo/(+blog)/tech ~ About me: =Gustavo/about | |