设计像C这样的编译器 [英] Design a compiler like C

查看:65
本文介绍了设计像C这样的编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:)

我正在开发一个类似C的编译器,我想知道它是如何工作的包含系统...我的意思是,编译器如何使用系统包括



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



Hi :)
I''m developing a C like compiler and I whant to know, how it''s work the include system... I mean, how the compiler works with the system include

The compiler read the entire code, and stores all includes found in one list and parser the includes, after finish the reading the current code?

// 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(...)
{
}





顺便说一句,我开发了一个系统(仅测试)阅读包含,并停止解析,阅读包括...(这是一个恶心的代码,但工作......)

(样本链接)

https://gist.github.com/4399601 [ ^ ]

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



Btw, I have developd an system ( only test ) to read the includes and, stop the parse, to read the include... ( it''s a disgusting code, but, work... )
( link of sample )
https://gist.github.com/4399601[^]
Btw, What is the best way to read the includes... and work with includes files ??

推荐答案

大多数编译器使用预处理器来读取包含的标题,然后将所有引用,宏等转换为最终形式。然后将预处理器的转换输出馈送到编译器以转换为目标代码。互联网上有很多免费工具可以帮助您构建编译器,Google可以帮助您找到它们。
Most compilers use a preprocessor to read the included headers and then convert all references, macros etc. into their final form. The converted output of the preprocessor is then fed into the compiler for conversion to object code. There are quite a few free tools available on the internet to help with building compilers, and Google should help you find them.


如果您想解析和处理任何语言,您可能需要先阅读一些教科书。

例如 C-Programming Language [ ^ ],或 C11语言草案 [ ^ ]等



Eg后者指定 #include 语义如下:

[...]

6.10.2源文件包含

If you want to parse and process any language, you probably need to read some text books first.
E.g. C-Programming Language[^], or C11 Language Draft[^], etc.

E.g. the latter specifies the #include semantics as follows:
[...]
6.10.2 Source file inclusion


  1. 约束

    A #include 指令应标识可由

    实现处理的头文件或源文件。
  2. 语义

    格式的预处理指令

    #include< h-char-sequence>换行符

    搜索一系列实现定义的位置,以获得由< 和> 分隔符,并导致用标题的全部内容替换该

    指令。如何指定场所或标识

    标识是实现定义的。
  3. 形式的预处理指令

    #包括q-char-sequence换行符

    导致用指定的源文件的全部内容替换该指令

    by 分隔符之间的指定序列。以实现定义的方式搜索指定的源文件。如果不支持此搜索,或者搜索

    失败,该指令被重新处理,就像它读取了

    #include< h-char-sequence> new-line

    与原始

    指令中包含相同的序列(包括>字符,如果有的话)。
  4. [...]

  1. Constraints
    A #include directive shall identify a header or source file that can be processed by the
    implementation.
  2. Semantics
    A preprocessing directive of the form
    # include <h-char-sequence> new-line
    searches a sequence of implementation-defined places for a header identified uniquely by
    the specified sequence between the < and > delimiters, and causes the replacement of that
    directive by the entire contents of the header. How the places are specified or the header
    identified is implementation-defined.
  3. A preprocessing directive of the form
    # include "q-char-sequence" new-line
    causes the replacement of that directive by the entire contents of the source file identified
    by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search
    fails, the directive is reprocessed as if it read
    # include <h-char-sequence> new-line
    with the identical contained sequence (including > characters, if any) from the original
    directive.
  4. [...]





此外,一些回退被定义为避免infinte包含循环(例如,实现不需要支持超过15个嵌套包含级别等)。



通常,在包括< ...> 首先搜索系统标头,...搜索提供的 -I ... 路径优先。但如上所述,它取决于实现。检查目标编译器是否包含这些包含的语义。



请注意 #include 将文件添加到某个魔术列表中 - 它只是简单地用引用文件的内容替换 #include 行(就像你写的那样)包含文件的内容位于 #include 行的位置。



干杯

Andi



PS:如果你使用gcc -E -C选项,你会看到包含和其他预处理的效果。



Furthermore, some fallback is defined to avoid infinte inclusion loop (e.g. an implementaiton is not required to support more than 15 nested including levels, etc.).

Usually, including <...> searches first for system headers, "..." searches in the provided -I... paths first. But as stated above, it is implementation dependent. Check your target compiler for the implemented semantics of these includes.

Please note that the #include does not add the files in some "magic" list - it simply replaces the #include line literally by the content of the referenced file (as if you had written the content of the included file at the location of the #include line).

Cheers
Andi

PS: if you use with gcc the -E -C options, you see the effect of the inclusion and other preprocessing.


这篇关于设计像C这样的编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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