Alex problem, help needed!!!!!!!!! ASAP :)

Dear list, I am trying to make a compiler and we are having a hard time getting Alex to work. We have succeded to work out Alex using older version, but with the the 2.2 version we keep getting this error and we havent been able to figure it out. So this is our tokens definition: { module Lexico where import Alex } %wrapper "posn" $digit = 0-9 -- digits $alpha = [a-zA-Z] -- alphabetic characters tokens :- $white+ ; --Los espacios en blanco los omito \/\/.* ; --Lo que venga después de dos barras omito \/\*.*\*\/ ; --Lo que está entre las barras de comentario omito $digit+ { \p s -> TokenEntero p (read s) } \' [$alpha $digit \_]* \' { \p s -> TokenString p (read s)} [$digit]+\.[$digit]+ { \p s -> TokenDouble p (read s) } $alpha [$alpha $digit \_]* { \p s -> TokenVar p (read s) } And when we call the alexScanTokens "hello" we get this error: [TokenVar (AlexPn 0 1 1) "*** Exception: Prelude.read: no parse So we are concerned about the "TokenVar p (read s)" . Would that be the way to read a string? We just get this error with TokenVar and TokenString, whenever we call the function like: alexScanTokens 19 , for instance we dont get any problems and the token 19 is recognized ok. Please, any ideas on what am i doing wrong???????? regards, L _________________________________________________________________ Keep your friends updated—even when you’re not signed in. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/soc...

del_31416no:
Dear list,
I am trying to make a compiler and we are having a hard time getting Alex to work. We have succeded to work out Alex using older version, but with the the 2.2 version we keep getting this error and we havent been able to figure it out.
So this is our tokens definition:
{ module Lexico where import Alex }
%wrapper "posn"
$digit = 0-9 -- digits $alpha = [a-zA-Z] -- alphabetic characters
tokens :-
$white+ ; --Los espacios en blanco los omito \/\/.* ; --Lo que venga despu s de dos barras omito \/\*.*\*\/ ; --Lo que est entre las barras de comentario omito $digit+ { \p s -> TokenEntero p (read s) } \' [$alpha $digit \_]* \' { \p s -> TokenString p (read s)} [$digit]+\.[$digit]+ { \p s -> TokenDouble p (read s) } $alpha [$alpha $digit \_]* { \p s -> TokenVar p (read s) }
And when we call the alexScanTokens "hello" we get this error:
[TokenVar (AlexPn 0 1 1) "*** Exception: Prelude.read: no parse
So we are concerned about the "TokenVar p (read s)" . Would that be the way to read a string?
Use readMaybe rather than read, and check the error. maybeRead :: Read a => String -> Just a maybeRead s = case reads s of [(x, s')] | all isSpace s' -> Just x _ -> Nothing

$digit+ { \p s -> TokenEntero p (read s) } \' [$alpha $digit \_]* \' { \p s -> TokenString p (read s)} [$digit]+\.[$digit]+ { \p s -> TokenDouble p (read s) } $alpha [$alpha $digit \_]* { \p s -> TokenVar p (read s) }
You are using 'read' in slightly strange ways here. What read does is to take a string of characters that supposedly represents a value of some type, and tries to create a value of said type from the string. On the first and third lines you read a sequence of digits to an integer resp double, which is fine, the strings you pass to 'read' are representations of integers and doubles resp. On the second line you're trying to read something of the form 'hello', i.e. surrounded by single-quotes, as a Haskell String. But this doesn't represent a Haskell String, which is what 'read' expects. Haskell Strings have double-quotes, so there will be no proper 'read', if you want single-quotes you need to define your own function to do the conversion (hint: init . tail). On the last line, which is the one giving you the error here, you're again trying to 'read' a string of characters into a String. And again, Haskell Strings are surrounded by double-quotes, so 'read' will not find a representation. This time the fix is even simpler. :-) Prelude> read "hello" :: String "*** Exception: Prelude.read: no parse Prelude> read "\"hello\"" :: String "hello" Cheers, /Niklas
participants (3)
-
Don Stewart
-
María Laura
-
Niklas Broberg