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.

Close

#7 operatorPrecedence: more uniform parse results

closed
nobody
None
5
2008-12-20
2008-09-18
Eike Welk
No

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.

Discussion

  • Paul McGuire
    Paul McGuire
    2008-11-14

    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 simpleBool.py example (http://pyparsing.wikispaces.com/file/view/simpleBool.py).

     
  • Paul McGuire
    Paul McGuire
    2008-12-20

    • status: open --> closed