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我被困住了......
最佳答案
我询问了一位 BNFC 开发人员并在此引用他的回复:
Space characters such as newlines are not well supported in tokens, because BNFC has a hard-wired lexer type "space". The idea is that spaces can't carry meaning in "well-behaved" languages. One of those restrictions that has made BNFC so simple... but you should be able solve this by using a preprocessor, e.g. parse input line by line.
例如:
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
(没查过,不知道有没有用,只是给个思路!!)
关于parsing - 如何使用 BNFC 定义 INI 文件语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1042441/