• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

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?
1 Solution
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 $.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now