0

When building a unary operator in a tree, I usually draw it as a parent-child tree, for example:

-4

  (-)
   |
   |
   4

And when drawing a binary operator in a tree, it will have a left and right node, something like:

2-4

   -
  / \
 2   4

It makes sense to me where there needs to be associativity with multiple operators, even = will have the lhs and rhs:

a=4

     =
    / \
   /   \
a (lv)  4 (rv)

But I don't really understand why a unary operator -- where there is just a single-child in the parse tree, would have the concept of associativity. Why would, for example, the unary plus/minus be right-associative rather than just being 'non-associativite' or 'doesnt-matter' ?

Here is a helpful answer too: https://stackoverflow.com/a/14084830/12283181. And:

Associativity specification is redundant for unary operators and is only shown for completeness: unary prefix operators always associate right-to-left (sizeof ++*p is sizeof(++(*p))) and unary postfix operators always associate left-to-right (a[1][2]++ is ((a[1])[2])++). Note that the associativity is meaningful for member access operators, even though they are grouped with unary postfix operators: a.b++ is parsed (a.b)++ and not a.(b++).

Source: C++ Operator Precedence


carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • What is your question exactly? – rici Jan 17 '21 at 13:28
  • Also, I have to say that the answer you link is *not" helpful. It only confuses the issue. Almost any of yhe other answers are more helpful, although now that I've reread them all, I'm still partial to [my own answer](https://stackoverflow.com/a/14087026/1566221) – rici Jan 17 '21 at 13:36
  • @rici perhaps, but the answer is a bit over my head to understand, so I'll need a bit of help dumbing it down for myself. – carl.hiass Jan 17 '21 at 21:19
  • @rici actually a better question/follow-up to you rather than complaining is if you could suggest a resource (book or otherwise) that would be good for learning some of the concepts you mention in your two answers, as what you mention is a bit beyond my current level of parsing/c understanding ? – carl.hiass Jan 17 '21 at 21:32
  • If you're really interested in practical parsing, I'd go for Dick Grune's *Parsing Techniques, A Practical Guide*, which in addition to being what it's subtitle says it is, contains good summaries of a variety of parsing algorithms. But if you can manage to formulate an actual question, I'll attempt to answer it. (It's possible that the answer is just "Associativity is a property of binary infix operators, not a general concept in parsing.") – rici Jan 18 '21 at 14:18

0 Answers0