Re: [Pyparsing] How to distinguish a variable from a integer
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2009-05-14 20:18:11
|
> I said that an operand could be a variable or a number to simplify things > given that the problem was between numbers and variables. But it's > actually > more complex than that: It could be a quoted string or a set (in the form > "{element1, element2, ...}" where each element can be a number, variable, > quoted string or even another set) too: > > operand = number | string | variable | set > 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. -- Paul |