Search code examples
flex-lexer

Flex not parsing variable names properly


I'm new to using Flex. Here's my lex file:

%{
    #include <stdio.h>
%}

%%

\w[\w\d]+    printf("WORD\n");
.            printf("OTHER\n");

%%

int main() {
    yylex();
    return 0;
}

I then compile this with

flex lexfile.l && gcc lex.yy.c -ll

However, after running

echo "hello" | ./a.out

I get

UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN

Why wouldn't I get

WORD

?


Solution

  • Ah. Looks like Flex's version of regex doesn't understand \w and \d. Replaced with

    [A-Za-z][A-Za-z0-9]+
    

    and it worked fine.