设计像C这样的编译器

Ale*_*dre 0 c compiler-construction syntax abstract-syntax-tree

我正在开发一个类似C的编译器,我想知道编译器如何与系统包含.

编译器读完整个代码,并在完成读取当前代码后,将所有包含在一个列表和解析器中的包含存储起来?

// file main.c
#include <stdio.h> // store in one list

// continue the parse ...
int main()
{
    return 0;
}
// now, read the includes
// after finish the includes parse, gen code of sources

// just a sample
// file stdio.h
#include <types.h> // store in list
#include <bios.h>  // store in list

void printf(...)
{
}

void scanf(...)
{
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我已经开发了一个系统(只测试)来读取包含,并停止解析,阅读包含...(这是一个恶心的代码,但是,工作......)(样本的链接) - > https: //gist.github.com/4399601

顺便说一句,阅读包含的最佳方法是什么...并使用包含文件?

Ola*_*che 5

#include,#define,#ifdef和类似物被称为单独的过程处理的预处理.它用#include包含的文件替换行.然后将得到的临时源文本提供给后来的传递,如tokenizerparser.