在C和C字符串拼接的实现++ [英] Implementation of string literal concatenation in C and C++

查看:730
本文介绍了在C和C字符串拼接的实现++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,这个问题同样适用于 C 和C ++

AFAIK, this question applies equally to C and C++

C 标准(5.1.1.2在C99标准草案)规定的转换阶段的步骤6中指出,相邻的字符串必须连接成一个单一的文字。

Step 6 of the "translation phases" specified in the C standard (5.1.1.2 in the draft C99 standard) states that adjacent string literals have to be concatenated into a single literal. I.e.

printf("helloworld.c" ": %d: Hello "
       "world\n", 10);

时当量(语法)为:

Is equivalent (syntactically) to:

printf("helloworld.c: %d: Hello world\n", 10);

但是,标准似乎并没有指定编译器的一部分必须处理这一点 - 它应该是preprocessor( CPP )或编译器本身。一些网上的研究告诉我,这个功能普遍预期由preprocessor(的源1 源#2 和有更多),这是很有意义的。

However, the standard doesn't seem to specify which part of the compiler has to handle this - should it be the preprocessor (cpp) or the compiler itself. Some online research tells me that this function is generally expected to be performed by the preprocessor (source #1, source #2, and there are more), which makes sense.

但是,运行 CPP 在Linux中显示, CPP 并没有做到这一点:

However, running cpp in Linux shows that cpp doesn't do it:

eliben@eliben-desktop:~/test$ cat cpptest.c 
int a = 5;

"string 1" "string 2"
"string 3"

eliben@eliben-desktop:~/test$ cpp cpptest.c 
# 1 "cpptest.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "cpptest.c"
int a = 5;

"string 1" "string 2"
"string 3"

所以,我的问题是:应该在哪里语言的这一功能进行处理,在preprocessor或编译器本身

So, my question is: where should this feature of the language be handled, in the preprocessor or the compiler itself?

或许没有单一的好的答案。根据经验,知名的编译器,和一般良好的工程实践启发式的答案将是AP preciated。

Perhaps there's no single good answer. Heuristic answers based on experience, known compilers, and general good engineering practice will be appreciated.

P.S。如果你想知道我为什么关心这个...我想弄清楚是否我的 Python的基于C语言解析器应该处理字符串连接(它没有这样做,此刻),或者让 CPP 这之前假定运行吧。

P.S. If you're wondering why I care about this... I'm trying to figure out whether my Python based C parser should handle string literal concatenation (which it doesn't do, at the moment), or leave it to cpp which it assumes runs before it.

推荐答案

没有指定preprocessor对编译器的标准,它只是指定了翻译的你已经注意到阶段。传统上,相位1至4个是在preprocessor,阶段5虽然7中的编译器,和相8的连接体 - 但由标准非必需的那

The standard doesn't specify a preprocessor vs. a compiler, it just specifies the phases of translation you already noted. Traditionally, phases 1 through 4 were in the preprocessor, Phases 5 though 7 in the compiler, and phase 8 the linker -- but none of that is required by the standard.

这篇关于在C和C字符串拼接的实现++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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