部分预处理的实用程序 [英] Utility to partially preprocess

查看:52
本文介绍了部分预处理的实用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要部分预处理C代码。通过''部分''我

意味着我需要定义一些宏(到实用程序)并让它

只预处理那些宏,保留其他所有内容。一个
编译器的预处理器不会这样做,因为它还会处理代码中(和标准头文件中)定义的宏和/ b $ b并去掉

一切。例如,给定的来源如:


#include< stdio.h>

#if defined(AAA)

void doSomeCode(无效)

{

#如果AAA> 1

printf(做点什么%d \ n,AAA);

#else

printf(做其他事情\ n");

#endif

}

#else

#include< math.h> ;

#define PI(4 * atan(1.0))

无效doSomeCode(无效)

{

printf(完全不同的东西 - PI =%f \ n,PI);

}

#endif


我希望输出为:


AAA未定义:


#include< stdio.h>

#include< math.h>

#define PI(4 * atan(1.0))

void doSomeCode(void)

{

printf(完全不同的东西 - PI =%f \ n,PI);

}


AAA = 0或1


#include< stdio.h>

void doSomeCode(void)

{

printf(做其他事情\ n);

}


AAA = 2


#in clude< stdio.h>

void doSomeCode(void)

{

printf(" do something%d \ n", 2);

}


好​​吧,这是一个愚蠢的例子,但这个想法应该表明。请注意,使用

C预处理器会扩展头文件,这不是
想要的......


一个目的是采取一些非常苛刻的代码,

许多特定的条件编译,不再存在
(特别是预标准所需的东西)和

非标准编译器),并将其减少为更简单的情况。是的,这个

可以手动完成。但这很容易出错并且非常耗时

且代码量很大。


另一个目的是作为预解析头的一般工具和代码

文件用于分发,这样目标系统就不必包含>
包括config.h (或等效)每次。


我的问题是:


这样的实用程序是否作为自由软件存在?许可证并不重要,因为它与其他自由软件兼容 - (L)GPL,BSD和

''Infozip''特许。源代码为标准C所以它将在任何地方编译
(因此不能依赖于yacc或lex,除非生成的

代码是标准C)。 br />

如果没有,我需要自己编写。如果它确实已经存在,我会

而不是重新发明轮子......


Chris C

I have a requirement to partially preprocess C code. By ''partially'' I
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. A
compiler''s preprocessor won''t do this, since it will also process macros
defined in the code (and in standard header files) and strip out
everything. For instance, given source like:

#include <stdio.h>
#if defined(AAA)
void doSomeCode(void)
{
# if AAA > 1
printf("do something %d\n", AAA);
# else
printf("do something else\n");
# endif
}
#else
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void)
{
printf("something completely different - PI = %f\n", PI);
}
#endif

I would want the output to be:

AAA undefined:

#include <stdio.h>
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void)
{
printf("something completely different - PI = %f\n", PI);
}

AAA = 0 or 1

#include <stdio.h>
void doSomeCode(void)
{
printf("do something else\n");
}

AAA = 2

#include <stdio.h>
void doSomeCode(void)
{
printf("do something %d\n", 2);
}

OK, it''s a silly example, but the idea should show. Note that using the
C preprocessor would have expanded the header files, which is not
wanted...

One purpose is to take some code which has got horribly crufty, with
lots of specific conditional compilation for things which no longer
exist (in particular things which were needed for pre-standard and
non-standard compilers), and reduce it for a simpler case. Yes, this
can be done "by hand" but this is prone to error and very time-consuming
with a large amount of code.

Another purpose is as a general utility for pre-parsing header and code
files for distribution, so that the destination system does not have to
include "config.h" (or equivalent) every time.

My question is:

Does such a utility exist as Free Software? Licence is unimportant as
long as it is compatible with other Free Software -- (L)GPL, BSD and
''Infozip'' licences particularly. Source code as Standard C so it will
compile anywhere (so can''t depend on yacc or lex, unless the generated
code from them is Standard C).

If not, I need to write my own. If it does exist already, I would
rather not re-invent the wheel...

Chris C

推荐答案

在文章< sl ****************** @ ccserver.keris.net>

