1

我知道如何用 flex 或 bison 生成 C 扫描仪代码,但不幸的是,我需要一个 C 代码来读取 && -write- 配置文件,但我无法用 flex 或 bison 生成这样的代码,也许我可以使用配置文件读/写库,但是当我想自定义配置文件的格式时,我认为它不灵活,所以有什么提示吗?

4

1 回答 1

1

我知道没有这样的专用工具,仅仅是因为这并不是一项真正的工作。

您对输入进行词法和语义分析的原因是您必须将复杂的东西(可能出现错误的自由格式文本)变成简单的东西(没有错误的内存表示)。

采用另一种方式通常要简单得多,因为您可以简单地遍历内存结构并输出它们的字符串表示形式。一个简化的示例,假设您的配置文件具有以下行:

define xyzzy integer size 5 is 1 3 5 7 9 ;

创建一个xyzzy包含五个元素的数组。

在输入时,您必须将字符流标记化(词法分析)为:

keyword:define
name:xyzzy
keyword:integer
keyword:size
constant:5
keyword:is
constant:1
constant:3
constant:5
constant:7
constant:9
keyword:semicolon

然后使用语义分析将其转换为您可以在程序中使用的形式,例如结构:

type = array
name = xyzzy
underlyingtype = integer
size = 5
element[1..5] = {1,3,5,7,9}

现在,将其恢复配置文件中相对容易。您只需遍历所有内存结构,例如:

for each in-memory-thing imt:
    if imt.type is array:
        output "define ", imt.name, " ", imt.underlyingtype
        output " size ", imt.size, " is "
        for i = 1 to imt.size inclusive:
            output imt.element[i], " "
        output " ;" with newline
    fi
    // Handle other types of imt here
rof

因此,您可以看到写入配置文件的操作比从配置文件中重新写入要容易得多。

于 2013-05-21T03:08:20.367 回答