FLEX lexical analyzer issue

Hello experts,

I am doing an assignment with Flex and Visual Studio 2008.

The analyzer should recognize Integers and Octals. Octals should start with backslash \ and should not occupy more than one byte in the memory. For that, I think octal values should be between 0 and 377 (which corresponds to 255 in decimal).

I have written these pattern definitions in the FLEX scanner file:

OCTAL            0$|[1-3][0-7]{0,2}$
INTEGER            [+-]*0|[+-]*[1-9][0-9]*


{INTEGER}                              printf("INTEGER");
\\{OCTAL}                              printf("OCTAL");
\\^{OCTAL}                              printf("ERROR");

When I input 3422, it prints INTEGER
When I input \324,  it prints OCTAL

The problem, when I input \567, it prints \INTEGER, while it should print ERROR
Another problem, when I input 12 (doesn't start with backslash) it prints (OCTAL), while it should print INTEGER

Any help solving this?
Who is Participating?
käµfm³d 👽Commented:
You don't have any anchors on your INTEGER pattern, so, assuming FLEX's regex engine is similar to most engines, the pattern will match if *any* part of the input string matches the pattern. The INTEGER pattern overlooks the backslash because it doesn't have anything that describes whether that character is important; it then sees the 5, which satisfies the [1-9]; then it sees 6, and subsequently 7, which both satisfy [0-9]*. The short of it is, add anchors to your pattern. You might try ^ and $.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.