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 = 20Parse Successful!
[Abstract Syntax]
Ini [Sect (ID "name") [Bind (ID "y") (ID "20")]]
[Linearized tree]
[name]y = 20[name]
x = 10
#y = 20Parse Successful!
[Abstract Syntax]
Ini [Sect (ID "name") [Bind (ID "x") (ID "10")]]
[Linearized tree]
[name]x = 10o__O我卡住了..。
发布于 2009-06-25 11:20:02
我问了其中一位BNFC开发人员,并在这里引用了他的回答:
空间字符(如换行符)在令牌中不受很好的支持,因为BNFC具有硬连接的lexer类型" Space“。这个想法是,在“行为良好”的语言中,空格不能承载意义。其中一个限制使得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(没有检查,不知道它是否有效,只是想给出一个主意!)
https://stackoverflow.com/questions/1042441
复制相似问题