## Re: [Pyparsing] Operator Precedence and Associativity.

 Re: [Pyparsing] Operator Precedence and Associativity. From: Ralph Corderoy - 2006-09-13 12:23:32 Hi Paolo, > Ralph Corderoy wrote: > > Yes, but I don't think the R.D. parser below, written by hand, not with > > pyparsing, is left-recursive. It can just support different > > associativity depending on how the parse tree is built. > > It is not left-recursive, of course. > In your example you basically build the tree in the correct way > instead of rearranging it. But please note that in my example I could > not add a parse action to ZeroOrMore but to expr. I agree. Given expr << term + ZeroOrMore(plusop + term) we can't use Group() because the left-most (term+term) isn't accessible. > a new token as the ones that I suggested could maybe provide a solution > (similar to your parse_term or parse_expr in how the tree is built). > ... > If you think that could be a solution, I could try to provide an > implementation... That's OK. The use of parseAction() that you showed me is enough to give me the idea. I guess I'd like to write number = Words(digit) factor = Forward() factor << RightBinOpSeq(number, expop, factor) unary = OptRightUnaryOpSeq(signop, factor) term = unary ^ LeftBinOpSeq(unary, multop, unary) expr = term ^ LeftBinOpSeq(term, plusop, term) statement = expr + semicolon and have the /(Opt)?(Left|Right|Non)(Unary|Bin)OpSeq/ routines do the Grouping implicitly so 1+2*-3^4*5+-+-6 yeilds the tree ( , +, ) (1, +, ) (-, ) ( , *, 5) (+, ) (2, *, ) (-, 6) (-, ) (3, ^, 4) Although, something like expop = Literal('^') signop = OneOf('+ -') multop = Literal('*') plusop = Literal('+') expr = Forward() term = Forward() unary = Forward() factor = Forward() expr = OperatorGrammar( \ expop, 2, 'R', signop, 1, 'R', multop, 2, 'L', plusop, 2, 'L', ''') also seems attractive, and simpler to specify. Cheers, Ralph.