user1019517
user1019517

Reputation:

Lex/Flex Scanner Isn't Scanning and I Have No Idea Why

I have written a simple lex scanner in the file myscanner.l, where testlex.h is just a bunch of #defines as integers (MATCH_0 == 0, etc)

%{
#include "testlex.h"
%}

%%

"dinky"         return MATCH_0;
"pinky"         return MATCH_1;
"stinky"        return MATCH_2;
[ \t\n]         ;
.               printf("unexpected character\n");

%%

int yywrap(void)
{
    return 1;
}

After using lex to create the lex.yy.c file, I implement the code using this C file

#include <stdio.h>
#include "myscanner.h"

extern int yylex();
extern int yylineno;
extern char* yytext;

int main(void)
{
    int l = yylex();
    while (l)
    {
        printf("%d\n", l);
        l = yylex();
    }
    return 0;
}

When I pass it this input stream: dinky pinky stinky stinky pinky dinky, there is absolutely no output. The output I am expecting looks like this:

0
1
2
2
1
0

Not even "unexpected character". I know my stack is set up right because I've compiled others' examples and they all scan correctly, but for some inconceivable reason my code _will_not_scan_!

What am I missing?

Upvotes: 0

Views: 375

Answers (2)

meaning-matters
meaning-matters

Reputation: 22946

Looking at your expected output, what you see is the simple result of defining "dinky" -> MATCH_0 as 0.

The first value of l now becomes 0, after having scanned dinky. So while(l) is while(0) and the block is not even executed once. Subsequently your main immediately returns 0.

So don't define any tokens as 0, and then write:

int main(void)
{
    int token;
    while (token = yylex())
    {
        printf("%d\n", token);
    }

    return 0;
}

To be honest I'm surprised you did not find this yourself. Simply trying other input would immediately have giving a clue. And, it should be easy to find that yylex() returns 0 at EOF.

BTW, I think it's better to not use l as variable name as it's almost the same as 1.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

The reason why your code does not print anything is that your first input happens to be "dinky", which returns MATCH_0. According to your expected output, MATCH_0 is zero. Therefore, the code will exit right away, before entering the loop even once.

Re-defining MATCH_0 to 1, MATCH_1 to 2, and so on will fix this problem.

Upvotes: 0

Related Questions