我试图理解亚历克斯和词法分析者的一般情况,但是我很难运行我的词法分析器.
我用"基本"和"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
}
Run Code Online (Sandbox Code Playgroud)
我的行为是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)'
Run Code Online (Sandbox Code Playgroud)
当我尝试在Alex的github repo中编译示例时,我也遇到了各种错误,它是否与版本不匹配有关?我用ghc 7.0.4从cabal安装了alex.有任何想法吗?
这看起来像是Alex 3.0.1中的一个错误.在处理代码1中的其他一些不相关的问题后,它在2.3.3版中运行良好.问题是生成的代码中的这一行:
ignorePendingBytes (p,c,ps,s) = (p,c,s)
Run Code Online (Sandbox Code Playgroud)
通过遵循生成的代码中的类型,似乎这个函数应该具有类型AlexInput -> AlexInput
,但AlexInput
显然不能同时是3元组和4元组.
这可能是因为AlexInput
两个版本之间的定义发生了变化.
type AlexInput = (AlexPosn, Char, String) -- v2.3.3
type AlexInput = (AlexPosn, Char, [Byte], String) -- v3.0.1
Run Code Online (Sandbox Code Playgroud)
据我所知,正确的代码应该是
ignorePendingBytes (p,c,ps,s) = (p,c,[],s)
Run Code Online (Sandbox Code Playgroud)
并在生成的代码中手动进行此更改,使其在处理其他问题后进行编译.
但是,除非你需要3.0.1中的内容,否则我建议降级直到修复,因为必须维护针对生成的代码的补丁通常比它的价值更麻烦.
1您的代码缺少Show
比如Lexeme
和你也叫return
上alexMonadScan
,这已经是在Alex
单子.