So I've been having a weird problem with my code. While the Lexical Analyzer successfully recognizes all the tokens, when reading from an input text file, it doesn't recognize the new lines (stays on Line 1) and after every successful token recognition it gives me a weird error. Here's my code:
%option noyywrap
%x error
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "token.h"
int line = 1;
void ERROR (const char *msg);
%}
// TMHMA ORISMWN
//-----------------------------------
DELIMITER [\ \\t\\n]+
INTEGER [+-]*(([1-9][0-9]*)|([0]))
FLOAT [+-]?(((0+|[1-9][0-9]*)\.[0-9]+)|((0+|[1-9][0-9]*)\.)|\.[0-9]+)
([eE][+-]?[0-9]+)?
STRING (\'[^\']*\'|\"[^\"]*\")
IDENTIFIER [a-zA-Z_][a-zA-Z0-9_]*
AROPERATOR [-+*/%=]
COMPERATOR [<=>]
COMMENT ((#)[^\n#]*|(""")[\w\W]*?("""))
%%
// TMHMA KANONWN
//-----------------------------------
\n { line++; printf("\t#eol#\n");}
{DELIMITER} { }
{INTEGER} { return INTEGER; }
{FLOAT} { return FLOAT; }
{STRING} { return STRING; }
{IDENTIFIER} { return IDENTIFIER; }
{AROPERATOR} { return AROPERATOR; }
{COMPERATOR} { return COMPERATOR; }
{COMMENT} { return COMMENT; }
<<EOF>> { printf("#End of file#\n"); exit(0); }
. {ERROR("\n+Error! Can't recognise characters!\n"); BEGIN(error);}
<error>[ \n\t] {BEGIN(0);}
<error>. {}
%%
void ERROR(const char *msg)
{
fprintf(yyout, "\tFlex -> ERROR, line %d at lexeme \'%s\' : %s\n",line,
yytext, msg);
}
char *tname[8] = {"DELIMITER","INTEGER","FLOAT","STRING","IDENTIFIER","AROPERATOR","COMPERATOR","COMMENT"};
int main(int argc, char **argv){
int token;
if(argc == 3)
{
if(!(yyin = fopen(argv[1], "r")))
{
fprintf(stderr, "Cannot read file: %s\n", argv[1]);
return 1;
}
if(!(yyout = fopen(argv[2], "w")))
{
fprintf(stderr, "Cannot create file: %s\n", argv[2]);
return 1;
}
}
else
if(argc == 2)
{ if(!(yyin = fopen(argv[1], "r")))
{fprintf(stderr, "Cannot read file: %s\n", argv[1]);
return 1;
}
}
while( (token=yylex()) >= 0)
{
fprintf(yyout, "\tLine=%d, token=%s, value=\"%s\"\n", line, tname[token-1], yytext);
}
return 0;
}
And this is the error:
Flex -> ERROR, line 1 at lexeme '' : +Error! Can't recognise characters!