bison – warning, rule cannot be matched in a line with blank spaces

I have this flex file that gets tokens from a file. For some reason i get this strange warning whenever i compile my file. However the file still compiles and i dont get why do i even get this error if the line has nothing on it, am i missing something?

Sorry if the code is too long the code itself isnt really important the important thing is why line 166 gives a warning

error:

$ flex l22_scanner.l
l22_scanner.l:166: warning, rule cannot be matched

Program:

%option c++ prefix="l22_scanner_" outfile="l22_scanner.cpp"
%option stack noyywrap yylineno 8bit debug
%{ 
// make relevant includes before including the parser's tab file
#include <string>
#include <stack>
#include <cdk/ast/sequence_node.h>
#include <cdk/ast/expression_node.h>
#include <cdk/ast/lvalue_node.h>
#include "l22_parser.tab.h"

// don't change this
#define yyerror LexerError

long conv_long(const std::string &s, int base = 10) {
  try {
      long num = std::stol(s,NULL,base);
      if(num != (int) std::stol(std::to_string(num).c_str(), NULL, 10))
        std::cerr << "Overflow detected";
      return num;
  } catch (const std::invalid_argument &e) {
    std::cerr << "invalid integer: " << s << std::endl;
    exit(1);
  } catch (const std::out_of_range &e) {
    std::cerr << "integer out of range: " << s << std::endl;
    exit(1);
  }
}

double conv_double(const std::string &s) {
  try {
    return std::stod(s, NULL);
  } catch (const std::invalid_argument &e) {
    std::cerr << "invalid double: " << s << std::endl;
    exit(1);
  } catch (const std::out_of_range &e) {
    std::cerr << "double out of range: " << s << std::endl;
    exit(1);
  }
}



std::vector<int> indents_at_index;
std::stack<char> indent;
bool termination = false;

int idents_counter(const char* str) {
    int i, counter;
    i = counter = 0;
    while(str[i] != '' && str[i] == ' ') {
        if(str[i] == ' ') {
            counter++;
        }
        i++;
    }
    return counter;

}

%}
%x X_STRING X_COMMENT X_NOINDENT X_CLOSE X_ENDSTR

/*TYPE "int"|"double"|"text"|"void"*/

%%
  yydebug=1; set_debug(1);


^.*$                    {
                            if(indents_at_index.empty()) { 
                                if(idents_counter(yytext) != 0){ 
                                    yyerror("wrong indentation on 1st line");
                                }
                                indents_at_index.push_back(0);
                                yyless(0);
                            }
                            else{
                                int counter = idents_counter(yytext);
                                if(counter < indents_at_index.back()) {
                                    while(counter != indents_at_index.back()) {
                                        if(counter > indents_at_index.back()) {
                                            yyerror("wrong identation");
                                        } else if(counter < indents_at_index.back()) {
                                            indents_at_index.pop_back();
                                            indent.push('}');
                                        }
                                    }
                                    yyless(0);
                                    return ';';

                                } else if(counter == indents_at_index.back()) {
                                    yyless(0);
                                } else if(counter > indents_at_index.back()) {
                                    indents_at_index.push_back(counter);
                                    yyless(0);
                                    return '{';

                                }
                                
                            }
                        }

"..."$                  yy_push_state(X_NOINDENT);
<X_NOINDENT>^[ t]*     yy_pop_state();
<X_NOINDENT>^.          yy_pop_state(); yyless(0);
<X_NOINDENT>n          ;



";".*$                 ; /* ignore comments */


"int"               return tTYPE_INT;
"text"              return tTYPE_TEXT;
"double"            return tTYPE_DOUBLE;
"void"              return tTYPE_VOID;

[-+*/%~<>@?=]        return *yytext;
">="                return tGE; 
"<="                return tLE; 
"=="                return tEQ; 
"!="                return tNE;
"not"               return tNOT;
"and"               return tAND;
"or"                return tOR;
"->"                return tLAMBDA;

"foreign"           return tFOREIGN;
"use"               return tUSE;
"public"            return tPUBLIC;
"var"               return tVAR;

"if"                return tIF;
"then"              return tTHEN;
"elif"              return tELIF; 
"else"              return tELSE;

"while"             return tWHILE;
"do"                return tDO; 
"stop"              return tSTOP;
"again"             return tAGAIN;
"write"             return tWRITE;
"writeln"           return tWRITELN;
"return"            return tRETURN;
"begin"             return tBEGIN; 
"end"               return tEND;
"sizeof"            return tSIZEOF; 
"input"             return tINPUT;
"null"              return tNULL;

[A-Za-z][A-Za-z0-9_]*  yylval.s = new std::string(yytext); return tIDENTIFIER;



"                     yy_push_state(X_STRING); yylval.s = new std::string("");
<X_STRING>"           yy_pop_state(); return tTEXT;
<X_STRING>\"         *yylval.s += '"';
<X_STRING>\\         *yylval.s += '\';
<X_STRING>\n          *yylval.s += 'n';
<X_STRING>\r          *yylval.s += 'r';
<X_STRING>\t          *yylval.s += 't';
<X_STRING>""         yy_push_state(X_STRING);
<X_STRING>\[0-6]{1,3} *yylval.s += (char) conv_long(yytext+1,7);
<X_STRING>\0          yy_push_state(X_ENDSTR);



<X_STRING>           yyerror("nullchar in string");
<X_STRING>.            *yylval.s += yytext;
<X_STRING>n           yyerror("newline in string");

<X_ENDSTR>                 yyerror("null byte in string");
<X_ENDSTR>"                 yy_pop_state(); yy_pop_state(); return tTEXT;
<X_ENDSTR>.|n               ; /* ignore everything after  */


"(*"                    yy_push_state(X_COMMENT);
<X_COMMENT>"(*"         yy_push_state(X_COMMENT);
<X_COMMENT>"*)"         yy_pop_state();
<X_COMMENT>.|n         ; /*ignore comments */

[0-6]+                 yylval.i = conv_long(yytext,7); return tINTEGER;
[0-9]+                 yylval.i = conv_long(yytext); return tINTEGER;

[0-9]+.[0-9]+([Ee][-+]?[0-9]+)?     yylval.d = conv_double(yytext); return tDOUBLE;
[0-9]+([Ee][-+]?[0-9]+)              yylval.d = conv_double(yytext); return tDOUBLE;


[-+*/%()<>={}.?[],@:=] return *yytext;

[ tn]+               ; /* ignore whitespace */

.                      yyerror("Unknown character");

<X_CLOSE><<EOF>>        {   
                            if(!indent.empty()) {
                                char return_value = indent.top();
                                indent.pop();
                                yy_pop_state();
                                yy_push_state(X_CLOSE);
                                return return_value;
                            }
                            else {
                                yy_pop_state();
                                termination = true;
                            }
                        }

<<EOF>>                 {
                            if(termination) {
                                return 0;
                            }
                            int counter = 0;
                            if(counter == indents_at_index.back()){
                                termination = true;
                                return ';'; 
                            }
                            if(counter < indents_at_index.back()) {
                                while(counter != indents_at_index.back()) {
                                    if(counter < indents_at_index.back()) {
                                        indents_at_index.pop_back();
                                        indent.push('}');
                                    }
                                    else if(counter > indents_at_index.back()) {
                                        yyerror("wrong indentation!");
                                    }
                                }
                                indent.push(';');
                            }
                            yy_push_state(X_CLOSE);
                        }

%%

Leave a Comment