Chris Croughton< ch *** @ keristor.net>写道:
In article <sl******************@ccserver.keris.net>
Chris Croughton <ch***@keristor.net> wrote:
我要求部分预处理C代码。通过''部分''我意味着我需要定义一些宏(对实用程序)并让它只预处理那些宏,保留其他所有内容。 ...
I have a requirement to partially preprocess C code. By ''partially'' I
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. ...




如果您只想取出某些#ifdef',那么unifdef实用工具

(其中毫无疑问有多个版本)可以解决问题。

对于更复杂的东西,请查找scpp。程序 -

选择性C预处理程序,在20世纪80年代的一段时间内编写在

之前的ANSI C.

-

In-Real-Life:风河系统Chris Torek

美国犹他州盐湖城(40°39.22''N,111°50.29''W)+ 1 801 277 2603

电子邮件:忘了它 http ://web.torek.net/torek/index.html

由于垃圾邮件发送者,阅读电子邮件就像在垃圾中搜索食物一样。



If you only want certain #ifdef''s taken out, the "unifdef" utility
(of which there are no doubt multiple versions) will do the trick.
For something more complicated, look for the "scpp" program -- the
"selective C preprocessor", written some time in the 1980s in
pre-ANSI C.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


2004年11月1日19:34:11 GMT,Chris Torek

< no **** @ torek.net>写道:
On 1 Nov 2004 19:34:11 GMT, Chris Torek
<no****@torek.net> wrote:
在文章< sl ****************** @ ccserver.keris.net>
Chris Croughton < CH *** @ keristor.net>写道:
In article <sl******************@ccserver.keris.net>
Chris Croughton <ch***@keristor.net> wrote:
我要求部分预处理C代码。通过''部分''我意味着我需要定义一些宏(对实用程序)并让它只预处理那些宏,保留其他所有内容。 ...
I have a requirement to partially preprocess C code. By ''partially'' I
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. ...



如果您只想取出某些#ifdef',那么unifdef将会被取消。实用工具
(其中毫无疑问有多个版本)可以解决问题。
对于更复杂的东西,请查找scpp。程序 -
选择性C预处理器,在1980年代在ANSI C之前编写。



If you only want certain #ifdef''s taken out, the "unifdef" utility
(of which there are no doubt multiple versions) will do the trick.
For something more complicated, look for the "scpp" program -- the
"selective C preprocessor", written some time in the 1980s in
pre-ANSI C.




谢谢, scpp的男人听起来像我想要的,但是唯一的

副本(其中3个)我找不到编译(很多lex和

lex-生成代码错误)。我从搜索结果中收集起来,而不仅仅是一个用现代实用程序找到它的人(我正在使用Debian

GNU / Linux' 'woody''(稳定),gcc 2.95.4,lex 2.5.4,yacc(GNU bison)

1.35)。


然而,阅读它给了我写自己的想法。自从我在纯C中编写C语言分析器以来,已经很长一段时间了,最​​后一次

是预先ANSI,可能与scpp大致相同,但有限的

预处理器也不算太糟糕(只有一半的运营商有效,而且b $ b我根本不用担心程序结构)。


(非gcc)C编译器测试兼容性的任何建议?


Chris C



Thanks, the man for scpp sounds like about what I want, but the only
copies (3 of them) I could find won''t compile (lots of lex and
lex-generated code errors). I gather from searches that I''m not the
only one to find that with ''modern'' utilities (I''m using Debian
GNU/Linux ''woody'' (stable), gcc 2.95.4, lex 2.5.4, yacc (GNU bison)
1.35).

However, reading about it has given me ideas for writing my own. It''s
been quite a long time since I wrote a C parser in pure C, the last time
was pre-ANSI, probably about the same time as scpp, but a limited
pre-processor is not too bad (only half of the operators are valid, and
I don''t need to worry about program structure at all).

Any recommendations for (non-gcc) C compilers to test compatibility?

Chris C


Chris Croughton写道:
Chris Croughton wrote:
我需要部分预处理C代码。通过''部分''我意味着我需要定义一些宏(对实用程序)并让它只预处理那些宏,保留其他所有内容。编译器的预处理器不会这样做,因为它还会处理代码中定义的宏(以及标准头文件中)并删除所有内容。例如,给定的来源如:

