Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your code you are comparing each operator with its children to see if you need parentheses around it. But you should actually be comparing it with its parent. Here are some rules that can determine if parentheses can be omitted:</p> <ol> <li>You never need parentheses around the operator at the root of the AST.</li> <li>If operator A is the child of operator B, and A has a higher precedence than B, the parentheses around A can be omitted.</li> <li>If a left-associative operator A is the left child of a left-associative operator B with the same precedence, the parentheses around A can be omitted. A left-associative operator is one for which <code>x A y A z</code> is parsed as <code>(x A y) A z</code>.</li> <li>If a right-associative operator A is the right child of a right-associative operator B with the same precedence, the parentheses around A can be omitted. A right-associative operator is one for which <code>x A y A z</code> is parsed as <code>x A (y A z)</code>.</li> <li>If you can assume that an operator A is <a href="http://en.wikipedia.org/wiki/Associative_property" rel="nofollow">associative</a>, i.e. that <code>(x A y) A z = x A (y A z)</code> for all x,y,z, and A is the child of the same operator A, you can choose to omit parentheses around the child A. In this case, reparsing the expression will yield a different AST that gives the same result when evaluated.</li> </ol> <p>Note that for your first example, the desired result is only correct if you can assume that <code>+</code> is associative (which is true when dealing with normal numbers) and implement rule #5. This is because your input tree is built in a right-associative fashion, while operator <code>+</code> is normally left-associative.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload