FLEX - 在输出文件中出现奇怪的新行错误

时间:2017-12-17 13:42:59

标签: bison flex-lexer lex

所以我的代码遇到了一个奇怪的问题。当词法分析器成功识别所有令牌时,从输入文本文件中读取时,它无法识别新行(保留在第1行),并且在每次成功识别令牌后,它都会给我一个奇怪的错误。这是我的代码:

%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;
}

这就是错误:

Flex -> ERROR, line 1 at lexeme '' : +Error! Can't recognise characters!

2 个答案:

答案 0 :(得分:0)

问题是DELIMITER规则中的双反斜杠。他们应该是单身。目前,您正在考虑\(指定多次),空格,Button btnSubmit = findViewById(R.id.btn_button); btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { float desiredGradeValue = Float.valueOf(desiredGradeInput.getText().toString()); float examWeightValue = Float.valueOf(examWeightInput.getText().toString()); textViewExamMarkNeeded.setText((Float.toString(examMarkNeededValue))); } }); t作为分隔符。

答案 1 :(得分:-1)

解决了它。对于遇到相同问题的任何人,请尝试在DELIMITER部分添加所有空格字符。 \ r为我工作

DELIMITER   [ \t\r]+