happy --glr
Works fine -- there is just one thing that must be manually changed in the output. Note however, it is different from using Happy on a typical _.y file.
Basically, there are a bunch of differences you have to take into consideration. The type of the expected lexer result is different. The type of return from the parser is less controllable and different. And the way you use the parser is slightly different. The necessary information can be found on : http://www.haskell.org/happy/doc/html/sec-glr-using.html and it's really important to understand that web-page to correctly use the glr option.
Here is how you can use Happy with the glr option to produce a standalone parser; all we will care about is if the parse was successful -- you can read more about the results of parsing and how to interpret them on the above page. We will do this for the parser above, ABC. First, create a file called ABCMain.hs:
module Main where
import ABC
import ABCData
main = do
inp <- getContents
case happyParse (lexer inp) of
ParseOK _ _ -> putStrLn "success"
_ -> putStrLn " success"
lexer ('a':xs) = [A] : lexer xs
ETC -- note that it is [[Token]] instead of [Token]
And the ABC.y file is simply:
%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) -- you must have Eq and Ord
parseError _ = error "bad"
}
Run
happy --glr ABC.y
Producing the two files. Now, there is one point I would love someone to comment about -- essentially you have to manually change, in the generated file ABC.hs, the line
import System
to
import System.IO
Then, the following works for me:
ghc --make ABCMain.hs
Everything compiles and the parser works as expected. Feel free to let me know if I am doing correctly.