#include< stdio.h>
#if defined(AAA)
void doSomeCode(void)
{
#if AAA> 1
printf(做某事%d \ n,AAA);
#else
printf(做其他事情\ n);
#endif
}
#else
#include< math.h>
#define PI(4 * atan(1.0))
void doSomeCode(void)
{
printf(完全不同的东西 - PI =%f \ n,PI);
}
#endif

我想要输出成为:

AAA undefined:

#include< stdio.h>
#include< math.h>
#define PI( 4 * atan(1.0))
无效doSomeCode(无效)
{/> printf(完全不同的东西 - PI =%f \ n,PI);
}

AAA = 0或1

#include< stdio.h>
void doSomeCode(void)
{
printf(" ;做别的事情\ n");
}

AAA = 2

#include< stdio.h>
void doSomeCode(void )
{
printf(do somethi ng%d \ n",2);
}
好吧,这是一个愚蠢的例子,但这个想法应该表明。请注意,使用
C预处理器会扩展头文件,这不是想要的......

一个目的是采取一些非常苛刻的代码,
对于不再存在的事物(特别是预标准和非标准编译器所需的事物)进行了大量特定的条件编译,并将其简化为更简单的情况。是的,这个
可以手工完成。但是这很容易出错并且耗费大量代码。

另一个目的是作为预解析头文件和代码文件的通用工具分发,以便目标系统不必包含config.h和config.h。 (或同等的)每一次。

我的问题是:

这样的实用程序是否像自由软件一样存在?许可证并不重要,因为它与其他自由软件兼容 - (L)GPL,BSD和
'Infozip''许可证。源代码为标准C,因此它将在任何地方编译(因此不能依赖于yacc或lex,除非生成的代码是标准C)。

如果不,我需要自己写。如果它确实存在,我宁愿不重新发明轮子......

Chris C
I have a requirement to partially preprocess C code. By ''partially'' I
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. A
compiler''s preprocessor won''t do this, since it will also process macros
defined in the code (and in standard header files) and strip out
everything. For instance, given source like:

#include <stdio.h>
#if defined(AAA)
void doSomeCode(void)
{
# if AAA > 1
printf("do something %d\n", AAA);
# else
printf("do something else\n");
# endif
}
#else
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void)
{
printf("something completely different - PI = %f\n", PI);
}
#endif

I would want the output to be:

AAA undefined:

#include <stdio.h>
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void)
{
printf("something completely different - PI = %f\n", PI);
}

AAA = 0 or 1

#include <stdio.h>
void doSomeCode(void)
{
printf("do something else\n");
}

AAA = 2

#include <stdio.h>
void doSomeCode(void)
{
printf("do something %d\n", 2);
}

OK, it''s a silly example, but the idea should show. Note that using the
C preprocessor would have expanded the header files, which is not
wanted...

One purpose is to take some code which has got horribly crufty, with
lots of specific conditional compilation for things which no longer
exist (in particular things which were needed for pre-standard and
non-standard compilers), and reduce it for a simpler case. Yes, this
can be done "by hand" but this is prone to error and very time-consuming
with a large amount of code.

Another purpose is as a general utility for pre-parsing header and code
files for distribution, so that the destination system does not have to
include "config.h" (or equivalent) every time.

My question is:

Does such a utility exist as Free Software? Licence is unimportant as
long as it is compatible with other Free Software -- (L)GPL, BSD and
''Infozip'' licences particularly. Source code as Standard C so it will
compile anywhere (so can''t depend on yacc or lex, unless the generated
code from them is Standard C).

If not, I need to write my own. If it does exist already, I would
rather not re-invent the wheel...

Chris C




使用你写的lcc-win32编译系统:


browsegen -showifdeflines foo.c


它将在标准输出中写入行号<文件foo.c中#ifdefed行的
行。

这允许IDE显示灰色的ifdefed行,

但是你当然可以根据需要使用输出。

http://www.cs.virginia.edu/~lcc-win32


这篇关于部分预处理的实用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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