I'm trying to understand Alex and lexers in general but I'm having trouble to run my lexer.
I wrote lexers in "basic" and "posn" wrappers but I couldn't in "monad" wrapper. I think I have to use monad
wrapper because I need to collect strings and token positions in input. I also need multiple states. For now I'm trying to run this simple exmaple:
{
module Main (main) where
}
%wrapper "monad"
$whitespace = [\ \b\t\n\f\v\r]
$digit = 0-9
$alpha = [a-zA-Z_]
$upper = [A-Z]
$lower = [a-z]
@tidentifier = $upper($alpha|_|$digit)*
@identifier = $lower($alpha|_|$digit)*
tokens :-
$whitespace+ ;
$upper $alpha+ { typeId }
$lower $alpha+ { id_ }
$digit+ { int }
{
data Lexeme = L AlexPosn LexemeClass String
data LexemeClass
= TypeId String
| Id String
| Int Int
| EOF
deriving (Show, Eq)
typeId :: AlexInput -> Int -> Alex Lexeme
typeId = undefined
id_ :: AlexInput -> Int -> Alex Lexeme
id_ = undefined
int :: AlexInput -> Int -> Alex Lexeme
int = undefined
alexEOF = return (L undefined EOF "")
main :: IO ()
main = do
s <- getContents
let r = runAlex s $ do
return alexMonadScan
print r
}
My actions are undefined
for now. When I try to compile it, I'm getting this error:
➜ haskell ghc --make Tokens.hs
[1 of 1] Compiling Main ( Tokens.hs, Tokens.o )
templates/wrappers.hs:208:17:
Couldn't match expected type `(AlexPosn, Char, [Byte], String)'
with actual type `(t0, t1, t2)'
Expected type: AlexInput
Actual type: (t0, t1, t2)
In the return type of a call of `ignorePendingBytes'
In the first argument of `action', namely
`(ignorePendingBytes inp)'
I'm also getting various errors when I try to compile examples in Alex's github repo, could it be related with a version mismatch? I've installed alex from cabal with ghc 7.0.4. Any ideas?