7

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

我应该如何编写带标签的 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我被困住了......

4

1 回答 1

6

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

(没查过,不知道有没有用,只是给个思路!!)

于 2009-06-25T11:20:02.060 回答