1

我刚刚学习了haskell 和alex/happy,因为我想写一个解析器。但是,我遇到了这个问题:

来源:lexer.x

{
module ValkenLexer where

strip :: String -> String
strip s = take ((length s) - 2) (drop 1 s)
}

%wrapper "basic"

$digit = 0-9
$alpha = [a-zA-Z]

tokens :-
  $white+                           ;
  $alpha+                               { \s -> TokIdent s }
  \"[^\"]*\"                       { \s -> TokStr strip(s) }
  =                                     { \s -> TokSlash}
  \|                                    { \s -> TokPipe }
  \/                                    { \s -> TokSlash }
  \n                                    { \s -> TokEol}
  \%$alpha+                              { \s -> TokVar (drop 1 s)}

{

data Token = TokIdent String
           | TokStr String
           | TokEq
           | TokPipe
           | TokSlash
           | TokEol
           | TokVar String
             deriving (Eq,Show)

}

并得到:

\->>> alex lexer.x && ghc lexer.hs
[1 of 1] Compiling ValkenLexer      ( lexer.hs, lexer.o )

lexer.hs:15:1: parse error on input `import'

我究竟做错了什么?

4

1 回答 1

3

正如 Daniel Wagner 预测的那样,函数定义需要放在页脚。您只能将模块声明和导入语句放在标题上。此外,您需要更改TokStr strip(s)TokStr (strip s)

{
module ValkenLexer where
}

%wrapper "basic"

$digit = 0-9
$alpha = [a-zA-Z]

tokens :-
  $white+                           ;
  $alpha+                               { \s -> TokIdent s }
  \"[^\"]*\"                       { \s -> TokStr (strip s) }
  =                                     { \s -> TokSlash}
  \|                                    { \s -> TokPipe }
  \/                                    { \s -> TokSlash }
  \n                                    { \s -> TokEol}
  \%$alpha+                              { \s -> TokVar (drop 1 s)}

{
strip :: String -> String
strip s = take ((length s) - 2) (drop 1 s)

data Token = TokIdent String
           | TokStr String
           | TokEq
           | TokPipe
           | TokSlash
           | TokEol
           | TokVar String
             deriving (Eq,Show)
}
于 2013-10-07T14:27:37.487 回答