Search code examples
c++c++11flex-lexerunordered-set

Flex tokens not working with char* hashtable


I am making a simple compiler, and I use flex and a hashtable (unordered_set) to check if an input word is an identifier or a keyword.

%{
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unordered_set>
using std::unordered_set;
void yyerror(char*);
int yyparse(void);

typedef unordered_set<const char*> cstrset;
const cstrset keywords = {"and", "bool", "class"};
%}
%%
[ \t\n\r\f]             ;
[a-z][a-zA-Z0-9_]*      {   if (keywords.count(yytext) > 0)
                                printf("%s", yytext);
                            else
                                printf("object-identifier"); };

%%

void yyerror(char* str) {printf("ERROR: Could not parse!\n");}
int yywrap() {}

int main(int argc, char** argv)
{
    if (argc != 2) {printf("no input file");}
    FILE* file = fopen(argv[1], "r");
    if (file == NULL) {printf("couldn't open file");}
    yyin = file;
    yylex();
    fclose(file);
    return 0;
}

I tried with an input file that has only the word "class" written, and the output is object_identifier, not class.

I tried with a simple program, without using flex and the unordered_set works fine.

int main()
{
    cstrset keywords = {"and", "class"};
    const char* str = "class";
    if (keywords.count(str) > 0)
        printf("works");
    return 0;
}

What could be the problem?


Solution

  • Use unordered_set<string> instead of your unordered_set<const char*>. You are trying to find the pointer to the char array that obviously cannot exist inside your defined variable.