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
避免對'ANY_CHAR'使用'*'量詞。由於'ANY_CHAR'匹配空白字符,因此您可能會匹配比您意識到的更長的字符串。 ' {ANY_CHAR} {printf(「print Other」);退出(1);}'應該爲你的目的服務。 –
[David Gorsline](http://stackoverflow.com/users/86809/david-gorsline)我做了你說的,但沒有奏效 –