我试图总体上理解 Alex 和词法分析器,但我无法运行我的词法分析器。
我在“basic”和“posn”包装器中编写了词法分析器,但我不能在“monad”包装器中。我想我必须使用monad
包装器,因为我需要在输入中收集字符串和令牌位置。我还需要多个状态。现在我正在尝试运行这个简单的例子:
{
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
}
我的行动是undefined
暂时的。当我尝试编译它时,我收到了这个错误:
➜ 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)'
当我尝试在 Alex 的 github 存储库中编译示例时,我也遇到了各种错误,这可能与版本不匹配有关吗?我已经使用 ghc 7.0.4 从 cabal 安装了 alex。有任何想法吗?