2012-05-09 75 views
6

我想了解Alex和詞法分析器一般,但我無法運行我的詞法分析器。哈斯克爾亞歷克斯 - 錯誤的包裝模板

我寫了「基本」和「posn處」包裝詞法分析器,但我不能在「單子」的包裝。我想我必須使用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安裝了cab的alex。有任何想法嗎?

回答

7

這看起來像亞歷克斯3.0.1的錯誤。在處理代碼中的一些其他不相關的問題後,它在2.3.3版本中正常工作。問題是此行中生成的代碼:

ignorePendingBytes (p,c,ps,s) = (p,c,s) 

按照在生成的代碼類型,好像這個功能應該有類型AlexInput -> AlexInput,但AlexInput顯然不能同時是3元組和一個4元組。

這有可能發生,因爲AlexInput的定義是兩個版本之間改變。

type AlexInput = (AlexPosn, Char, String)   -- v2.3.3 
type AlexInput = (AlexPosn, Char, [Byte], String) -- v3.0.1 

從我所知道的,正確的代碼應該是

ignorePendingBytes (p,c,ps,s) = (p,c,[],s) 

,並手動進行此更改在生成代碼使處理其他問題後,編譯。

但是,除非你需要從3.0.1的東西,我建議降級,直到這個是固定的,具有維持對生成的代碼補丁通常是更多的麻煩比它的價值。

您的代碼缺少Lexeme一個Show實例,你還呼籲alexMonadScan,這已經是在Alex單子return

+0

謝謝!我應該在GitHub倉庫中打開一個問題嗎? – sinan

+0

@sinan:是的,這可能是一個好主意。 – hammar