Let me walk you through one way to do this.
As always, the most important part of a token definition
is the regular expression that it corresponds to.
Here we can optionally start with a minus sign.
Then we definitely have one or more digits, 867, 3, 1.
And then there's a big optional part.
We can have a dot and then some number of digits after that.
We could have the dot and then some number of digits.
-1. is okay or 3.14159.
Here we've got 5 trailing digits. Here we've got 0 of them.
And this whole dot followed by digits is optional.
But remember that the dot has special meaning in regular expressions,
so I'm going to need to use a \ to escape it to say
we literally mean the period and not any character.
Then we turn that string into a floating point number.
Then we return it.
Just to walk through these again, this part, the optional minus sign,
catches either this minus sign or the nothing that's in front of this 3.
And this part here is this 1, this 3, or 867.
This dot, literally a dot, is this dot, that dot,
and I think there was one under here, and then this part
is 14159, 5309,
or the nothing that's here after this dot.
では正しい定義の方法をお見せします
トークン定義において最も重要な部分は
それに対応する正規表現です
ここは任意で-から始められます
そのあとに1つ以上の数字を持てるようにします
次の部分も任意となりますが
ドットを持てるようにし そのあと数字が続きます
これでドットと
そのあとに数字を持つことができます
-1.や3.14159も合致します
小数点以下に5桁ある数字と
何もない数字がありますが
ドットとそのあとに続く数字は任意です
正規表現でドットは特別な意味がありましたね
そのため\を使いエスケープしましょう
“任意の文字”の意味にはなりません
文字列を浮動小数点に変換したら
実行してみましょう
おさらいですが この部分は任意の-です
-のある数字とない数字どちらも合致します
1、3、867はこの部分に当てはまります
正規表現にあるこの.は
上記の青で囲まれた3つの.に合致します
正規表現のこの部分に当てはまるのは
ピンクで囲まれた14159と5309
そして1の後ろに当てはまります
Vamos ver uma maneira de fazer isso.
Como sempre, o mais importante de uma definição de token
é a expressão regular que corresponde a ele.
Aqui, podemos opcionalmente começar com um sinal `-',
então temos 1 ou mais dígitos -- 876, 3, 1 --
e então temos essa parte opcional:
podemos ter um ponto e depois alguns números, ou dígitos, depois disso.
Podemos ter `.' e então um certo número de dígitos:
-1 é ok, assim como 3.14159 --
aqui temos 5 dígitos; aqui temos 0.
E isso tudo -- `.' seguido de dígitos -- é opcional.
Mas lembre-se que o ponto tem significado especial em expressões regulares,
e portanto eu vou precisar usar o caractere de escape `\' para
dizer que literalmente me refiro a `.', e não a qualquer caractere
Então convertemos este string como um número de ponto flutuante.
E então retornamos esse valor.
Vamos dar uma olhada mais uma vez: esta parte, o sinal negativo opcional,
casa ou com o sinal negativo, ou com nada, como na frente deste 3.
E esta parte aqui é o 1, o 3, ou 867.
O ponto, literalmente ponto, é este `.', este `.',
e eu achoque existe outro aqui, mas esta parte
é 14159, 5309,
ou nada, neste caso aqui, depois do ponto.