Search code examples
regexflex-lexer

Definitions in flex lexical analyzer - How to define scientific notation


I'm new to using flex and need to define real literal numbers, ie. 3.1 or 3.0e-10 as acceptable numbers. Here is what I have so far:

digit       [0-9]
int     {digit}+
real_literal  ({digit}+)("."{digit}*)

From my understanding this works for decimals without accepting something like 12.52.23

How would I define numbers that accept scientific notation, such as 3.0e-10 like mentioned above?

Would it be something like this?

real_literal  ({digit}+)("."{digit}*)|({digit}+)("."{digit})[Ee]["-""+"]{digit}+

Solution

  • A possible solution, a bit shorter than yours, would be:

    {digit}+"."{digit}*([eE][-+]?{digit}+)?
    

    Break down

    • {digit}+"."{digit} matches positive real numbers (it won't recognize integers).
    • (...)? enclosing the next part in parenthesis followed by a question mark makes what's inside optional.
    • [eE][-+]?{digit}+ match the exponent. Note that the - is not escaped within squared brackets in this case because it's the first or last character of the list, as @rici pointed out.

    I just want to point out that you're not recognizing negative numbers here, but I don't know if that's intentional.