Victor Brunell
Victor Brunell

Reputation: 6418

Lex: C keyword rules not identifying correctly

I wrote a simple lex file to identify C keywords. My rules looks like:

keyword do|while|char|if
%%
{keyword}  { printf("Keyword %s found.", yytext); }

The problem is the rule correctly identifies char in source code, but it also identifies things like putchar as keyword char. How can I force the rule to only identify the keyword char and not when it's present in other words?

Upvotes: 4

Views: 1700

Answers (2)

Aleksandar Makragić
Aleksandar Makragić

Reputation: 1997

You need to put keywords before identifiers. That's all. Lex is searching for regular expressions sequentially.

%%

IF|ELSE|etc {action for keywords }

[a-zA-Z_][a-zA-Z0-9]* {action for identifiers}

%%

Upvotes: 2

Thomas Dickey
Thomas Dickey

Reputation: 54515

Your lexer has to match other things (including something that will match the "put" substring) to allow it to distinguish between keywords and non-keywords.

If I were writing the lexer, I would simplify it by matching possible identifiers and using a lookup table to identify keywords in the resulting tokens.

Upvotes: 0

Related Questions