使用m4进行预处理 [英] Pre-Processing using m4

查看:170
本文介绍了使用m4进行预处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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

那谁把这行转换成上面的行?我们可以使用sedawk之类的工具执行相同的操作,但是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.

推荐答案

    在这种情况下,
  1. m4是将DEF转换为3的工具.的确,对于这种简单情况,sedawk可以达到相同的目的,但是m4是一个功能更强大的工具,因为它a)允许对宏进行参数化,b)包括条件,c)允许宏通过输入文件进行重新定义等等.例如,一个人可以在
  1. m4 is the tool that will convert DEF to 3 in this case. It is true that sed or awk could serve the same purpose for this simple case but m4 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 file for.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;

  1. 无法使用m4删除常规Pascal注释,但是创建一个宏以包含在处理过程中被"m4"删除 的注释很简单:
  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