Manuel Chakravarty writes:

> What kind of implementation did the originators of this
> clause envision?  If the layout rule is really implemented
> as a filter between the scanner and the parser, it seems
> extremely awkward to add a dependency on the error condition
> of the parser - in particular, it makes a functional, ie,
> side-effect free implementation rather hard and a true two
> phase implementation impossible.  So, I guess (I hope!!) 
> there is a nifty trick that lets you achieve the same effect
> by using only conditions depending on local information
> (either during layout processing or by letting the parser
> insert the missing braces).

GHC and Hugs both make use of yacc-style error recovery, albeit in a very
limited form.  The idea is to have a production in your grammar like this:

close_brace :   '}'
                |     error

where the '}' token is assumed to have been inserted by the lexical analyser
as a result of layout (i.e. a token was found to be less indented than the
current layout context).  The error case fires if any other token is
encountered, and the semantic action for this production will probably pop
the current layout context and carry on (in practice you also have to tell
yacc not to continue with error recovery, otherwise all sorts of strange
things happen).  Take a look at GHC's parser for the details.

I believe you're right in that a true two-phase implementation of the
Haskell grammar is impossible.  This is consistent with Haskell's policy of
making life easy for programmers and hard for compiler writers :)

Cheers,
        Simon


Reply via email to