3 ms·
People can't answer a question like this honestly. It would be a deal breaker for many more than will admit it here. You have to defer the grouping of operands
by andreyf 17y ago
People can't answer a question like this honestly. It would be a deal breaker for many more than will admit it here.
You have to defer the grouping of operands until well after the actual parse-time.
Why?
you could have the same operator with different precedence depending on the macros in scope.
But if your macros have scope, isn't this a problem independent of operator precedence?
I'd suggest defining higher-order macros, where you can have a something like infix(op, prec=0, assoc=LEFT), used like:
infix('+', 0)
infix('*', 1)
...
For a similar example, see Haskell's infixr/infixl: http://www.haskell.org/tutorial/functions.html#sect3.2.2 http://www.haskell.org/tutorial/functions.html#sect3.2.2
Also, whatever it is you are doing might be easiest to implement using Alex Warth's OMeta: http://tinlizzie.org/ometa/ http://tinlizzie.org/ometa/
- daeken 17y ago> You have to defer the grouping of operands until well after the actual parse-time. Why? Well, in the case of my parser, I get back an S-expression corresponding to the groups (parentheses, braces, brackets) in the code, with each token inside that. So to group expressions, I then have to go over that, applying the macros in order of precedence. If I make operators purely left-to-right (with the exception of . , which has to be special-cased), I can do this grouping at the parser level and simplify things. But if your macros have scope, isn't this a problem independent of operator precedence? It's still a problem, as it is with any language with macros, but by letting operators have precedence that can change from file to file (or even worse, from function to function, if you're sadistic) would make it very difficult for people to track what's going on. With the pure left-to-right parsing, the behavior of operator grouping would be consistent regardless of anything else that's going on.