[Haskell-beginners] a problem with happy ABCParser example

kak dod kak.dod2008 at gmail.com
Mon Apr 2 18:26:04 CEST 2012


Hello,
Greeting everybody.
I am a beginner of Haskell. I am trying out the happy.
When I compiled the happy grammar file for the ABCParser given at
http://www.haskell.org/happy/doc/html/sec-AttributeGrammarExample.html
I did get a Haskell source file.
Then I loaded this file, named "abc.hs" into ghci.
There is a function called "parse" available to us.
GHCi shows its type to be
parse :: [Char] -> [Char]

But no matter whatever input I give to the parse function, it always gives
the following error:
"*** Exception: parse error

Here are some sample inputs on the GHCi shell:
*ABCParser> parse "abc"
"*** Exception: parse error
*ABCParser> parse "a"
"*** Exception: parse error
*ABCParser> parse ""
"*** Exception: parse error
*ABCParser> parse "aabbcc"
"*** Exception: parse error

What is the problem?

The happy grammar is here:

{
module ABCParser (parse) where
}

%tokentype { Char }

%token a { 'a' }
%token b { 'b' }
%token c { 'c' }
%token newline { '\n' }

%attributetype { Attrs a }
%attribute value { a }
%attribute len   { Int }

%name parse abcstring

%%

abcstring
   : alist blist clist newline
        { $$ = $1 ++ $2 ++ $3
        ; $2.len = $1.len
        ; $3.len = $1.len
        }

alist
   : a alist
        { $$ = $1 : $2
        ; $$.len = $2.len + 1
        }
   |    { $$ = []; $$.len = 0 }

blist
   : b blist
        { $$ = $1 : $2
        ; $2.len = $$.len - 1
        }
   |    { $$ = []
        ; where failUnless ($$.len == 0) "blist wrong length"
        }

clist
   : c clist
        { $$ = $1 : $2
        ; $2.len = $$.len - 1
        }
   |    { $$ = []
        ; where failUnless ($$.len == 0) "clist wrong length"
        }

{
happyError = error "parse error"
failUnless b msg = if b then () else error msg
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120402/1edb7dd4/attachment.htm>


More information about the Beginners mailing list