简体   繁体   English

预处理器 C 程序替换包含头文件

[英]Preprocessor C program to replace include headers

I have homework assignment where I need to write a program to replace include commands (assuming there are no standard library includes) of C file with the actual.h' file (performing the preprocessor's job).我有家庭作业,我需要编写一个程序来用actual.h'文件替换C文件的包含命令(假设没有标准库包含)(执行预处理器的工作)。

I thought using regex matching, but since we are only allowed to use ansi-C's standard libraries regex is not allowed.我想使用正则表达式匹配,但由于我们只允许使用 ansi-C 的标准库,所以不允许使用正则表达式。

I am not sure what is the efficient way to do so?我不确定这样做的有效方法是什么?

edit: there is no need to check for #defines or #ifdefs .编辑:无需检查#defines#ifdefs

I suggest you follow this guide to create your program:我建议您按照本指南创建程序:

  1. Open the target c file read only.以只读方式打开目标 c 文件。
  2. Open the destination file write only.打开目标文件只写。
  3. Create a function that you pass the input file and the output file, i will call this function "expand_function".创建一个 function 传递输入文件和 output 文件,我将调用此 function “expand_function”。
  4. In the expand_function read each byte of the input, and while reading each character check if it is a #, then test if it is a include keyword, and finally if all conditions matches open the filename that will be in front the include, then call again the expand_function with the new input file and the same output file (don't forget closing all open files), and finally continue the loop.在expand_function中读取输入的每个字节,并在读取每个字符时检查是否为#,然后测试是否为include关键字,最后如果所有条件都匹配,则打开将在include前面的文件名,然后调用再次使用新的输入文件和相同的 output 文件的 expand_function(不要忘记关闭所有打开的文件),最后继续循环。 If any conditions not match write all read bytes in the output file.如果任何条件不匹配,则将所有读取字节写入 output 文件中。
  5. Extra: Check if you are not in a dead loop of includes in the beginning of expand_function.额外:检查您是否没有在 expand_function 开头的包含死循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM