2013-10-10 42 views
0

我写的规则,但我不明白为什么欲望规则不匹配,因为文档对此表示:如何将优先级设置为规则?

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 
+0

避免对'ANY_CHAR'使用'*'量词。由于'ANY_CHAR'匹配空白字符,因此您可能会匹配比您意识到的更长的字符串。 ' {ANY_CHAR} {printf(“print Other”);退出(1);}'应该为你的目的服务。 –

+0

[David Gorsline](http://stackoverflow.com/users/86809/david-gorsline)我做了你说的,但没有奏效 –

回答

1

如果输入是真的在两个不同的线,那么你的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);}