I am writing a lexical analyzer here is the code:
%{
#include <stdio.h>
void showToken(char*);
%}
%%
int main(){
void showToken(char* name){
printf("<%s,%s>",name,yytext);
}
return 0;
}
%%
I am getting the following :
~/hedor1>cc -c -o lexical.o lexical.c
lexical.l:40: error: expected identifier or â(â before â%â token
I cant find where is the problem and moreover in the CODE SECTION must I write :
int main(){}
what happens if I don’t write the main function above?
Primary problem
You can only have two
%%lines in a Lex (Flex) analyzer.The programs Lex and Flex simply copy the content of the file after the second
%%verbatim into the generated C code. And C doesn’t like%%at any time.Nitpick
You shouldn’t nest functions inside each other like you’re trying to with:
You need to separate
main()fromshowToken(). (There is a GCC-specific extension that does allow nested functions. Don’t use it.)Also, when you have a line number in an error message, it is helpful to insert a comment to identify the line in the source. Or describe the line that is identified. But we shouldn’t have to count the lines in your code, even if the error is in line 1…well, maybe lines 1-3 aren’t too critical, but there is a fuzzy breakpoint after which identifying the line is important. By the time is has reached the teens, it is close to essential; the first 5 lines probably aren’t crucial; in between (6-12) it’s generally better to indicate the line number.