parsing - 如何使用 BNFC 定义 INI 文件语法?

标签 parsing haskell bnf context-free-grammar ebnf

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/

相关文章:

java - Jsoup 解析超出必要的字段,在 java 中使用

haskell - 创建数据类型时派生意味着什么?

haskell - 如何创建线程池?

python - 是否有适用于 Python 的标准词法分析器/解析器工具?

grammar - 不明白为什么 Bison 扔 "Rules useless in parser due to conflicts"

windows cmd - 解析文件,输出字符范围,查找唯一值

regex - Python 3如何使用正则表达式在两点之间获取字符串?

python - 如何从一行之间删除换行符而不从行尾删除换行符python?

haskell - 递归方案允许递归调用之间的依赖关系(有序变形?)

java - Java方法声明中的方括号是什么意思?