Suzy Tros
Suzy Tros

Reputation: 373

regex for a real number in flex ignoring leading zeros

I have the following sets:

NUMBER  [0-9]+
DECIMAL ("."{NUMBER})|({NUMBER}("."{NUMBER}?)?)
REAL    {DECIMAL}([eE][+-]?{NUMBER})?

and I want my lexer to accept real numbers like: 0.002 or 0.004e-10 or .01

the problem is that I want it ignore the leading zeros but to keep the rest of the number for example:

when I give 000.0002 I want to keep 0.0002 and when I give 0.2e-0100 I want to keep 0.2e-100

So I was thinking something like the atof function but I do not know how to do it exactly.

Any thoughts?

Thanks in advance

Upvotes: 1

Views: 525

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54525

lex will return the complete token that your pattern matches as one string. You cannot change that. At the expense of considerable complexity you could use start conditions to match a leading zero (which may be the only digit), and collect tokens for the pieces, e.g.,

0.2e-0100

as

0.2e-
0
100

and glue the first/last tokens together but you would find it much simpler to develop your own string function which filters out the unwanted leading zeroes.

Upvotes: 1

Related Questions