使用m4进行预处理 [英] Pre-Processing using m4
问题描述
I am writing a pre-processor for Free-Pascal (Course Work) using m4. I was reading the thread at stackoverflow here and from there reached a blog which essentially shows the basic usage of m4
for pre-processing for C
. The blogger uses a testing C
file test.c.m4
like this:
#include
define(`DEF', `3')
int main(int argc, char *argv[]) {
printf("%d\n", DEF);
return 0;
}
并使用m4
生成像这样处理的C
文件.
and generates processed C
file like this using m4
, which is fine.
$ m4 test.c.m4 > test.c
$ cat test.c
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("%dn", 3);
return 0;
}
我的疑问是:
1.程序员将在下面的行中编写代码
My doubts are:
1. The programmer will write the code where the line
define(`DEF', `3')
将是
#define DEF 3
那谁把这行转换成上面的行?我们可以使用sed
或awk
之类的工具执行相同的操作,但是m4
的用途是什么. m4
所做的事情也可以使用sed
来实现.
如果有人可以告诉我如何将程序员的代码转换为m4
可以使用的文件,这将非常有帮助.
2.我在使用m4
时遇到了另一个问题.像C
这样的语言的注释在预处理之前已被删除,因此可以使用m4
完成吗?为此,我正在寻找m4
中的命令,通过这些命令我可以使用正则表达式替换注释,并找到了regexp()
,但是它需要将字符串替换为自变量,在这种情况下不可用.那么如何实现呢?
抱歉,这是一个幼稚的问题.我阅读了m4
的文档,但找不到解决方案.
then who converts this line to the above line? We can use tool like sed
or awk
to do the same but then what is the use of m4
. The thing that m4
does can be implemented using sed
also.
It would be very helpful if someone can tell me how to convert the programmer's code into a file that can be used by m4
.
2. I had another issue using m4
. The comment in languages like C
are removed before pre-processing so can this be done using m4
? For this I was looking for commands in m4
by which I can replace the comments using regex and I found regexp()
, but it requires the string to be replaced as argument which is not available in this case. So how to achieve this?
Sorry if this is a naive question. I read the documentation of m4
but could not find a solution.
推荐答案
-
在这种情况下,
-
m4
是将DEF
转换为3
的工具.的确,对于这种简单情况,sed
或awk
可以达到相同的目的,但是m4
是一个功能更强大的工具,因为它a)允许对宏进行参数化,b)包括条件,c)允许宏通过输入文件进行重新定义等等.例如,一个人可以在
m4
is the tool that will convertDEF
to3
in this case. It is true thatsed
orawk
could serve the same purpose for this simple case butm4
is a much more powerful tool because it a) allows macros to be parameterized, b) includes conditionals, c) allows macros to be redefined through the input file, and much more. For example, one could write (in the filefor.pas.m4
, inspired by ratfor):
define(`LOOP',`for $1 := 1 to $2 do
begin')dnl
define(`ENDLOOP',`end')dnl
LOOP(i,10)
WriteLn(i);
ENDLOOP;
...由m4 for.pas.m4
处理后,将为Pascal编译器准备以下输出:
... which produces the following output ready for the Pascal compiler when processed by m4 for.pas.m4
:
for i := 1 to 10 do
begin
WriteLn(i);
end;
- 无法使用
m4
删除常规Pascal注释,但是创建一个宏以包含在处理过程中被"m4"删除 的注释很简单:
- Removing general Pascal comments using
m4
would not be possible but creating a macro to include a comment that will be deleted by `m4' in processing is straightforward:
define(`NOTE',`dnl')dnl
NOTE(`This is a comment')
x := 3;
...产生:
x := 3;
要由m4
扩展的常用宏可以放在一个通用文件中,该文件可以包含在使用它们的任何Pascal文件的开头,从而不必在每个Pascal中定义所有必需的宏文件.请参见 m4手册中的include (file)
.
Frequently-used macros that are to be expanded by m4
can be put in a common file that can be included at the start of any Pascal file that uses them, making it unnecessary to define all the required macros in every Pascal file. See include (file)
in the m4 manual.
这篇关于使用m4进行预处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!