[erlang-questions] lexer for IEEE488.2 numbers

Mikael Pettersson mikpelinux@REDACTED
Sat Apr 27 10:30:47 CEST 2019


On Thu, Apr 25, 2019 at 6:04 PM <dieter@REDACTED> wrote:
>
> Hi all,
>
> I am writing a lexer for a SCPI parser. The number format of 488.2 is more tolerant than
> list_to_float/1, it seems.
> .123 and 456. are both valid in 488.2, but list_to_float/1 rejects them.
>
> I have not yet found an erlang alternative to list_to_float/1,
> my current approach is to add the omitted zeroes and feed the patched string to list_to_float/1.
>
> This works if the number is only a mantissa, but
> it gets a bit more complicated when there is also an exponent, like 123.e11
> my leex expression for the number looks like
>
> {MANTISSA}{EXPONENT} : {token, {float, TokenLine, i488tofloat(TokenChars)}}.
>
> With this approach, I have to parse the string in my function a second time, which is not elegant at all.
>
> Is there a way to have access to the regex macros (or regex groups) on the left side, like \1, \2?
>
> I already tried to push back zeroes, but immediately ran into the promised consequences from note 4 on
> http://erlang.org/doc/man/leex.html, so I quickly left that alley.
>
> The question might be heretic, but why is list_to_float so strict? Every calculator allows to omit a leading 0 before the decimal point.

list_to_float is there to convert Erlang-style floating-point numbers,
not anyone else's, and since Erlang has some restrictions(*) on its
floats, that's reflected in this function.
This is normal.  C's strtoul() for instance can't convert Erlang-style
Base#Digits numbers.

You have two options: either fix up the string to match Erlang's
syntax before passing it to list_to_integer (which is what you're
already doing) or write your own stand-alone conversion code.  I'd
usually do the latter.

(*) Apart from syntax Erlang's floats are restricted to "finite"
values: infinities and NaNs are not permitted anywhere.  I don't
approve of this restriction, but it's there.



More information about the erlang-questions mailing list