【发布时间】:2013-12-17 10:04:37
【问题描述】:
我正在尝试学习使用 Alex + Happy 构建解析器,特别是我对学习使用 Alex 的 monad 包装器感兴趣。我已经查看了 Alex 和 Happy 的文档,但对我来说,他们都真的缺乏关于一起使用它们的任何有用信息。我设法让它们与basic 和posn 包装器一起工作,但我对monad 不知所措。
我已经在 SO 上查看过关于 Alex、Happy 和 monadic 词法分析器的不同问题(包括:Are there any tutorials on building a simple interpreter using Alex + Happy?,但没有一个能够提供使用 monad 的简单示例。
大部分在线代码使用 Happy 和自定义词法分析器函数,或使用 basic 或 posn Alex 包装器。
这是一个类似 ini 语法的简单词法分析器:
{
module IniLexer where
}
%wrapper "monad"
$spaces = [\ \t]
$alpha = [a-zA-Z]
$digits = [0-9]
$alnum = [$alpha$digits]
@identifier = $alpha $alnum*
@comment = \#.*
@integer = $digits+
@boolean = (true) | (false)
@string = \"[^\"]*\"
:-
@integer { mkL LInteger }
@boolean { mkL LBoolean }
@string { mkL LString }
@identifier { mkL LIdentifier }
\[@identifier\] { mkL LSection }
= { mkL LAssign }
\; { mkL LEndAssign }
@comment ;
[\ \t \n]+ ;
{
data LexemeClass = LInteger | LBoolean | LString | LIdentifier | LSection | LAssign | LEndAssign | LEOF
deriving (Eq, Show)
mkL :: LexemeClass -> AlexInput -> Int -> Alex Token
mkL c (p, _, _, str) len = let t = take len str
in case c of
LInteger -> return (IntegerNum ((read t) :: Integer) p)
LBoolean -> return (BooleanVal (if t == "true"
then True
else False
) p)
LString -> return (StringTxt (take (length t - 2) (drop 1 t)) p)
LIdentifier -> return (Identifier t p)
LSection -> return (SectionHeader (take (length t - 2) (drop 1 t)) p)
LAssign -> return (Assignment p)
LEndAssign -> return (EndAssignment p)
-- No idea why I have to write this myself. Documentation doesn't mention it.
alexEOF :: Alex Token
alexEOF = return Eof
data Token = SectionHeader {identifier :: String, position :: AlexPosn} |
Identifier {name :: String, position :: AlexPosn} |
Assignment {position :: AlexPosn} |
EndAssignment {position :: AlexPosn} |
IntegerNum {value :: Integer, position :: AlexPosn} |
BooleanVal {istrue :: Bool, position :: AlexPosn} |
StringTxt {text :: String, position :: AlexPosn} |
Eof
deriving (Eq, Show)
}
这里是相对的 Happy 解析器:
{
module Main where
import IniLexer
}
%name parseIniFile
%error {parseError}
%lexer {alexMonadScan} {AlexEOF}
%monad {Alex}
%tokentype {Token}
%token
SECTION {SectionHeader name _ }
IDENT {Identifier name _ }
'=' {Assignment _ }
INT {IntegerNum value _ }
BOOL {BooleanVal istrue _ }
STRING {StringTxt text _ }
';' {EndAssignment _ }
%%
ConfigFile : SequenceOfSections {reverse $1}
SequenceOfSections : {- empty -} { [] }
| SequenceOfSections Section {$2 : $1}
Section : SECTION SectionBody {Section (identifier $1) (reverse $2)}
SectionBody : {- empty -} {[]}
| SectionBody AssignmentLine ';' {$2 : $1}
AssignmentLine : IDENT '=' Value {(name $1, $3)}
Value : INT {IntV (value $1)}
| BOOL {BoolV (istrue $1)}
| STRING {StringV (text $1)}
{
data Value = IntV Integer | BoolV Bool | StringV String
deriving (Eq, Show)
data Section = Section String [(String, Value)]
deriving (Eq, Show)
data IniFile = IniFile [Section]
deriving (Eq, Show)
parseError :: [Token] -> Alex a
parseError t = fail "a"
main = do
s <- getContents
print $ parseIniFile $ runAlex s alexMonadScan
}
这会引发很多编译器错误:
[...]
Couldn't match expected type `(AlexReturn t1 -> Alex a0) -> t0'
with actual type `Alex Token'
The function `alexMonadScan' is applied to one argument,
but its type `Alex Token' has none
[...]
我应该如何修改解析器以使用alexMonadScan?
Happy 文档根本不清楚,并努力不使用任何澄清示例(或者从我的角度来看,提供的示例未能阐明)。
如果需要,我可以发布我的 posn 版本的同一个词法分析器+解析器。
【问题讨论】:
-
上次我尝试过这个(几年前!),
monad包装器的文档完全错误,而且似乎仍然是错误的。我不记得我必须做些什么才能让它工作,但你最好手动生成包装器代码,例如language-c和haskell-src-exts做。 -
投反对票的人应该解释为什么他认为这是一个糟糕的问题。我相信我确实已经提供了所需的所有信息,包括 MWE 和一切。