c - 如何设定规则的优先级?

标签 c lex rules flex-lexer lexical-analysis

我已经写了规则,但我不明白为什么desires规则不匹配,因为文档是这样说的:

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.

我也看过这个答案,但没有帮助:Is it possible to set priorities for rules to avoid the "longest-earliest" matching pattern?

 ...
 ANY_CHAR .
 ...

 %%
 "gago"                         { BEGIN V_TYPE; }
 <V_TYPE>"If"                   { printf("print If");       exit(1);}
 <V_TYPE>"Then"                 { printf("print Then");     exit(1);}
 <V_TYPE>"Endif"                { printf("print Endif");    exit(1);}
 <V_TYPE>"While"                { printf("print While");    exit(1);}
 <V_TYPE>"EndWhile"             { printf("print EndWhile"); exit(1);}
 <V_TYPE>{ANY_CHAR}*            { printf("print Other");    exit(1);}

简单输入:

gago
EndWhile

期望的输出:

print EndWhile

实际输出:

print Other

最佳答案

如果您的输入确实位于两个不同的行,则您的 ANY_CHAR 规则将匹配换行符。如果您不关心换行符,则应该忽略它们。我还建议根据 David Gorsline 的评论,删除 ANY_CHAR 上的 * 修饰符。

...
ANY_CHAR .
NEW_LINE [\n\r]
...

%%
"gago"                         { BEGIN V_TYPE; }
<V_TYPE>"If"                   { printf("print If");       exit(1);}
<V_TYPE>"Then"                 { printf("print Then");     exit(1);}
<V_TYPE>"Endif"                { printf("print Endif");    exit(1);}
<V_TYPE>"While"                { printf("print While");    exit(1);}
<V_TYPE>"EndWhile"             { printf("print EndWhile"); exit(1);}
<V_TYPE>{NEW_LINE}+            { /* ignore */  }
<V_TYPE>{ANY_CHAR}             { printf("print Other");    exit(1);}

关于c - 如何设定规则的优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19293492/

相关文章:

c - 如何在 C 中捕获进程输出?

c++ - 在 C99 中需要 _Bool 吗?

.pc 文件 "PCC-S-02015, unable to open include file"和 "PCC-S-02201, Encountered the symbol "size_t""的编译错误

rust - 表驱动的词法分析需要多少缓冲?

css - 使用@media 忽略整个样式表

c++ - C 中的 fork()、共享内存和指针

c - 执行使用 lex 和 yacc 工具开发的 c 文件时出错

c - lex 前缀 undefined symbol yyparse

java - forall 总是评估为 true [Drools]

java - 在 Drools 电子表格中使用 "ignore rule"回退循环遍历值数组