samedi 10 octobre 2015

Lexer in Haskell - How to Pattern Match specific case?

I'm currently working on a lexer written in Haskell, and am almost finished, but am running into a problem for a special case token. Currently, my lexer takes an input string and breaks down the statement into tokens for numbers, variable names, and specific tokens such as "if", "else", and "then".

It works great for all of my tokens, except for one that is "000...".

I was taught to use the span function, so I have my lexer use the isDigit and isAlphaNum boolean functions to parse the input. However, because "000..." starts with a zero, it automatically returns as a number. Additionally, the period is a token in the grammar as well, so the result of inputting "000..." in my lexer currently results in "0" "." "." ".".

I'm not proficient in the language of Haskell, but is it possible to match a string using isPrint, and use cases to handle instances of strings and integers? I'm at a loss for words right now, and it seems everything I have tried broke my program. My current pattern matching part looks like this:

lexer (c:cs)
| isSpace c = lexer cs
| isDigit c = lexDigit (c:cs)
| isAlphaNum c = lexString (c:cs)
| True = InvalidToken c : lexer cs

lexString 
| s1 == "if" = IfToken : lexer s2 
| s1 == "else" = ElseToken : lexer s2 
| s1 == "then" = ThenToken : lexer s2 
| s1 == "000..." = Zero : lexer s2
| True = StringToken s1 : lexer s2 
where (s1,s2) = (span isAlphaNum cs)

Any help is appreicated!

Aucun commentaire:

Enregistrer un commentaire