parsing an Excel formula with the re module
Paul McGuire
ptmcg at austin.rr.com
Thu Jan 14 23:41:59 EST 2010
I never represented that this parser would handle any and all Excel
formulas! But I should hope the basic structure of a pyparsing
solution might help the OP add some of the other features you cited,
if necessary. It's actually pretty common to take an incremental
approach in making such a parser, and so here are some of the changes
that you would need to make based on the deficiencies you pointed out:
functions can have a variable number of arguments, of any kind of
expression
- statFunc = lambda name : CaselessKeyword(name) + LPAR + delimitedList
(expr) + RPAR
sheet name could also be a quoted string
- sheetRef = Word(alphas, alphanums) | QuotedString("'",escQuote="''")
add boolean literal support
- boolLiteral = oneOf("TRUE FALSE")
- operand = numericLiteral | funcCall | boolLiteral | cellRange |
cellRef
These small changes are enough to extend the parser to successfully
handle the test2a, 2b, and 3a cases. (I'll add this to the pyparsing
wiki examples, as it looks like it is a good start on a familiar but
complex expression.)
-- Paul
More information about the Python-list
mailing list