Pre-Processing using m4

走远了吗. 提交于 2019-12-06 08:17:10
  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;

... 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. 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;

... produces:

    x := 3;

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!