Search code examples
haskellhappyglr

Errors when compiling GLR parsers from Happy - 'parse error on input ‘case’'


I have tried multiple example grammars and get the same error when I try to compile the generated files.

For example I have followed exactly the solution to this question - GLR_Lib.hs: Could not find module 'System'

where the grammar file is

%tokentype { ABC }
%error { parseError }
%token 
    a { A }
    b { B }
    c { C }
%%
s1 : a a a b {} | b s2 a {}
s2 : b a b s2 {} | c {}
{
data ABC = A | B | C deriving (Eq,Ord,Show) 
parseError _ = error "bad"
}

But when I compile I get:

[1 of 2] Compiling ABCData ( ABCData.hs, ABCData.o )

[2 of 2] Compiling ABC ( ABC.hs, ANC.o )

GLR_Lib.hs:164:2: parse error on input ‘case’

This exact error has happened with every grammar that I have tried. I don't know what I could be doing differently to people that have the examples working successfully.


Solution

  • There are indentation errors in the GLR_Lib template. This is what I did to get it to work:

    1. Create the ABCMain.hs file.
    2. Create a new directory ./templates for the edited templates.
    3. Find the originals - e.g. use locate GLR_Lib. On OSX with the Haskell Platform I found them in /Library/Haskell/current/share/happy-1.19.4/
    4. Copy all of the templates to ./templates
    5. Make the following edits to ./templates/GLR_Lib:
      • line 44: comment out import System
      • line 161: replace the leading space with a tab: case new_stks of
      • line 190: replace the leading space with a tab: stks' <- foldM (pack i) stks reds
    6. Run: happy --glr --template=./templates ABC.y
    7. Compile with: ghc --make ABCMain

    You will probably only need the GLR_Lib and GLR_Base templates.