Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.


#7 operatorPrecedence: more uniform parse results

Eike Welk

The parser which is generated by operator precedence creates quite different parse results for (binary) left associative operators in comparison to right associative operators.

Left associative operators - flat list:
[1, '+', 2, '+', 3]

Right associative operators - tree:
[1, '**', [2, '**', 3]]

I think both types of operators should result in a tree-shaped parse result like right associative operators.


  • Paul McGuire
    Paul McGuire

    There is nothing I can do about this. The right-associative operators are built using recursion, which works naturally into a nested tree. If the left-associative operators also used recursion, then I would get into an LR tailspin, so I have to use iteration instead. Iteration gives the flat list as you describe.

    If you wanted, you could get tricky and add a parse action to the iteration part of operatorPrecedence, to take the flat list and recast it as a nested list of binary operations. But oP is already pretty complicated, and also rather pokey, so I'm not sure that adding more complexity at parse time is a good idea. Instead, look at the iterative handling of the tokens in the example (

  • Paul McGuire
    Paul McGuire

    • status: open --> closed