Search code examples
compiler-constructionflex-lexerlexical-analysis

The question mark (?) in flex


I cannot understand what the question means in a regular expression in flex. For example, in my notes it says:

[ a-z ]? corresponds to 0 or 1 repetition (used for optional parts).

and another example says:

-?[1-9][ 0-9 ]∗ corresponds to any nonzero integer.

I do not understand why these regular expressions correspond to their explanations. Can somebody explain these two?


Solution

  • The ? isn't specific to flex it is a part of most regular expression engines. It basically means that whatever comes before the ? is optional, so it doesn't actually have to be there.

    Specifically it means "Zero or one of the preceding element".

    Examples:

    Nov(ember)?              // matches November or Nov
    Feb(ruary)? 23(rd)?      // matches Feb 23 or February 23 or February 23rd or Feb 23rd
    colou?r                  // matches color or colour