2009-06-25 74 views
6

http://www.cs.chalmers.se/Cs/Research/Language-technology/BNFC/如何使用BNFC定義INI文件語法?

我該怎麼寫我的標記BNF來讓BNFC爲我生成一個INI解析器?

我只得到了這麼多o__O!

entrypoints File ; 

comment "#" ; 

token ID (letter | digit | ["-_'"])+ ; 

Ini. File ::= [Section] ; 
Sect. Section ::= "[" ID "]" [Statement] ; 
Bind. Statement ::= ID "=" ID ; 

separator Statement "\n" ; 
terminator Section "" ; 

[name] 
#x = 10 
y = 20 

Parse Successful! 

[Abstract Syntax] 

Ini [Sect (ID "name") [Bind (ID "y") (ID "20")]] 

[Linearized tree] 

[name]y = 20 

[name] 
x = 10 
#y = 20 

Parse Successful! 

[Abstract Syntax] 

Ini [Sect (ID "name") [Bind (ID "x") (ID "10")]] 

[Linearized tree] 

[name]x = 10 

O__O我卡住了...

+0

你下一步想做什麼呢?看起來像這樣幾乎可以滿足解析ini文件 – 2009-06-25 06:47:04

回答

5

我問BNFC開發者之一,在這裏引用他的回答是:

空格字符,如換行符 不令牌很好的支持,因爲 BNFC有一個硬連線詞法型 「空間」 。這個想法是,空間不能 在「良好行爲」 語言中攜帶意義。其中一個限制 使BNFC變得如此簡單......但是 您應該能夠通過使用預處理器(例如 )來解決這個問題。按行解析輸入行 。


例如像:

entrypoints File ; 

comment "#" ; 

token ID (letter | digit | ["-_'"])+ ; 

Ini. File ::= [Section] ; 
Sect. Section ::= "[" ID "]" [Statement] ; 
Bind. Statement ::= ID "=" ID ; 

separator Statement "//" ; 
terminator Section "//" ; 

閱讀:

[name] 
x = 10 
y = 20 

預處理:

[name]// 
x = 10// 
y = 20// 

解析:

Ini [Sect (ID "name") [Bind (ID "x") (ID "10"), Bind (ID "y") (ID "20")]] 

變換:

          ↓      ↓ 
Ini [Sect (ID "name") [Bind (ID "x") (ID "0"), Bind (ID "y") (ID "0")]] 

寫:

[name]// 
x = 0// 
y = 0// 

後處理:

[name] 
x = 0 
y = 0 

(不檢查,不知道它的工作原理,只給一個想法!)