是否有任何配置文件读/写C代码生成器?

时间:2022-03-23 09:40:48

I know how to generate a C scanner code with flex or bison, but unluckily, I need a C code to read && -write- configure file, but I can not generate such code with flex or bison, May be I can use configure file read/write library, but I think it's not flexible when I want custom the format of configure file, so any tips?

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

1 个解决方案

#1


1  

I know of no such dedicated tool for that, simply because it's not really that hard a job.

我知道没有这样的专用工具,仅仅因为它并不是那么难的工作。

The reason you have lexical and semantic analysis on input is because you have to turn something complex (free form text with the possibility of errors) into something simple (in-memory representation with no errors).

你对输入进行词汇和语义分析的原因是因为你必须把一些复杂的东西(带有错误可能性的*格式文本)变成简单的东西(没有错误的内存中表示)。

Going the other way is usually much simpler because you can simply step through your in-memory structures and output their string representations. A simplified example, let's say your config file has the line:

另一种方式通常更简单,因为您只需单步执行内存中的结构并输出其字符串表示形式。一个简化的例子,假设您的配置文件包含以下行:

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

to create an array called xyzzy with five elements.

用五个元素创建一个名为xyzzy的数组。

On input, you have to tokenise (lexical analysis) the character stream into something like:

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

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

and then use semantic analysis to get that into a form you can use within your program, such as a structure:

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

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

Now, to get that back out to the configuration file is relatively easy. You just walk through all your in-memory structure, such as with:

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

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

So you can see that the act of writing to a configuration file is a lot easier than reding from it.

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

#1


1  

I know of no such dedicated tool for that, simply because it's not really that hard a job.

我知道没有这样的专用工具,仅仅因为它并不是那么难的工作。

The reason you have lexical and semantic analysis on input is because you have to turn something complex (free form text with the possibility of errors) into something simple (in-memory representation with no errors).

你对输入进行词汇和语义分析的原因是因为你必须把一些复杂的东西(带有错误可能性的*格式文本)变成简单的东西(没有错误的内存中表示)。

Going the other way is usually much simpler because you can simply step through your in-memory structures and output their string representations. A simplified example, let's say your config file has the line:

另一种方式通常更简单,因为您只需单步执行内存中的结构并输出其字符串表示形式。一个简化的例子,假设您的配置文件包含以下行:

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

to create an array called xyzzy with five elements.

用五个元素创建一个名为xyzzy的数组。

On input, you have to tokenise (lexical analysis) the character stream into something like:

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

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

and then use semantic analysis to get that into a form you can use within your program, such as a structure:

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

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

Now, to get that back out to the configuration file is relatively easy. You just walk through all your in-memory structure, such as with:

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

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

So you can see that the act of writing to a configuration file is a lot easier than reding from it.

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