I've written a Lex program for identifying tokens but while I'm running it it's showing an error

Viewed 26

My Code:

%{
    #include<stdio.h>
%}

%%

"int"|"char"|"double"|"void"|"main" {printf("\n%s is a keyword",yytext);}

^[a - z A - Z _][a - z A - Z 0 - 9 _] * {printf("\n%s is an identifier",yytext);}

^[-+]?[0-9]* {printf(}\n%s is an integer",yytext);}

^[-+]?[0-9]*[.][0-9]+$ {printf("\n%s is a floating number",yytext);}

"+"|"-"|"*"|"/"|"%"|"//" {printf("\n%s is an arithmetic operator",yytext);}

">"|"<"|">="|"<="|"="|"=="|"<>" {printf("\n%s is a relational operator",yytext);}

.;

%%

int yywrap()
{
  return 1;
}

main()
{
  printf("Enter a String:\n");
  yylex();
}

This is the Code I've written. I'm getting an error while compiling it and I'm not sure what it is. Can you please take a look at it and say how to fix it. Thank you.

1 Answers

Your code doesn't compile because there is a missing " in line ^[-+]?[0-9]* {printf(}\n%s is an integer",yytext);} (at the beginning of the printf's argument).

Flex says it found an EOF which means "End Of File". It found an "opening" " and it was looking for a matching closing " but it encountered end of the file first.


You should also pay attention to whitespaces in lex patterns. Those two are not two are not equivalent:

^[a - z A - Z _][a - z A - Z 0 - 9 _] *
^[a-zA-Z_][a-zA-Z0-9_]*
Related