http://www.cs.chalmers.se/Cs/Research/Language-technology/BNFC/

我应该如何编写带有标签的BNF以获得BNFC为我生成INI解析器?

到目前为止,我才刚到!

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我被卡住了...

最佳答案

我问了一位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

(未选中,不知道它是否有效,只是给出一个想法!)

10-01 06:11