Regular expressions against lexical analyzers in Haskell

I'm starting out with Haskell, and I'm trying to use the Alex tool to create regular expressions, and I'm a little lost; my first inconvenience was compilation. How do I do to compile a file with Alex ?. Then I think that I need to import into my code the modules that alex generates, but I'm not sure. If someone can help me, I will be very grateful!

+4
source share
3 answers

You can specify regex functions in Alex.

Here, for example, is a regular expression in Alex to match floating point numbers:

$space = [\ \t\xa0] $digit = 0-9 $octit = 0-7 $hexit = [$digit AF af] @sign = [\-\+] @decimal = $digit+ @octal = $octit+ @hexadecimal = $hexit+ @exponent = [eE] [\-\+]? @decimal @number = @decimal | @decimal \. @decimal @exponent? | @decimal @exponent | 0[oO] @octal | 0[xX] @hexadecimal lex :- @sign? @number { strtod } 

When we match the floating point number, we send a parsing function to work with this captured string, which we can then wrap and expose to the user as a parsing function:

 readDouble :: ByteString -> Maybe (Double, ByteString) readDouble str = case alexScan (AlexInput '\n' str) 0 of AlexEOF -> Nothing AlexError _ -> Nothing AlexToken (AlexInput _ rest) n _ -> case strtod (B.unsafeTake n str) of d -> d `seq` Just $! (d , rest) 

A good consequence of using Alex for this regex matching is that performance is good because the regex engine is statically compiled. It can also be represented as a regular Haskell library built using cabal. For a complete implementation, see Bytestring-lexing .

The general advice on when to use a lexer instead of regular expression matching is that if you have a grammar for the tokens you are trying to match, as I did for floating point, use Alex. If you do not, and the structure will be more ad hoc, use the regular expression mechanism.

+6
source

Why do you want to use alex to create regular expressions? If all you need to do is match some regular expression, etc., you should look at the regular expression package.

+3
source

If this is a simple regex, you want the API specified in text.regex.base . Then there are implementations of text.regex.Posix , text.regex.pcre and several others. The Haddoc documentation is a bit subtle, however the basics are described in Real World Haskell, chapter 8. That's the SO Question.

+1
source

Source: https://habr.com/ru/post/1313446/


All Articles