Typing /0 (not \0) into a lex program causes unexpected behavior

28 Views Asked by At

I made a simple lex program for school, and for some reason it has an unexpected behavior while reading "/0".

The program is as following:

%%
[a-z] printf("char %c", yytext[0]-32);
[A-Z] printf("char %c", yytext[0]+32);
([a-z]+|[A-Z][a-z]+) printf("word %s %lu", yytext, yyleng);
[-][1-9][0-9]* printf("negative integer");
([+][1-9]|[1-9])[0-9]* printf("positive integer");
[-]([1-9][0-9]*|0)[.][0-9]+ printf("negative float");
([+][1-9][0-9]*|[+]0|[1-9][0-9]*|0)[.][0-9]+ printf("positive float");
[-]([1-9][0-9]*|0)[.][0-9]+(e|E)([+]|[-])([0-9]*|0) printf("negative expo");
([+][1-9][0-9]*|[+]0|[1-9][0-9]*|0)[.][0-9]+(e|E)([+]|[-])([1-9][0-9]*|0) printf("positive expo");
[a-zA-Z0-9]+ printf("string");
[ ]+ printf("   ");
. ;
%%

While giving it /0 it identifies it as "string" for unknown reason. I couldn't find any special meaning for this key sequence as its a regular / and not a \.

If I replace the digit 0 with any other digit it gives me an expected behavior of ignoring the / key and identifying the digit as "positive integer".

I'll be glad to understand this unexpected behavior.

1

There are 1 best solutions below

0
Jiraiya On

([+][1-9]|[1-9])[0-9]* printf("positive integer"); This will only accept positive numbers starting with non zero digit, if it's zero, it will go to [a-zA-Z0-9]+ printf("string");

So, if you want 0 or 01 to be accepted as positive number, change the regex for positive number to desired behaviour.