收到警告:无法匹配规则
Getting Warning: rule cannot be matched
所以我正在尝试使用 Flex Code 和一些 Bison.I 制作电子词法解析器,不断收到关于我的规则的警告,而不是无法匹配,我找不到任何 error.I 也搜索过这个警告是什么意思,但我想我还没有声明类似的规则 again.So 我认为任何其他规则都不可能超越其他规则并且不能 matched.I 继续从第 83 行收到错误是 {COMMA} {return COMMA;},直到我的代码结束,我不知道 why.Any 想法?
WHITESPACE [ \t]
INTDEC [0]|([1-9][0-9]*)
INTHEX 0[xX][0-9A-F]+
INTOCTAL 0[0-7]+
VARIABLE [A-Za-z][a-zA-Z0-9_]+
COMMA ","
INT "int"
FLOAT "float"
DOUBLE "double"
VOID "void"
LONG "long"
SHORT "short"
BREAK "break"
RETURN "return"
LEKTIKA \"(\[n"\]|[^"\\n])*\"
COMMENTS "//".*
REAL ([0][.][0-9]+)|([1-9][0-9]*[.][0-9]+)
EXP ([0-9]+|{REAL})[Ee]-?[0-9]+
UNKNOWN_TOKEN [^ \t\n]*
NEWLINE \n
LESS_THAN [<]
GREATER_THAN [>]
GREATER_OR_EQUAL_TO [>\=]
LESS_OR_EQUAL_TO [<\=]
NOT_EQUAL_TO [\=!]
EQUAL [\=\=]
DIVIDE [\/]
MULTIPLY [\*]
MODULO [%]
PLUS [\+]
MINUS [\-]
PLUS_ASSIGN [\+\=]
MINUS_ASSIGN [\-\=]
MULT_ASSIGN [\*\=]
DIV_ASSIGN [\/\=]
LOGICAL_AND [&&]
LOGICAL_OR [\|\|]
INCREMENT [\+\+]
DECREMENT [\-\-]
ALL_OPERATORS [{LESS_THAN}{GREATER_THAN}{GREATER_OR_EQUAL_TO}{LESS_OR_EQUAL_TO}{NOT_EQUAL_TO}{EQUAL}{DIVIDE}{MULTIPLY}{MODULO}{PLUS}{MINUS}{PLUS_ASSIGN}{MINUS_ASSIGN}{MULT_ASSIGN}{DIV_ASSIGN} {LOGICAL_AND}{LOGICAL_OR}{INCREMENT}{DECREMENT}]
SCAN scan[[(][{VARIABLE}][)];
LEN len[(][{VARIABLE}|{LEKTIKA}|{INTDEC}|{REAL}|{EXP}]*[)];
CMP cmp[(][{VARIABLE}|{LEKTIKA}|{INTDEC}|{REAL}|{EXP},]*[)];
PRINT print[(][{VARIABLE}|{LEKTIKA}|{INTDEC}|{REAL}|{EXP}]*[)];
FUNCTION_NAME {VARIABLE}
FUNCTION [func]+\s[{FUNCTION_NAME}]+\s[(]+[{INT}|{FLOAT}|{DOUBLE}\s+[{VARIABLE}],\s]*[)]
CALL_FUNC [{FUNCTION_NAME}][(][{INTEDEC}|{INTHEX}|{INOCTAL}|{REAL}][)];
ARRAY_NAME [{VARIABLE}]
ARRAY_CON [\[][{INTDEC}|{VARIABLE}|{VARIABLE}|{LEKTIKA},\s]*
ARRAY [{ARRAY_NAME}]\s[\=]\s[\[][{INTDEC}|{VARIABLE}|{VARIABLE}|{LEKTIKA},]*[\]];
DEC_INT {INT}[{VARIABLE}];
DEC_FLOAT {FLOAT}[{VARIABLE}];
DEC_DOUBLE {DOUBLE}[{VARIABLE}];
COMPARISONS [{VARIABLE}]\s[{LESS_THAN}|{GREATER_THAN}|{GREATER_OR_EQUAL_TO}| {LESS_OR_EQUAL_TO}|{NOT_EQUAL_TO}|{EQUAL}]\s[{VARIABLE}]
DEC_VAR [{DEC_INT}|{DEC_FLOAT}|{DEC_DOUBLE}];
VAR_ASSIGN [{VARIABLE}|{ARRAY_NAME},\s]*\s[\=]\s[{INT}|{FLOAT}|{DOUBLE}|{ARRAY_CON},\s]*;
%%
{WHITESPACE} {}
{COMMENTS} {}
{INTDEC} {return INTDEC;}
{INTHEX} {return INTHEX;}
{INTOCTAL} {return INTOCTAL;}
{REAL} {return REAL;}
{EXP} {return EXP;}
{LEKTIKA} {return LEKTIKA;}
{VARIABLE} {return VARIABLE;}
{NEWLINE} {return NEWLINE;}
{UNKNOWN_TOKEN} {return UNKNOWN_TOKEN;}
{COMMA} {return COMMA;}
{INT} {return INT;}
{FLOAT} {return FLOAT;}
{DOUBLE} {return DOUBLE;}
{VOID} {return VOID;}
{LONG} {return LONG;}
{SHORT} {return SHORT;}
{BREAK} {return BREAK;}
{RETURN} {return RETURN;}
{LESS_THAN} {return LESS_THAN;}
{GREATER_THAN} {return GREATER_THAN;}
{GREATER_OR_EQUAL_TO} {return GREATER_OR_EQUAL_TO;}
{LESS_OR_EQUAL_TO} {return LESS_OR_EQUAL_TO;}
{NOT_EQUAL_TO} {return NOT_EQUAL_TO;}
{EQUAL} {return EQUAL;}
{DIVIDE} {return DIVIDE;}
{MULTIPLY} {return MULTIPLY;}
{MODULO} {return MODULO;}
{PLUS} {return PLUS;}
{MINUS} {return MINUS;}
{PLUS_ASSIGN} {return PLUS_ASSIGN;}
{MINUS_ASSIGN} {return MINUS_ASSIGN;}
{MULT_ASSIGN} {return MULT_ASSIGN;}
{DIV_ASSIGN} {return DIV_ASSIGN;}
{LOGICAL_AND} {return LOGICAL_AND;}
{LOGICAL_OR} {return LOGICAL_OR;}
{INCREMENT} {return INCREMENT;}
{DECREMENT} {return DECREMENT;}
{ALL_OPERATORS} {return ALL_OPERATORS;}
{SCAN} {return SCAN;}
{LEN} {return LEN;}
{CMP} {return CMP;}
{PRINT} {return PRINT;}
{FUNCTION_NAME} {return FUNCTION_NAME;}
{FUNCTION} {return FUNCTION;}
{CALL_FUNC} {return CALL_FUNC;}
{ARRAY_NAME} {return ARRAY_NAME;}
{ARRAY_CON} {return ARRAY_CON;}
{ARRAY} {return ARRAY;}
{DEC_INT} {return DEC_INT;}
{DEC_FLOAT} {return DEC_FLOAT;}
{DEC_DOUBLE} {return DEC_DOUBLE;}
{COMPARISONS} {return COMPARISONS;}
{DEC_VAR} {return DEC_VAR;}
{VAR_ASSIGN} {return VAR_ASSIGN;}
在您的规则中,{UNKNOWN_TOKEN}
出现得很早。由于它匹配不包含空格的任何长度的任何标记,因此它将匹配与任何后续规则相同的标记(可能还有更多字符)。由于 (f)lex 总是选择第一个规则,如果可能有多个规则,{UNKNOWN_TOKEN}
之后的 none 规则将永远匹配。
我建议你少用宏。 (或者根本没有宏。)它们不会为您的代码做出任何贡献。它们只会让它变得不必要地长,而且它们让你感到困惑(我认为),因为宏定义不是规则,宏定义的顺序不会影响匹配,只会影响规则本身的顺序。
还有一些其他错误。例如,[\+\+]
不匹配 ++
。它是一个字符class,所以它恰好匹配一个字符。它唯一可以匹配的字符是 +
因为集合中重复的字符不会改变集合。 (也不需要在字符 class 中反斜杠转义 +
。括号内的大多数字符都失去了它们的特殊含义。)你的意思是 "++"
.
所以我正在尝试使用 Flex Code 和一些 Bison.I 制作电子词法解析器,不断收到关于我的规则的警告,而不是无法匹配,我找不到任何 error.I 也搜索过这个警告是什么意思,但我想我还没有声明类似的规则 again.So 我认为任何其他规则都不可能超越其他规则并且不能 matched.I 继续从第 83 行收到错误是 {COMMA} {return COMMA;},直到我的代码结束,我不知道 why.Any 想法?
WHITESPACE [ \t]
INTDEC [0]|([1-9][0-9]*)
INTHEX 0[xX][0-9A-F]+
INTOCTAL 0[0-7]+
VARIABLE [A-Za-z][a-zA-Z0-9_]+
COMMA ","
INT "int"
FLOAT "float"
DOUBLE "double"
VOID "void"
LONG "long"
SHORT "short"
BREAK "break"
RETURN "return"
LEKTIKA \"(\[n"\]|[^"\\n])*\"
COMMENTS "//".*
REAL ([0][.][0-9]+)|([1-9][0-9]*[.][0-9]+)
EXP ([0-9]+|{REAL})[Ee]-?[0-9]+
UNKNOWN_TOKEN [^ \t\n]*
NEWLINE \n
LESS_THAN [<]
GREATER_THAN [>]
GREATER_OR_EQUAL_TO [>\=]
LESS_OR_EQUAL_TO [<\=]
NOT_EQUAL_TO [\=!]
EQUAL [\=\=]
DIVIDE [\/]
MULTIPLY [\*]
MODULO [%]
PLUS [\+]
MINUS [\-]
PLUS_ASSIGN [\+\=]
MINUS_ASSIGN [\-\=]
MULT_ASSIGN [\*\=]
DIV_ASSIGN [\/\=]
LOGICAL_AND [&&]
LOGICAL_OR [\|\|]
INCREMENT [\+\+]
DECREMENT [\-\-]
ALL_OPERATORS [{LESS_THAN}{GREATER_THAN}{GREATER_OR_EQUAL_TO}{LESS_OR_EQUAL_TO}{NOT_EQUAL_TO}{EQUAL}{DIVIDE}{MULTIPLY}{MODULO}{PLUS}{MINUS}{PLUS_ASSIGN}{MINUS_ASSIGN}{MULT_ASSIGN}{DIV_ASSIGN} {LOGICAL_AND}{LOGICAL_OR}{INCREMENT}{DECREMENT}]
SCAN scan[[(][{VARIABLE}][)];
LEN len[(][{VARIABLE}|{LEKTIKA}|{INTDEC}|{REAL}|{EXP}]*[)];
CMP cmp[(][{VARIABLE}|{LEKTIKA}|{INTDEC}|{REAL}|{EXP},]*[)];
PRINT print[(][{VARIABLE}|{LEKTIKA}|{INTDEC}|{REAL}|{EXP}]*[)];
FUNCTION_NAME {VARIABLE}
FUNCTION [func]+\s[{FUNCTION_NAME}]+\s[(]+[{INT}|{FLOAT}|{DOUBLE}\s+[{VARIABLE}],\s]*[)]
CALL_FUNC [{FUNCTION_NAME}][(][{INTEDEC}|{INTHEX}|{INOCTAL}|{REAL}][)];
ARRAY_NAME [{VARIABLE}]
ARRAY_CON [\[][{INTDEC}|{VARIABLE}|{VARIABLE}|{LEKTIKA},\s]*
ARRAY [{ARRAY_NAME}]\s[\=]\s[\[][{INTDEC}|{VARIABLE}|{VARIABLE}|{LEKTIKA},]*[\]];
DEC_INT {INT}[{VARIABLE}];
DEC_FLOAT {FLOAT}[{VARIABLE}];
DEC_DOUBLE {DOUBLE}[{VARIABLE}];
COMPARISONS [{VARIABLE}]\s[{LESS_THAN}|{GREATER_THAN}|{GREATER_OR_EQUAL_TO}| {LESS_OR_EQUAL_TO}|{NOT_EQUAL_TO}|{EQUAL}]\s[{VARIABLE}]
DEC_VAR [{DEC_INT}|{DEC_FLOAT}|{DEC_DOUBLE}];
VAR_ASSIGN [{VARIABLE}|{ARRAY_NAME},\s]*\s[\=]\s[{INT}|{FLOAT}|{DOUBLE}|{ARRAY_CON},\s]*;
%%
{WHITESPACE} {}
{COMMENTS} {}
{INTDEC} {return INTDEC;}
{INTHEX} {return INTHEX;}
{INTOCTAL} {return INTOCTAL;}
{REAL} {return REAL;}
{EXP} {return EXP;}
{LEKTIKA} {return LEKTIKA;}
{VARIABLE} {return VARIABLE;}
{NEWLINE} {return NEWLINE;}
{UNKNOWN_TOKEN} {return UNKNOWN_TOKEN;}
{COMMA} {return COMMA;}
{INT} {return INT;}
{FLOAT} {return FLOAT;}
{DOUBLE} {return DOUBLE;}
{VOID} {return VOID;}
{LONG} {return LONG;}
{SHORT} {return SHORT;}
{BREAK} {return BREAK;}
{RETURN} {return RETURN;}
{LESS_THAN} {return LESS_THAN;}
{GREATER_THAN} {return GREATER_THAN;}
{GREATER_OR_EQUAL_TO} {return GREATER_OR_EQUAL_TO;}
{LESS_OR_EQUAL_TO} {return LESS_OR_EQUAL_TO;}
{NOT_EQUAL_TO} {return NOT_EQUAL_TO;}
{EQUAL} {return EQUAL;}
{DIVIDE} {return DIVIDE;}
{MULTIPLY} {return MULTIPLY;}
{MODULO} {return MODULO;}
{PLUS} {return PLUS;}
{MINUS} {return MINUS;}
{PLUS_ASSIGN} {return PLUS_ASSIGN;}
{MINUS_ASSIGN} {return MINUS_ASSIGN;}
{MULT_ASSIGN} {return MULT_ASSIGN;}
{DIV_ASSIGN} {return DIV_ASSIGN;}
{LOGICAL_AND} {return LOGICAL_AND;}
{LOGICAL_OR} {return LOGICAL_OR;}
{INCREMENT} {return INCREMENT;}
{DECREMENT} {return DECREMENT;}
{ALL_OPERATORS} {return ALL_OPERATORS;}
{SCAN} {return SCAN;}
{LEN} {return LEN;}
{CMP} {return CMP;}
{PRINT} {return PRINT;}
{FUNCTION_NAME} {return FUNCTION_NAME;}
{FUNCTION} {return FUNCTION;}
{CALL_FUNC} {return CALL_FUNC;}
{ARRAY_NAME} {return ARRAY_NAME;}
{ARRAY_CON} {return ARRAY_CON;}
{ARRAY} {return ARRAY;}
{DEC_INT} {return DEC_INT;}
{DEC_FLOAT} {return DEC_FLOAT;}
{DEC_DOUBLE} {return DEC_DOUBLE;}
{COMPARISONS} {return COMPARISONS;}
{DEC_VAR} {return DEC_VAR;}
{VAR_ASSIGN} {return VAR_ASSIGN;}
在您的规则中,{UNKNOWN_TOKEN}
出现得很早。由于它匹配不包含空格的任何长度的任何标记,因此它将匹配与任何后续规则相同的标记(可能还有更多字符)。由于 (f)lex 总是选择第一个规则,如果可能有多个规则,{UNKNOWN_TOKEN}
之后的 none 规则将永远匹配。
我建议你少用宏。 (或者根本没有宏。)它们不会为您的代码做出任何贡献。它们只会让它变得不必要地长,而且它们让你感到困惑(我认为),因为宏定义不是规则,宏定义的顺序不会影响匹配,只会影响规则本身的顺序。
还有一些其他错误。例如,[\+\+]
不匹配 ++
。它是一个字符class,所以它恰好匹配一个字符。它唯一可以匹配的字符是 +
因为集合中重复的字符不会改变集合。 (也不需要在字符 class 中反斜杠转义 +
。括号内的大多数字符都失去了它们的特殊含义。)你的意思是 "++"
.