gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:44:53 25 4
gpt4 key购买 nike

我已经写了规则,但我不明白为什么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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com