首页 > 解决方案 > flex 上的 [ABCDEFGH] 和 [AH] 有区别吗?

问题描述

我在 flex 上有以下代码:

Y "line "|"triangle "|"square "|"pentagon "|"hexagon "|"heptagon "|"octagon "
X1 [BCDEFGH]*"A"[BCDEFGH]*("A"[BCDEFGH]*)+
X2 [ACDEFGH]*"B"[ACDEFGH]*("B"[ACDEFGH]*)+
X3 [ABDEFGH]*"C"[ABDEFGH]*("C"[ABDEFGH]*)+
X4 [ABCEFGH]*"D"[ABCEFGH]*("D"[ABCEFGH]*)+
X5 [ABCDFGH]*"E"[ABCDFGH]*("E"[ABCDFGH]*)+
X6 [ABCDEGH]*"F"[ABCDEGH]*("F"[ABCDEGH]*)+
X7 [ABCDEFH]*"G"[ABCDEFH]*("G"[ABCDEFH]*)+
X8 [ABCDEFG]*"H"[ABCDEFG]*("H"[ABCDEFG]*)+ 
%%
"point "[ABCDEFGH]{2,} printf("TOO MANY LETTERS"); /*check if the letters are more than the given word or shape could have, lines 14-21*/
"line "[ABCDEFGH]{3,} printf("TOO MANY LETTERS"); /*if yes,a TOO MANY LETTERS message will appear*/
"triangle "[ABCDEFGH]{4,} printf("TOO MANY LETTERS");
"square "[ABCDEFGH]{5,} printf("TOO MANY LETTERS");
"pentagon "[ABCDEFGH]{6,} printf("TOO MANY LETTERS");
"hexagon "[ABCDEFGH]{7,} printf("TOO MANY LETTERS");
"heptagon "[ABCDEFGH]{8,} printf("TOO MANY LETTERS");
"octagon "[ABCDEFGH]{9,} printf("TOO MANY LETTERS");

{Y}({X1}|{X2}|{X3}|{X4}|{X5}|{X6}|{X7}|{X8}) printf("DUPLICATE/S"); /*check if there are any duplicates,if yes,a DUPLICATE message will appear*/

"point "[ABCDEFGH] printf("CORRECT EXPRESSION"); /*Here, after we check for duplicates or more letters*/
"line "[ABCDEFGH]{2} printf("CORRECT EXPRESSION"); /*it will check if the number of letters are EXACTLY as many as they should be*/
"triangle "[ABCDEFGH]{3} printf("CORRECT EXPRESSION"); /*etc 3 for triangle,8 for octagon, lines 27-32*/
"square "[ABCDEFGH]{4} printf("CORRECT EXPRESSION"); /*if thats true,it will print the message CORRECT*/
"pentagon "[ABCDEFGH]{5} printf("CORRECT EXPRESSION");
"hexagon "[ABCDEFGH]{6} printf("CORRECT EXPRESSION");
"heptagon "[ABCDEFGH]{7} printf("CORRECT EXPRESSION");
"octagon "[ABCDEFGH]{8} printf("CORRECT EXPRESSION");

[^\n]+ printf("UNRECOGNIZED CHARACTER/S"); /*show UNRECOGNIZED CHARACTERS if the user tries to write unwanted characters or if he makes an alphabetical mistake*/
%%
int main() {
/*other things*/
}

它检查用户是否给出了正确格式的形状表达式并打印相应的消息。例如正确的格式是:点 A、正方形 AHDB、六边形 HCDBAG 错误的格式是:线 AA(重复)、正方形 ABCDE(更多字母)

问题是,当我用 [AH] 替换表达式 [ABCDEFGH] 时,程序的工作方式就像

"point "[ABCDEFGH]{2,} printf("TOO MANY LETTERS"); /*check if the letters are more than the given word or shape could have, lines 14-21*/
"line "[ABCDEFGH]{3,} printf("TOO MANY LETTERS"); /*if yes,a TOO MANY LETTERS message will appear*/
"triangle "[ABCDEFGH]{4,} printf("TOO MANY LETTERS");
"square "[ABCDEFGH]{5,} printf("TOO MANY LETTERS");
"pentagon "[ABCDEFGH]{6,} printf("TOO MANY LETTERS");
"hexagon "[ABCDEFGH]{7,} printf("TOO MANY LETTERS");
"heptagon "[ABCDEFGH]{8,} printf("TOO MANY LETTERS");
"octagon "[ABCDEFGH]{9,} printf("TOO MANY LETTERS");

"point "[ABCDEFGH] printf("CORRECT EXPRESSION"); /*Here, after we check for duplicates or more letters*/
"line "[ABCDEFGH]{2} printf("CORRECT EXPRESSION"); /*it will check if the number of letters are EXACTLY as many as they should be*/
"triangle "[ABCDEFGH]{3} printf("CORRECT EXPRESSION"); /*etc 3 for triangle,8 for octagon, lines 27-32*/
"square "[ABCDEFGH]{4} printf("CORRECT EXPRESSION"); /*if thats true,it will print the message CORRECT*/
"pentagon "[ABCDEFGH]{5} printf("CORRECT EXPRESSION");
"hexagon "[ABCDEFGH]{6} printf("CORRECT EXPRESSION");
"heptagon "[ABCDEFGH]{7} printf("CORRECT EXPRESSION");
"octagon "[ABCDEFGH]{8} printf("CORRECT EXPRESSION");

从未存在过。

例如:给定第一种形式的“point AB”表达式([ABCDEFGH]),将打印:TOO MANY LETTERS

给定第一种形式的“A点”表达式([ABCDEFGH]),将打印:正确的表达式

给定第二种形式的“点 AB”表达式(如果我们将 [ABCDEFGH] 更改为 [AH]),将打印:UNRECOGNIZED CHARACTER/S。

给定第二种形式的“点 A”表达式(如果我们将 [ABCDEFGH] 更改为 [AH]),将打印:UNRECOGNIZED CHARACTER/S。

有没有解释为什么会发生这种情况?

标签: cflex-lexer

解决方案


推荐阅读