在Flex中处理数字后跟字母 [英] Handle number followed by letters in Flex
问题描述
在某些Flex/Lex代码中,
DIGIT [0-9]字母[A-Za-z]ID {LETTER} {LETTER} | {DIGIT})*NUM {DIGIT} +(\.{DIGIT} +)?
因此, 123
匹配 NUM
,而 abc123
匹配 ID
.的情况下 123abc
,它匹配 NUM
,后跟和 ID
.然而,我希望它生成一条错误消息而不是匹配消息.
有没有简单的方法可以做到这一点?
您可以添加其他模式来检测错误.
以下内容不使用宏,因为我个人认为它们会分散注意力.但是想法是一样的.
[[:: alpha:]] [[:alnum:]] * {返回IDENTIFIER;}[[:::] +."([[:::] +)?{返回NUMBER;}[[:::] +."([[:::] +)?[[:alpha:]] {返回BAD_NUMBER;}
只有在数字后紧跟一个字母时,最后一个模式才匹配,并且由于最长匹配规则,它将覆盖第二个模式.
顺便说一句,一个更好的数字模式是:
[[:::]] +(." [[:::] *)?|." [[:::]] +
这将与 23.
和 .56
匹配,许多人希望这是有效数字.
您可能还会发现此答案有趣,尤其是其他编程语言中的示例.大多数语言(除了C& family之外)都允许将 123abc
作为两个标记进行扫描,无论如何这通常都会导致语法错误,并且这是最简单且可维护性最高的解决方案.>
In some Flex/Lex code we have,
DIGIT [0-9]
LETTER [A-Za-z]
ID {LETTER}{LETTER}|{DIGIT})*
NUM {DIGIT}+(\.{DIGIT}+)?
So, 123
matches NUM
and abc123
matches ID
. In case of
123abc
, this matches NUM
followed by and ID
. However,
I want this to generate an error message instead of matching.
Is there a simple way to do this?
You can just add an additional pattern to detect an error.
The following does not use macros, because I personally find them distracting. But the idea is the same.
[[:alpha:]][[:alnum:]]* { return IDENTIFIER; }
[[:digit:]]+"."([[:digit:]]+)? { return NUMBER; }
[[:digit:]]+"."([[:digit:]]+)?[[:alpha:]] { return BAD_NUMBER; }
The last pattern will only match if there is a letter immediately following a NUMBER, and will override the second pattern because of the longest-match rule.
By the way, a better pattern for a number is:
[[:digit:]]+("."[[:digit:]]*)?|"."[[:digit:]]+
That will match 23.
and .56
, which many people expect to be valid numbers.
You might also find this answer interesting, particularly the examples from other programming languages. Most languages (other than C & family) do allow 123abc
to be scanned as two tokens, which usually leads to a syntax error anyway, and that is both the easiest and the most maintainable solution.
这篇关于在Flex中处理数字后跟字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!