RE: H98 Report: expression syntax glitch
Consider the following Haskell 98 expressions:
(let x = 10 in x `div`) (let x = 10 in x `div` 3)
To parse the first, a bottom-up parser should reduce the let-expression before the operator, while to parse the second it should shift. But it needs 4 tokens of lookahead to decide which to do. That seems unreasonable, and is well beyond the LALR(1) parsers used by Hugs and GHC. Replacing `div` by + needs 2 tokens of lookahead, which is still too much. I think the first should be made illegal, but can't think of a clean rule. (There are similar expressions using lambda and if.)
This is a known problem with the Haskell grammar. Another example in a similar vein is let x = 3 in x == 4 == True which should parse as (let x = 3 in x == 4) == True according to the "extends as far to the right as possible" rule, because '==' is nonfix. In order to do this, the parser must have access to the fixity information for '==', which is unreasonable (fixity declarations can even be local to a binding group). Perhaps it is possible to post-process the parse when the fixities have been resolved, but that seems overly complex even if it is possible. My favourite solution would be to remove the notion of operator precedence from the grammar altogether and specify fixity resolution separately. Cheers, Simon
On Mon, Feb 25, 2002 at 04:27:56PM -0000, Simon Marlow wrote:
This is a known problem with the Haskell grammar. Another example in a similar vein is
let x = 3 in x == 4 == True
which should parse as
(let x = 3 in x == 4) == True
according to the "extends as far to the right as possible" rule, because '==' is nonfix. In order to do this, the parser must have access to the fixity information for '==', which is unreasonable (fixity declarations can even be local to a binding group).
I'm not sure I agree with you here. By this point we have lexed and applied the layout rule so all the braces and semi-colons are in place. Extracting the fixity information is fairly easy Noticing when operators are redefined (thus dropping fixity information) is slightly trickier but still doable, and then all you have to do is annotate your tree appropriately. Further, if you first construct a tree of blocks then this can all be done in linear time I believe. I don't see that this is a forced change; it seems to be purely to make implementors lives easier (maybe I am just bitter from spending a while thinking about and implementing this myself!) and makes some previously legal scripts illegal. Thanks Ian
participants (2)
-
Ian Lynagh -
Simon Marlow