Re: [Pyparsing] Operator Precedence and Associativity.
Brought to you by:
ptmcg
From: Paul M. <pa...@al...> - 2006-09-13 13:39:23
|
This discussion from c.l.py (http://www.velocityreviews.com/forums/t335296-basic-tokenizer.html - about 2 years ago) might also shed some light on this topic, especially the comments (and posted code) from Andrea Griffini. He pointed out the '^' left-associativity bug in fourFn.py which I was able to fix in a subsequent release. I like the graphical rending of the parse tree. :) -- Paul > -----Original Message----- > From: pyp...@li... > [mailto:pyp...@li...] On > Behalf Of Ralph Corderoy > Sent: Wednesday, September 13, 2006 7:23 AM > To: Paolo Losi > Cc: pyp...@li... > Subject: Re: [Pyparsing] Operator Precedence and Associativity. > > > 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. > > > > -------------------------------------------------------------- > ----------- > Using Tomcat but need to do more? Need to support web > services, security? > Get stuff done quickly with pre-integrated technology to make > your job easier > Download IBM WebSphere Application Server v.1.0.1 based on > Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057& > dat=121642 > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > > |