2012-08-08 29 views
1

我想模擬mqsi命令,使用ANTLR並遇到以下問題。 mqsicreateconfigurableservice的文檔針對queuePrefix說: 「前綴可以包含在WebSphere®MQ隊列名稱中有效的任何字符,但不得超過8個字符,且不得以句點(。)開頭或結尾。例如,SET.1有效,但.SET1和SET1無效,多個可配置服務可以使用相同的隊列前綴。「使用ANTLR,如何處理特定的重複而不使用語言特定的語義謂詞?

我已經使用了以下內容作爲權宜之計,但這種技術意味着我必須至少有兩個字符的名稱,並且看起來非常浪費且不可擴展的解決方案。有更好的方法嗎?

參見 'queuePrefixValue',下面...

感謝:O)

parser grammar mqsicreateconfigurableservice; 
mqsicreateconfigurableservice 
: 'mqsicreateconfigurableservice' ' '+ params 
; 
params : (broker ' '+ switches+) 
; 
broker : validChar+ 
; 
switches 
: AggregationConfigurableService 
; 
AggregationConfigurableService 
: (objectName ' '+ AggregationNameValuePropertyPair) 
; 

objectName 
: (' '+ '-o' ' '+ validChar+) 
; 

AggregationNameValuePropertyPair 
: (' '+ '-n' ' '+ 'queuePrefix' ' '+ '-v' ' '+ queuePrefixValue)? 
    (' '+ '-n' ' '+ 'timeoutSeconds' ' '+ '-v' ' '+ timeoutSecondsValue)? 
; 

// I'm not satisfied with this rule as it means at least two digits are mandatory 
//Couldn't see how to use regex or semantic predicates which appear to offer a solution 
queuePrefixValue 
: validChar (validChar | '.')? (validChar | '.')? (validChar | '.')? (validChar | '.')? (validChar | '.')? (validChar | '.')? validChar 
; 
timeoutSecondsValue //a positive integer 
: ('0'..'9')+ 
; 

//This char list is just a temporary subset which eventually needs to reflect all the WebSphere acceptable characters, apart from the dot '.' 
validChar 
: (('a'..'z')|('A'..'Z')|('0'..'9')) 
; 

回答

0

你使用,你應該使用詞法語法規則規則而不是。 .dot meta-char)和..範圍 meta-char)在解析器規則中的行爲與在詞法分析器規則中的行爲不同。在解析器規則中,.匹配任何令牌(在詞法規則中它們與任何字符匹配),並且..將匹配令牌範圍,不是字符範圍,因爲您期望它們匹配!

因此,請queuePrefixValue一個詞法規則(讓它以大寫字母開頭:QueuePrefixValue),並使用fragment規則在適當情況下。你QueuePrefixValue看起來是這樣的:

QueuePrefixValue 
: StartEndCH ((((((CH? CH)? CH)? CH)? CH)? CH)? StartEndCH)? 
; 

fragment StartEndCH 
: 'a'..'z' 
| 'A'..'Z' 
| '0'..'9' 
; 

fragment CH 
: '.' 
| StartEndCH 
; 

所以,這或多或少地回答你的問題:沒有,有限制的令牌有一定量的字符沒有一個語言 - 沒有方式具體的謂詞。請注意,我上面的建議並不含糊(你的QueuePrefixValue不明確),我的也接受單個字符值。

HTH


Practical difference between parser rules and lexer rules in ANTLR?

What does "fragment" mean in ANTLR?