Skip to content

makeExprParser: Mixing operators with different associativities in same precedence level does not work #87

Description

@cbjadwani

When makeExprParser is given two infix operators at the same precedence level but with different associativit it stops before parsing the expression completely for some inputs.

Example:

operatorTable =
  [ [ prefix "-" Negation
    , prefix "+" id
    ]
  , [ binaryR "^" Expo
    , binaryL "*" Product
    , binaryL "/" Division
    ]
  , [ binaryL "+" Sum
    , binaryL "-" Subtr
    ]
  ]

For the above slightly modified example from the tutorial the parser stops unexpectedly:

ghci> parseTest pExpr "2 ^ 2 * 3 ^ 2"
Expo (Int 2) (Int 2)

This is unexpected because documentation says it should work:

All operators in one list have the same precedence (but may have different associativity).

Looking at the code it seems like this could be fixed by adding recursion to addPrecLevel. But I am not sure this is a good idea so have not created a PR to fix this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions