我如何定义使用BNFC INI文件语法?
-
22-07-2019 - |
题
http://www.cs.chalmers.se/Cs /研究/语言 - 技术/ 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开发者之一,在这里引用他的回答是:
空间的字符,如换行是 不能很好地支持在令牌,因为 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
(不检查,不知道它的工作原理,只给一个想法!)
不隶属于 StackOverflow