Reputation: 6418
I'm new to writing lex files and I'm not sure whether I'm formatting my file correctly. I'm trying to capture C keywords from a C source file and print them back out with a format string prefixed.
My attempt is (showing a partial keyword list):
%%
auto|do|goto|short printf("%s%s", "formatting", yytext);
In some examples I've looked at, the words in the first column are surrounded by double quotes. Is that necessary, or have I formatted my lex file correctly to capture the keywords I've listed?
That is, should I instead have written:
"auto"|"do"|"goto"|"short" printf("%s%s", "formatting", yytext);
When are double quotes necessary?
Upvotes: 0
Views: 621
Reputation: 241731
Double quotes are necessary when the pattern text includes a regular expression operator or whitespace. It's probably a good idea to use them whenever the pattern text does not consist solely of letters and digits.
In the case of a simple alphabetic keyword, they are clearly not necessary. It's also not necessary to use them if the pattern text is already escaped with a \, so you can write \n
without quotes.
Actually, double quotes are not necessary. There are several ways of escaping characters, but double quotes have the advantage of being able to quote several consecutive characters. However, the following would be fine as well:
[a][u][t][o]|[d][o]...
which is sometimes used for manual case-insensitivity:
[aA][uU][tT][oO]|[dD][oO]...
Flex allows you to specify case-insensitivity for a pattern segment by using the flag syntax -- (?i:auto|do|goto|short)
-- but that's a flex extension so you'll still see patterns like the above.
Upvotes: 2