特定號碼在我的語法我有這樣的事情:匹配到一個非貪婪的方式重複在ANTLR
line : startWord (matchPhrase|
anyWord matchPhrase|
anyWord anyWord matchPhrase|
anyWord anyWord anyWord matchPhrase|
anyWord anyWord anyWord anyWord matchPhrase)
-> ^(TreeParent startWord anyWord* matchPhrase);
所以我想匹配的matchPhrase
第一次出現,但我會允許在它之前達到一定數量的anyWord
。組成matchPhrase
的令牌也與anyWord
匹配。
有沒有更好的方法來做到這一點?
我認爲它可能是由語義謂詞in this answer與非貪婪選項結合成爲可能:
(options {greedy=false;} : anyWord)*
,但我無法弄清楚究竟是如何做到這一點。
編輯:下面是一個例子。我想從下面的句子中提取信息:
Picture of a red flower.
Picture of the following: A red flower.
我輸入實際被標記的英語句子,和詞法規則相匹配的標籤,而不是單詞。所以輸入到ANTLR是:
NN-PICTURE Picture IN-OF of DT a JJ-COLOR red NN-FLOWER flower
NN-PICTURE Picture IN-OF of DT the VBG following COLON : DT a JJ-COLOR red NN-FLOWER flower
我有詞法規則,這樣每個標籤:
WS : (' ')+ {skip();};
TOKEN : (~' ')+;
nnpicture:'NN-PICTURE' TOKEN -> ^('NN-PICTURE' TOKEN);
vbg:'VBG' TOKEN -> ^('VBG' TOKEN);
我的語法規則是這樣的:
sentence : nnpicture inof matchFlower;
matchFlower : (dtTHE|dt)? jjcolor? nnflower;
當然,但這將在第二句話中失敗。所以我想通過在花比賽之前允許多達N個令牌來允許一點靈活性。我有一個匹配任何一個anyWord
令牌,以及以下工作:
sentence : nnpicture inof (matchFlower |
anyWord matchFlower |
anyWord anyWord matchFlower | etc.
,但它是不是很優雅,並且不與大N.很好地工作
@BartKiers:對不起,我沒有解釋它是那麼好 - ' matchPhrase'是'anyWord'的一個子集,所以可能會有一些單詞不在'matchPhrase'之前的'matchPhrase'中,並且它們會被'anyWord'匹配。但是因爲它是一個子集,所以'anyWord'匹配需要非貪婪,否則'matchPhrase'字將與'anyWord'匹配。因此,爲什麼我不能做'任何語言?任何單詞?任何單詞? matchPhrase'。 – 2012-03-14 10:13:23
@Matt,我明白你的意思了。如果有人在我面前不這樣做,我會在今天晚上回答你(我在ATM工作)。 – 2012-03-14 10:47:48