FLEX lexical analyzer issue

Posted on 2011-10-27
Last Modified: 2012-05-12
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?
Question by:Muhajreen
    1 Comment
    LVL 74

    Accepted Solution

    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 $.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now