1
votes

I am writing a simple scanner in flex. I want my scanner to print out "integer type seen" when it sees the keyword "int". Is there any difference between the following two ways?

1st way:

%%
int printf("integer type seen");
%%

2nd way:

%%
"int" printf("integer type seen");
%%

So, is there a difference between writing if or "if"? Also, for example when we see a == operator, we print something. Is there a difference between writing == or "==" in the flex file?

1

1 Answers

3
votes

There's no difference in these specific cases -- the quotes(") just tell lex to NOT interpret any special characters (eg, for regular expressions) in the quoted string, but if there are no special characters involved, they don't matter:

[a-z]     printf("matched a single letter\n");
"[a-z]"   printf("matched the 5-character string '[a-z]'\n");
0*        printf("matched zero or more zero characters\n");
"0*"      printf("matched a zero followed by an asterisk\n");

Characters that are special and mean something different outside of quotes include . * + ? | ^ $ < > [ ] ( ) { } /. Some of those only have special meaning if they appear at certain places, but its generally clearer to quote them regardless of where they appear if you want to match the literal characters.