Hi, I have this code in C for Flex/Lex, the only problem is that as I try to compile it it shows some errors and I dont know how to fix them.

letter [a-zA-Z]  
digit[0-9]
%%  
 {digit}+("E"("+"|"-")?{digit}+)? printf("\n%s\tis real number",yytext);  
 {digit}+"."{digit}+("E"("+"|"-")?{digit}+)? printf("\n%s\t is floating pt no ",yytext);  
 "if"|"else"|"int"|"char"|"scanf"|"printf"|"switch"|"return"|"struct"|"do"|"while"|"void"|"for"|"float" printf("\n%s\t is keywords",yytext);  
 "\a"|"\\n"|"\\b"|"\t"|"\\t"|"\b"|"\\a" printf("\n%s\tis Escape sequences",yytext);  
 {letter}({letter}|{digit})* printf("\n%s\tis identifier",yytext);  
 "&&"|"<"|">"|"<="|">="|"="|"+"|"-"|"?"|"*"|"/"|"%"|"&"|"||" printf("\n%s\toperator ",yytext);  
 "{"|"}"|"["|"]"|"("|")"|"#"|"'"|"."|"\""|"\\"|";"|"," printf("\n%s\t is a special character",yytext);  
 "%d"|"%s"|"%c"|"%f"|"%e" printf("\n%s\tis a format specifier",yytext);  
 %%  
 int yywrap()  
 {  
 return 1;  
 }  
 int main(int argc,char *argv[])  
 {  
 yyin=fopen(argv[1],"r");  
 yylex();  
 fclose(yyin);  
 return 0;  
 }

and the compiling problems I have are:

lexem.l: In function ‘yylex’:
lexem.l:4: error: ‘digit’ undeclared (first use in this function)
lexem.l:4: error: (Each undeclared identifier is reported only once
lexem.l:4: error: for each function it appears in.)
lexem.l:4: error: expected ‘;’ before ‘}’ token
lexem.l:4: error: invalid operands to binary | (have ‘char *’ and ‘char *’)
lexem.l:4: error: called object ‘"E"’ is not a function
lexem.l:4: error: expected expression before ‘{’ token
lexem.l:4: error: expected ‘:’ before ‘;’ token
lexem.l:5: error: expected ‘;’ before ‘}’ token
lexem.l:5: error: wrong type argument to unary plus
lexem.l:5: error: expected ‘;’ before ‘{’ token
lexem.l:6: error: invalid operands to binary | (have ‘char *’ and ‘char *’)
lexem.l:6: error: expected ‘;’ before ‘printf’
lexem.l:7: error: invalid operands to binary | (have ‘char *’ and ‘char *’)
lexem.l:7: error: expected ‘;’ before ‘printf’
lexem.l:8: error: ‘letter’ undeclared (first use in this function)
lexem.l:8: error: expected ‘;’ before ‘}’ token
lexem.l:8: error: expected ‘;’ before ‘}’ token
lexem.l:8: error: expected ‘)’ before ‘|’ token
lexem.l:9: error: invalid operands to binary | (have ‘char *’ and ‘char *’)
lexem.l:9: error: expected ‘;’ before ‘printf’
lexem.l:10: error: invalid operands to binary | (have ‘char *’ and ‘char *’)
lexem.l:10: error: expected ‘;’ before ‘printf’
lexem.l:11: error: invalid operands to binary | (have ‘char *’ and ‘char *’)
lexem.l:11: error: expected ‘;’ before ‘printf’
lexem.l:12: error: expected expression before ‘%’ token
lexem.l:23: error: stray ‘#’ in program

note that I am compiling using the "gcc -lfl lex.yy.c" command line, and thats where all the problems appear.

The syntax in this program you've written is completely wrong. It doesn't even appear to be real C code. What do you think the lines letter [a-zA-Z] and digit[0-9] do? What's with the two percent signs on the next line?

My apologies; the phrase "I have this code in C" threw me off and led me to beleive the OP was feeding the posted code directly into GCC.

This article has been dead for over six months. Start a new discussion instead.