I have to do a software to identify tokens in C. I have this code:
*main = do
x <- readFile "progc.c"
let resultado = lexCmm X
print resultado
lexCmm :: String -> [Tok]
lexCmm X = case X of
c:cs | isSpace c -> lexCmm cs
c:cs | isAlpha c -> getId s
c:cs | isDigit c -> getInt s
c:d:cs | isSymb [c,d] -> TS [c,d] : lexCmm cs
c:cs | isSymb [c] -> TS [c] : lexCmm cs
_ -> []
where
getId s = lx i : lexCmm cs where (i,cs) = span isIdChar s
getInt s = TI (read i) : lexCmm cs where (i,cs) = span isDigit s
isIdChar c = isAlpha c || isDigit c
lx i = if isReservedWord i then TS i else TI i
isSymb s = elem s $ words "++ -- == <= >= ++ { } = , ; + * - ( ) < >"
isReservedWord w = elem w $ words "else if int main printInt return while"*
The error is:
file:{Hugs}\prog.hs:7 - Syntax error in input (unexpected `=')
In addition to the mentioned error of using an upper case
Xfor a variable name, you have an indentation error, the defining equation oflexCmmis indented, but it ought to start in the leftmost column.If you unindent that line
and the definitions of
isSymbandisReservedWordtoo, you will get some other errors.The alternatives of the
caseexpression must be indented further than thecasekeyword.In
you use an entity
sthat is not in scope (so you should probably change the argument oflexCmmtos, or these twostox.strongly suggests that the constructor
TIofToktakes anIntargument, buttries to apply it to a
String. You probably wantTI (read i)there.The following code, containing a quick mockup of a
Toktype compiles: