correct pattern in flex, error : rule cannot me matched

correct pattern in flex, error : rule cannot me matched

你好我正在尝试在 flex 中使用以下模式来匹配

形式的信息

当我编译它时,它显示规则无法匹配的警告错误。我的代码是这样的:

%{
#include "y.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}
%option noyywrap
letter [a-zA-Z]
digit [0-9]
other_characters [./-_]
whitespace [ \t]
newline [\n]
string ({letter}|{digit}|{other_characters})({letter}|{digit}|{other_characters})+
%%
{string}({whitespace}|{string})     {printf ("%s", yytext); return TEXTMSG;}
"ss\:Workshop"      {printf("%s", yytext); return WORKSHOP;}
"ss\:Name\=\"Number\""|"ss\:Name\="\"{string}\"|    {printf("%s", yytext); return NAME;}

关于为什么这不行的任何线索?在这里 flex 有点新,所以我相信我错过了一些东西,但不确定是什么

问题出在这个字符class:

other_characters [./-_]

字符 class 中的破折号表示一个可能的字符范围(如 [a-z],它匹配任何小写字母)。因此 /-_ 匹配代码点介于 / (0x2F) 和 _ (0x5F) 之间的任何字符。该范围包括数字、大写字母和一些标点符号,包括冒号和分号。

这使得冒号成为 {string} 中的有效字符,因此 {string} 将匹配 ss:Workshop。由于 flex 优先考虑与输入匹配的第一条规则,这使得 ss:Workshop 规则永远不可能被匹配。

您可以通过将破折号放在字符 class 的开头或结尾来解决此问题:[-./_][./_-]。这些只会匹配列出的四个字符之一。

顺便说一句,不需要反斜杠转义冒号,甚至不需要引用它。在弹性模式中没有特殊意义。