c - 是否可以为规则设置优先级以避免 "longest-earliest"匹配模式?

标签 c lex rules flex-lexer

另一个简单的问题:有什么方法可以告诉 flex 优先选择匹配较短事物的规则而不是匹配较长事物的规则?我找不到关于此的任何好的文档。

这就是我需要它的原因:我为一种伪语言解析一个文件,其中包含一些与控制指令相对应的关键字。我希望它们具有绝对优先级,这样它们就不会被解析为表达式的一部分。我实际上需要这个优先事项,因为我不必为我的项目编写完整的语法(在我的情况下,这完全是矫枉过正,因为我对解析的程序进行结构分析,我不需要知道细节.. .),所以我无法使用精细的语法调整来确保这些 block 不会被解析为表达式。

我们将不胜感激。

这是一个解析文件的例子:

If a > 0 Then read(b); Endif
c := "If I were...";
While d > 5 Do d := d + 1 Endwhile

我只想收集有关 Ifs、Thens、Endifs 等的信息……其余的对我来说无关紧要。这就是为什么我希望在不编写语法的情况下优先考虑 Ifs、Thens 等...相关规则。

最佳答案

摘自龙书第2版第3.5.3节“Lex中的冲突解决”:

We have alluded to the two rules that Lex uses to decide on the proper lexeme
to select, when several prefixes of the input match one or more patterns:
    1. Always prefer a longer prefix to a shorter prefix.
    2. If the longest possible prefix matches two or more patterns, prefer the
       pattern listed first in the Lex program.

上述规则也适用于 Flex。 Flex 手册是这么说的(第 7 章:如何匹配输入。)

When the generated scanner is run, it analyzes its input looking for strings 
which match any of its patterns. If it finds more than one match, it takes the 
one matching the most text (for trailing context rules, this includes the length 
of the trailing part, even though it will then be returned to the input). If it 
finds two or more matches of the same length, the rule listed first in the flex 
input file is chosen.

如果我没理解错的话,你的词法分析器会将 Endif 之类的关键字视为标识符,因此之后它将被视为表达式的一部分。如果这是你的问题,只需将关键字的规则放在你的规范之上,例如:(假设大写的每个单词都是预定义的枚举对应于一个标记)

"If"                      { return IF;         }
"Then"                    { return THEN;       }
"Endif"                   { return ENDIF;      }
"While"                   { return WHILE;      }
"Do"                      { return DO;         }
"EndWhile"                { return ENDWHILE;   }
\"(\\.|[^\\"])*\"         { return STRING;     }
[a-zA-Z_][a-zA-Z0-9_]*    { return IDENTIFIER; }

然后关键字将始终在标识符之前匹配由于第 2 条规则。

编辑:

谢谢你的评论,kol。我忘了添加字符串规则。 但我不认为我的解决方案是错误的。例如,如果一个名为 If_this_is_an_identifier 的标识符,规则 1 将适用,因此标识符规则将生效(因为它匹配最长的字符串)。我写了一个简单的测试用例,在我的解决方案中没有发现问题。这是我的 lex.l 文件:

%{
  #include <iostream>
  using namespace std;
%}

ID       [a-zA-Z_][a-zA-Z0-9_]*

%option noyywrap
%%

"If"                      { cout << "IF: " << yytext << endl;         }
"Then"                    { cout << "THEN: " << yytext << endl;       }
"Endif"                   { cout << "ENDIF: " << yytext << endl;      }
"While"                   { cout << "WHILE: " << yytext << endl;      }
"Do"                      { cout << "DO: " << yytext << endl;         }
"EndWhile"                { cout << "ENDWHILE: " << yytext << endl;   }
\"(\\.|[^\\"])*\"         { cout << "STRING: " << yytext << endl;     }
{ID}                      { cout << "IDENTIFIER: " << yytext << endl; }
.                         { cout << "Ignore token: " << yytext << endl; }

%%

int main(int argc, char* argv[]) {
  ++argv, --argc;  /* skip over program name */
  if ( argc > 0 )
    yyin = fopen( argv[0], "r" );
  else
    yyin = stdin;

  yylex();
}

我用以下测试用例测试了我的解决方案:

If If_this_is_an_identifier > 0 Then read(b); Endif
    c := "If I were...";
While While_this_is_also_an_identifier > 5 Do d := d + 1 Endwhile

它给了我以下输出(与您提到的问题无关的其他输出被忽略。)

IF: If
IDENTIFIER: If_this_is_an_identifier
......
STRING: "If I were..."
......
WHILE: While
IDENTIFIER: While_this_is_also_an_identifier

lex.l 程序是根据 flex manual 中的示例修改的:(使用相同的方法匹配标识符中的关键字)

另请查看 the ANSI C grammar, Lex specification .

我在个人项目中也使用了这种方式,目前没有发现任何问题。

关于c - 是否可以为规则设置优先级以避免 "longest-earliest"匹配模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8379299/

相关文章:

c - 在c中不使用printf()打印 float

c - 使用 C 传递数据变量以在行命令 OpenSSL 中加密/解密

c++ - 如何在单独的文件中使用 `yy_scan_string(const char *str)`(由 lex yacc 生成)

没有配方的 makefile 模式规则

package - 为 dolphin 服务菜单创建 Debian 软件包

python - 我如何在 python 中使用 cssutils 编辑规则

c - C中的接口(interface)交互

c - 将字符数组中的字符放入字符串中,直到找到特定字符

ubuntu - LEX 和 YACC : grammar implementation in NLP

c - Lex yacc 移位/减少错误