[erlang-questions] lexer for IEEE488.2 numbers

dieter@REDACTED dieter@REDACTED
Thu May 2 09:28:09 CEST 2019


Hello Mikael,
 thank you for the explanation!
 Of course the reasons for the implementation of list_to_float/1 is due to the erlang requirements.
When your mileage varies, you have to walk a bit..
Right now I have implemented my conversion with some regular expressions, as I need this only
for reading configuration definitions, which is not performance-critical.
 kind regards,
 dieter
On 27.04.19 10:30, Mikael Pettersson wrote:
	On Thu, Apr 25, 2019 at 6:04 PM  (mailto: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 (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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190502/788e4c3f/attachment.htm>


More information about the erlang-questions mailing list