haskell alex: parse error on import? -
i've took haskell , alex/happy because wanted write parser. however, ran problem:
source: lexer.x
{ module valkenlexer strip :: string -> string strip s = take ((length s) - 2) (drop 1 s) } %wrapper "basic" $digit = 0-9 $alpha = [a-za-z] tokens :- $white+ ; $alpha+ { \s -> tokident s } \"[^\"]*\" { \s -> tokstr strip(s) } = { \s -> tokslash} \| { \s -> tokpipe } \/ { \s -> tokslash } \n { \s -> tokeol} \%$alpha+ { \s -> tokvar (drop 1 s)} { data token = tokident string | tokstr string | tokeq | tokpipe | tokslash | tokeol | tokvar string deriving (eq,show) }
and got:
\->>> alex lexer.x && ghc lexer.hs [1 of 1] compiling valkenlexer ( lexer.hs, lexer.o ) lexer.hs:15:1: parse error on input `import'
what doing wrong?
as daniel wagner predicted, function definitions need go on footer. can put module declaration , import statements on header. additionally, need change tokstr strip(s)
tokstr (strip s)
{ module valkenlexer } %wrapper "basic" $digit = 0-9 $alpha = [a-za-z] tokens :- $white+ ; $alpha+ { \s -> tokident s } \"[^\"]*\" { \s -> tokstr (strip s) } = { \s -> tokslash} \| { \s -> tokpipe } \/ { \s -> tokslash } \n { \s -> tokeol} \%$alpha+ { \s -> tokvar (drop 1 s)} { strip :: string -> string strip s = take ((length s) - 2) (drop 1 s) data token = tokident string | tokstr string | tokeq | tokpipe | tokslash | tokeol | tokvar string deriving (eq,show) }
Comments
Post a Comment