Hello,<br>Greeting everybody.<br>I am a beginner of Haskell. I am trying out the happy.<br>When I compiled the happy grammar file for the ABCParser given at <a href="http://www.haskell.org/happy/doc/html/sec-AttributeGrammarExample.html">http://www.haskell.org/happy/doc/html/sec-AttributeGrammarExample.html</a><br>
I did get a Haskell source file.<br>Then I loaded this file, named "abc.hs" into ghci.<br>There is a function called "parse" available to us.<br>GHCi shows its type to be <br><span style="font-family:courier new,monospace">parse :: [Char] -> [Char]</span><br>
<br>But no matter whatever input I give to the parse function, it always gives the following error:<br><span style="font-family:courier new,monospace">"*** Exception: parse error</span><br><br>Here are some sample inputs on the GHCi shell:<br>
<span style="font-family:courier new,monospace">*ABCParser> parse "abc"</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">"*** Exception: parse error</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">*ABCParser> parse "a"</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">"*** Exception: parse error</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">*ABCParser> parse ""</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">"*** Exception: parse error</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">*ABCParser> parse "aabbcc"</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">"*** Exception: parse error</span><br style="font-family:courier new,monospace">
<br>What is the problem?<br><br>The happy grammar is here:<br><pre class="programlisting">{
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
}</pre><br>