- 论坛徽章:
- 9
|
最近在研究 Kbuild 的词法分析器,遇到两个很相似的正则表达式,不知道他们的不同点在哪里,如下:
和
对于上面两个正则表达式,我使用两个测试用例,第一个如下:
- %option noyywrap
- %{
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- %}
- %%
- [^'"\\\n]+/\n { printf("Matchoo[%s]\n", yytext); }
- [^'"\\\n]+ { printf("Matchi2[%s]\n", yytext); }
- %%
- int main(int argc, char *argv[])
- {
- yylex();
- return 0;
- }
复制代码 第二个测试用例为
- %option noyywrap
- %{
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- %}
- %%
- [^'"\\\n]+/\n { printf("Matchoo[%s]\n", yytext); }
- [^'"\\\n]+ { printf("Matchi2[%s]\n", yytext); }
- %%
- int main(int argc, char *argv[])
- {
- /* Parsing a string. */
- char input[50] = "Linux/$ARCH $KERNELVERSION Kernel Configuration";
- /* Copy string into new buffer and switch buffers. */
- yy_scan_string(input);
- /* Analyze the string. */
- yylex();
- /* Delete the new buffer. */
- yy_delete_buffer(YY_CURRENT_BUFFER);
- return 0;
- }
复制代码 第一个测试用例处理一个来自终端输入的字符串: "Linux/$ARCH $KERNELVERSION Kernel Configuration"
第二个测试用例处理一个来自 buffer 的字符串: "Linux/$ARCH $KERNELVERSION Kernel Configuration"
同样的字符串,但是结果不同
第一个测试用例匹配了: [^'"\\\n]+/\n
第二个测试用例匹配了: [^'"\\\n]+
这两个测试用例有什么不同?
|
|