[Pyparsing] Recursive grammar
Brought to you by:
ptmcg
From: thomas_h <th...@go...> - 2007-11-09 11:04:56
|
Hi all, I'm new to pyparsing, and I'm struggling with recursive production rules. As an example, I want to parse expressions that are either numbers or additions of numbers. Grammar: number :: '0'..'9'* aexp :: number | number '+' aexp The following straight-forward implementation doesn't work: from pyparsing import * number = Word(nums) aexp = Forward() aexp = Or ([ number, number + '+' + aexp , ]) Running the code yields: aexp.parseString("3+4") => (['3'], {}) I probably miss something simple here, but I can't figure out what. If I make aexp non-recursive ("..., number + '+' + number,])") it works, but this doesn't solve the general problem (re-arranging the cases in the 'Or' argument doesn't help either). I looked into the "fourFn.py" example, but this is much too elaborate to answer my question. Thanks, Thomas |