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

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