How to match newlines with Alex/Haskell
I borrowed the example presented here
http://www.haskell.org/alex/doc/html/introduction.html
I am trying to make an interpreter for numerical expressions. (literals only, no variables) And I want to have newlines separate two different expressions. Unfortunaltely, when I do this
$ignoredWhite = [tfvr] -- ignored whitespace
$newline = "n" --new line
Alex (v 2.2) aborts compilation. I have tried
$newline = n --new line
but that just causes the abort to move from alex to final binary (aka one generated by ghc), when it is reading the input.
How do I go about fixing this?
This question is really old, nevertheless I'll post my answer for future visitors. When the tokens are defined, I tell alex to process all whitespace:
tokens :-
$white {p s -> checkWhite p $ head s}
checkWhite
looks like this:
checkWhite :: AlexPosn -> Char -> Token
checkWhite p 'n' = TNewLine p
checkWhite p _ = TEmpty
As you can see, it outputs a special token when newline is encountered. To delete TEmpty
from output, you can use ... filter (= TEmpty) $ alexScanTokens ...
.
只是猜测 - 你有没有试过把它包括在一个字符范围内?
$newline = [n]
链接地址: http://www.djcxy.com/p/65630.html
上一篇: 包装模板中的错误