0
votes

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!
2

2 Answers

0
votes

The problem is the double backslashes in the DELIMITER rule. They should be single. At present you are considering \ (specified multiple times), space, t, and n as delimiters.

-1
votes

Solved it. For anyone who has the same problem try adding all whitespace characters in the DELIMITER section. \r worked for me

DELIMITER   [ \t\r]+