[Haskell-beginners] Attribute Grammar and Type signature with Happy
Julien Lange
jl250 at leicester.ac.uk
Thu May 27 10:56:39 EDT 2010
Hi all,
I am working with a "home-made" language, with a rather big grammar, for
which I've made a parser with Happy.
I am now trying to use attributes as I'd like to add several
typecheck-like rules directly in the grammar.
My problem is that as soon as I add attributes to my grammar, the type
signatures of the rules seem to be wrong. Happy indeed produces a
Haskell file, but GHC complains when I try to compile.
*Is it possible to use type signatures with attribute grammar at all, if
so, what kind of type is legit?*
(I tried to use my grammar without type signature but GHC went mental
and I had to kill -9 it.)
I attached an example, abc.y (adapted from the Happy ones) which behaves
as my grammar. I would like to know what I should put in place of the
"????".
Say, if I give "[Char]" as type for the first rule, GHC says:
--------------------------------------
bc.hs:63:4:
The lambda expression `\ happyInhAttrs -> ...' has one argument,
but its type `[Char]' has none
In the first argument of `HappyAbsSyn4', namely
`(\ happyInhAttrs
-> let
happySelfAttrs = ...
(happyConditions_1, happySubAttrs_1) = happy_var_1
happyEmptyAttrs
....
in (happyConditions, happySelfAttrs))'
(...)
skip skip
(...)
abc.hs:141:54:
Couldn't match expected type `Attrs a -> (t, t1)'
against inferred type `[Char]'
In the expression: f happyEmptyAttrs
In a pattern binding: (conds, attrs) = f happyEmptyAttrs
In the expression:
let
f = do_parse toks
(conds, attrs) = f happyEmptyAttrs
x = foldr seq attrs conds
in (value x)
--------------------------------------
While, when I remove the type (i.e. ":: {[Char]}"), everything works
fine and the parse function is typed as "[Char] -> [Char]".
Oh, and I use:
- ghc-6.8.2
- happy 1.17
Many thanks,
Julien.
--
Julien Lange,
Graduate Teaching Assistant, PhD Student
-----------------------------------------------------
Department of Computer Science,
University of Leicester,
University Road, Leicester LE1 7RH.
T: +44 (0)116 252 3904
http://www.cs.le.ac.uk/people/jl250/
-----------------------------------------------------
-------------- next part --------------
{
module ABCParser 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
}
More information about the Beginners
mailing list