限制整数和十进制零件长度
有什么方法可以在“”之前和之后提取数字数量。在弹性中的浮动中? 我想将整数部分中的数字数量限制在4,而小数零件的数字为8。因此,包括''的字符总数。最多将达到13。
这就是我所做的:
仅当浮子长度超过13时,我才会出现错误。但是,当整数部分中有4个以上的字符或十进制部分中的8个以上的字符时,我不会遇到任何错误。
感谢您的帮助。
Is there a way I can extract the number of digits before and after the '.' in a float in Flex?
I want to limit the number of digits in the integer part to 4, and of the decimal part to 8. So that the total of the characters including '.' will be up to 13.
this is what I did:
I get errors only if the float is more than 13 in length. But when it has more than 4 characters in integer part or more than 8 characters in decimal part, I don't get any error.
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在(f)lex中,宏(
{...}
)只是宏。它们被其定义所取代(通常被括号包围,以避免宏扩展的通常问题)。因此,您不能使用{intpart}
和{decpart}
在{float}
模式的子序列上执行操作。要么{float}
(即该宏的宏扩展)匹配,要么其他两个模式之一匹配。这将产生令人困惑的结果,因为您的
{intpart}
模式与您想要的内容不符。您希望它匹配一个不以0开头的0个整数。那将是[1-9] [0-9]*| 0
。模式,([1-9] [0-9])*| 0
匹配0或偶数整数(然后,只有当奇数位置的数字不是0时,只有当数字不是0时)。其他整数将与{decPart}
模式相匹配,该模式也很有效(因为所有规则始终均处于活动状态,除非您使用扫描仪状态)。由于某些整数匹配了其中一个模式和其他整数匹配另一个模式,因此很有可能会应用错误的长度测试。例如,整数
12345
将与{decpart}
模式匹配,并将与长度8进行比较,因此不会触发错误消息。因此,您应该做的第一件事是尝试修复图案,以便它们实际上与您想要的东西相匹配,以记住您需要匹配整个令牌。当然,您可以发送部分令牌,从而使语法变得有些复杂。一种方法是使用类似的东西:
但是,您的解析器将不得不将两件事粘在一起。解析器实际上并不知道两个部分之间是否有空格,因此这将需要更多的工作。 [注意1]
就个人而言,我只是在检查比赛,然后进行检查,以确保这两个部分都太长了。或将整个内容转换为浮点数,并将其与10000进行比较,这无疑是最简单的选择。
注意:
。
的存在来调节所有这些。因此,从技术上讲,您可以做自己想做的事。但这几乎没有目的是很多工作,其结果将是很难维护的代码。In (f)lex, macros (
{...}
) are just macros; they're replaced with their definition (normally surrounded by parentheses to avoid the usual problem with macro expansion). So you can't use{IntPart}
and{DecPart}
to perform actions on subsequences in the{float}
pattern. Either{float}
(that is, the macro expansion of that macro) matches, or one of the other two patterns match.That's going to have confusing results because your
{IntPart}
pattern does not match what you want it to. You want it to match either a 0 or an integer which doesn't start with 0. That would be[1-9][0-9]*|0
. The pattern,([1-9][0-9])*|0
matches 0 or integers of even length (and then, only if the digits in odd positions are not 0). Other integers will be matched by the{DecPart}
pattern, which is also active (because all rules are always active, unless you use scanner states).Since some integers match one of those patterns and other integers match the other one, it's quite possible that the wrong length test will be applied. The integer
12345
, for example, will match the{DecPart}
pattern and will be compared with the length 8, so it won't trigger an error message. So the first thing you should do is to try to fix your patterns so that they actually match what you want them to, remembering that you need to match the entire token.You could, of course, send off partial tokens, thereby complicating the grammar somewhat. One way to do that would be to use something like this:
but then your parser will have to stick the two things together. And the parser doesn't actually know whether there was whitespace between the two parts, so that's going to need some more work. [Note 1]
Personally, I'd just do the match and later on check to make sure that neither part is too long. Or convert the whole thing to a floating point number and compare it with 10000, which is undoubtedly the simplest option.
Notes:
.
. So technically, you could do what you want. But it's a lot of work for little purpose, and the consequence will be code which is much harder to maintain.