K& R预处理器中的令牌粘贴问题 [英] token pasting problem in K&R preprocessor

查看:51
本文介绍了K& R预处理器中的令牌粘贴问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这是关于clc的主题 - 它是关于C预处理器比语言本身更多

,更一般地说是关于K& R行为,并且

最具体的关于Sun cpp的原因,这就是我在那里交叉发布的原因。

下面的测试用例取自Imake设置(是的,旧的,我知道)。

有一个Concat()宏使用旧式/ ** /用于标记粘贴(我们不允许
假设ANSI所以## isn不允许)。我正在尝试粘贴

并展开两个宏X和Y.我无法弄清楚为什么它不会在这种情况下工作
;文字/ ** /有效,但宏没有。


%cat / tmp / Xc

#define X xxx

#define Y yyy

#define Concat(a,b)a / ** / b

X / ** / Y

Concat(X,Y)


%/ usr / ccs / lib / cpp / tmp / Xc

#1" /tmp/X.c" ;

[空白省略]

xxxyyy

XY


谢谢,

HT

I hope this is on-topic in c.l.c - it''s about the C preprocessor more
than the language per se, more generally about the K&R behavior, and
most specifically about the Sun cpp which is why I''ve cross posted there.

The test case below is taken from an Imake setup (yes, old, I know).
There''s a Concat() macro which uses old-style /**/ for token pasting (we
are not allowed to assume ANSI so ## isn''t allowed). I''m trying to paste
AND expand the two macros X and Y. I cannot figure out why it doesn''t
work in this scenario; the literal /**/ works but the macro doesn''t.

% cat /tmp/X.c
#define X xxx
#define Y yyy
#define Concat(a,b)a/**/b
X/**/Y
Concat(X,Y)

% /usr/ccs/lib/cpp /tmp/X.c
# 1 "/tmp/X.c"
[blank space elided]
xxxyyy
XY

Thanks,
HT

推荐答案

Henry Townsend schrieb:
Henry Townsend schrieb:
我希望这是clc的主题 - 它'关于C预处理器的内容比语言本身更多,更普遍的是关于K& R的行为,而且最具体的是关于Sun cpp,这就是我在那里交叉发布的原因。下面的测试用例取自Imake设置(是的,旧的,我知道)。
有一个Concat()宏使用旧式/ ** /进行令牌粘贴(我们不允许采用ANSI,因此不允许##。我正在尝试粘贴
并展开两个宏X和Y.我无法弄清楚为什么它在这种情况下不起作用;文字/ ** /有效,但宏没有。

%cat / tmp / Xc
#define X xxx
#define Y yyy
#定义Concat(a,b)a / ** / b
X / ** / Y
Concat(X,Y)

%/ usr / ccs / lib / cpp / tmp / Xc
#1" /tmp/X.c"
[空格已省略]
xxxyyy
XY
I hope this is on-topic in c.l.c - it''s about the C preprocessor more
than the language per se, more generally about the K&R behavior, and
most specifically about the Sun cpp which is why I''ve cross posted there.

The test case below is taken from an Imake setup (yes, old, I know).
There''s a Concat() macro which uses old-style /**/ for token pasting (we
are not allowed to assume ANSI so ## isn''t allowed). I''m trying to paste
AND expand the two macros X and Y. I cannot figure out why it doesn''t
work in this scenario; the literal /**/ works but the macro doesn''t.

% cat /tmp/X.c
#define X xxx
#define Y yyy
#define Concat(a,b)a/**/b
X/**/Y
Concat(X,Y)

% /usr/ccs/lib/cpp /tmp/X.c
# 1 "/tmp/X.c"
[blank space elided]
xxxyyy
XY




我没有K& R编译器,所以我只是猜测。


请注意,##有同样的问题。在那里,你通过额外的间接级别来解决它,即你通过包装Concat来强制扩展宏参数。一次:

#define X xxx

#define Y yyy

#define con_cat(a,b)a ## b

#define CONCAT(a,b)con_cat(a,b)

con_cat(X,Y)

CONCAT(X,Y)

导致

XY

xxxyyy


我也会为K& R尝试相同的事情。


HTH

Michael

-

电子邮件:我的是/ at / gmx / dot / de address。



I don''t have a K&R compiler, so I am only guessing.

Note that you have the same problem for ##. There, you solve
it by an additional "level of indirection", i.e. you force
expansion of the macro arguments by wrapping "Concat" once:
#define X xxx
#define Y yyy
#define con_cat(a,b) a##b
#define CONCAT(a,b) con_cat(a,b)
con_cat(X,Y)
CONCAT(X,Y)
leads to
XY
xxxyyy

I''d try the same for K&R, too.

HTH
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Henry Townsend写道:

[...]
Henry Townsend wrote:
[...]
%cat / tmp / Xc
#define X xxx
#define Y yyy
#define Concat(a,b)a / ** / b
X / ** / Y
Concat(X,Y)

%/ usr / ccs / lib / cpp / tmp / Xc
#1" /tmp/X.c"
[空格已删除]
xxxyyy
XY
% cat /tmp/X.c
#define X xxx
#define Y yyy
#define Concat(a,b)a/**/b
X/**/Y
Concat(X,Y)

% /usr/ccs/lib/cpp /tmp/X.c
# 1 "/tmp/X.c"
[blank space elided]
xxxyyy
XY




使用MSVC6(cl / P usenet.c),我得到:

= =========

xxxyyy

xxx yyy

==========


使用gcc版本" egcs-2.91.66" (gcc -E usenet.c),我得到:


==========

#1" usenet.c"

xxx yyy

xxx yyy

==========


我无法想象你是如何获得XY的。为第二行。是不是

预处理器必须将X和Y扩展为#define''d值?


你可以运行C编译器,询问它只是预处理文件,

而不是直接运行cpp?然后会发生什么?


现在,为什么MSVC给出了xxxyyy对于第一行,gcc给出了

" xxx yyy,我不知道。有人可以告诉我哪个是正确的

哪个是错误的? (或者他们都是正确的?)


-

+ ---------------- --------- + -------------------- + ------------------- ---- +

| Kenneth J. Brody | www.hvcomputer.com | #include |

| kenbrody / at\spamcop.net | www.fptech.com | < std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------- +

不要给我发电子邮件:< ; mailto:Th ************* @ gmail.com>



Using MSVC6 (cl /P usenet.c), I get:
==========
xxxyyy
xxx yyy
==========

Using gcc version "egcs-2.91.66" (gcc -E usenet.c), I get:

==========
# 1 "usenet.c"
xxx yyy
xxx yyy
==========

I can''t imagine how you get "XY" for the second line. Doesn''t the
preprocessor have to expand the X and Y into their #define''d values?

Can you run the C compiler, asking it to only preprocess the file,
rather than run cpp directly? What happens then?

Now, as to why MSVC gives "xxxyyy" for the first line, and gcc gives
"xxx yyy", I don''t know. Can someone here tell me which is "right"
and which is "wrong"? (Or are they both "right"?)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>





Henry Townsend在2006年6月6日14:52写道:


Henry Townsend wrote On 06/19/06 14:52,:
[...]
这是一个使用旧式/ **的Concat()宏/用于标记粘贴(我们不允许使用ANSI,因此不允许##)。 [...]
[...]
There''s a Concat() macro which uses old-style /**/ for token pasting (we
are not allowed to assume ANSI so ## isn''t allowed). [...]




ANSI C已经存在了不到17美元的b $ b年,所以也许你在采用它时要谨慎有理由。

作为一个闲置的兴趣点,你认为在采用新标准之前必须经过几十年才能确定?

你如何计划从FORTRAN II搬到

FORTRAN IV?


挑战:没有参考报纸,年历,

维基百科或其他此类消息来源,指出三个人是联合国安理会成员国国家元首。

时间ANSI C是采用。


挑战:自从采用ANSI C以来,int是否足以计算数量

秒?小时数?

天数?


挑战:二十五个字或更少,比较和对比

你的组织的标准采用率

质子衰变。


(好吧,好吧 - 我有一些乐趣付出你的代价。

但严肃地说,我恳请你考虑在前沿的十年内前进到
。曾经有充分的理由

以适应ANSI之前的实施,但随着时间的推移,它们的优点已经随着时间的推移逐渐减少,现在大约相等于一个葬礼餐的价值。法老王的坟墓。你真的,

真的应该认真看看你坚持使用过时技术的理由。观察:手边的线程展示了/>
这种古老的浪漫主义正在制造麻烦,因此

花费你的钱......)


-
呃********* @ sun.com



ANSI C has been around for a little less than seventeen
years, so perhaps your caution in adopting it is justified.
Just as a point of idle interest, how many decades do you
think must elapse before it is safe to adopt a new standard?
How are you doing with the plans to move from FORTRAN II to
FORTRAN IV?

Challenge: Without reference to a newspaper, almanac,
Wikipedia, or other such source, name three people who were
heads of state of members of the UN Security Council at the
time ANSI C was adopted.

Challenge: Is an `int'' large enough to count the number
of seconds since the adoption of ANSI C? The number of hours?
Of days?

Challenge: In twenty-five words or fewer, compare and contrast
your organization''s rate of standards adoption with the rate of
proton decay.

(All right, all right -- I''m having some fun at your expense.
But in all seriousness, I urge you to consider moving forward to
within a decade of the leading edge. There were once good reasons
to accommodate pre-ANSI implementations, but their goodness has
diminished with the passage of time and is now approximately equal
to that of a funerary meal from a Pharaoh''s tomb. You really,
really ought to take a hard look at your reasons for adhering to
outdated technologies. Observe: The thread at hand demonstrates
that this antiquarian romanticism is making trouble and hence
costing you money ...)

--
Er*********@sun.com


这篇关于K&amp; R预处理器中的令牌粘贴问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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