我目前正在嘗試編寫一個Treetop語法來解析簡單遊戲格式文件,並且目前它的工作大部分都在進行中。但是,有幾個問題出現了。Treetop SGF解析
- 我不確定如何實際訪問Treetop在解析後生成的結構。
- 有沒有更好的方式來處理捕捉所有字符比我的字符規則?
有一種情況是我似乎無法正確寫入。
C [PLAYER1 [4K \]:您好player2 [3K \]:嗨]
我不能換我周圍的頭如何處理C []節點的嵌套結構與[]在裏面。
以下是我目前的進展情況。
SGF-grammar.treetop
grammar SgfGrammar
rule node
'(' chunk* ')' {
def value
text_value
end
}
end
rule chunk
';' property_set* {
def value
text_value
end
}
end
rule property_set
property ('[' property_data ']')*/property '[' property_data ']' {
def value
text_value
end
}
end
rule property_data
chars '[' (!'\]' .)* '\]' chars/chars/empty {
def value
text_value
end
}
end
rule property
[A-Z]+/[A-Z] {
def value
text_value
end
}
end
rule chars
[a-zA-Z0-9_/\-:;|'"\\<>(){}[email protected]#$%^&\*\+\-,\.\?!= \r\n\t]*
end
rule empty
''
end
end
而我的測試的情況下,目前不包括C []具有上述嵌套支架問題節點:
example.rb
require 'rubygems'
require 'treetop'
require 'sgf-grammar'
parser = SgfGrammarParser.new
parser.parse("(;GM[1]FF[4]CA[UTF-8]AP[CGoban:3]ST[2]
RU[Japanese]SZ[19]KM[0.50]TM[1800]OT[5x30 byo-yomi]
PW[stoic]PB[bojo]WR[3k]BR[4k]DT[2008-11-30]RE[B+2.50])")
任何人在近一年後閱讀這篇文章的結果是:http://github.com/boj/kantan-sgf - 不是世界上最有效的東西,但是是一個很棒的TreeTop/SGF解析實驗。 – bojo 2010-03-04 03:10:11