关于C和C ++编译器之间的注释解析的问题 [英] Question about comment parsing between C and C++ compiler

查看:89
本文介绍了关于C和C ++编译器之间的注释解析的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在读书<<专家C编程>>,它有以下

测验,


a // *

// * / b


在C和C ++编译器中,上面的代码是什么?

我认为它对C编译器来说很简单,它是a / b。


但对于C ++编译器,本书说这是一个。原因是//

发表其余的评论。


我想知道这个。


只需回页,它就会提到编译器有一个最大值
munch策略。对于我来说,当C ++编译器读取第一行时,

存在模糊的解释,它可能是a // *。或者a / / *,

然后如果我们应用maximal much strategy,它应该使用第二个

一个并解析代码


a / / *

// * / b


这是a / b。


我觉得我在某个地方很困惑,你能不知所措?


谢谢。

解决方案

li*****@hotmail.com 写道:
< blockquote class =post_quotes>
>



我正在读书<< Expert C Programming>> ;,它有以下内容

测验,

a // *

// * / b


在C和C ++编译器中,上面的代码中断了什么?


我觉得C编译器很简单,它是a / b。


但是对于C ++编译器,这本书说它是一个。原因是//

发表其余的评论。


我想知道这个。


只需回页,它就会提到编译器有一个最大值
munch策略。对于我来说,当C ++编译器读取第一行时,

存在模糊的解释,它可能是a // *。或者a / / *,

然后如果我们应用maximal much strategy,它应该使用第二个

一个并解析代码


a / / *

// * / b


这是a / b。


我觉得我在某个地方很困惑,你能解开一些光吗?



最大咀嚼策略是什么?是什么意思?


-

pete


li ***** @ hotmail.com 说:




我正在阅读本书<< Expert C Programming>>,它有以下

测验,


a // *

// * / b


在C和C ++编译器中,上面的代码输出了什么?


我觉得它很简单对于C编译器,它是a / b。



这很简单,但现在它并不像你做的那么简单。

out。 PvdL没有意识到的是// - 将在1999年的语言修订版中将评论引入C中给出答案!


但是对于C ++编译器,书中说它是一个。原因是//

进行其余的评论。



是的。


我想知道这个。


只是回到页面,它提到编译器有一个最大的
munch策略。



抓住最大的令牌,是的。


对我来说,当C ++编译器读取第一行时,


存在含糊不清的解释,它可能是a // *或者a / / *,



不,它不可能是其中之一。它可能是


a / / *




a // *


和最大的munch决定了第二个。


C ++是否真的有一个最大的蒙克规则是一个问题,我们的

朋友们comp.lang.c ++毫无疑问能够回答。


-

理查德希思菲尔德

Usenet是一个奇怪的放置" - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


pete< pf ***** @ mindspring。 comwrites:

[...]


最大咀嚼策略是什么?意思?



这意味着,在确定下一个令牌时,编译器会尽可能多地抓取许多字符来获取有效令牌。 />

例如,这个:


x +++++ y


被标记为< br $> b $ bx ++ ++ + y


导致语法错误,即使这样:


x ++ + ++ y


将导致有效的解析。 (Tokenization并不代表

后期阶段。)


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

San迭戈超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


Hi,
I am reading book <<Expert C Programming>>, it has the following
quiz,

a //*
//*/ b

In C and C++ compiler what does the above code trun out?

I think it is simple for C compiler, it is a/b.

But for C++ compiler, the book says it is a. The reason is "//"
makes the rest of line comment.

I am wondering on this.

Just couple page back, it mentions that compiler has a "maximal
munch strategy". For me when the C++ compiler reads the 1st line,
there is ambiguous intepretation, it could be "a// *" or "a / /*",
then if we apply the "maximal much strategy", it should use the second
one and parse the code to

a / /*
// */ b

which is a/b.

I think I am confused at somewhere, could you shed some light?

Thanks.

解决方案

li*****@hotmail.com wrote:

>
Hi,
I am reading book <<Expert C Programming>>, it has the following
quiz,

a //*
//*/ b

In C and C++ compiler what does the above code trun out?

I think it is simple for C compiler, it is a/b.

But for C++ compiler, the book says it is a. The reason is "//"
makes the rest of line comment.

I am wondering on this.

Just couple page back, it mentions that compiler has a "maximal
munch strategy". For me when the C++ compiler reads the 1st line,
there is ambiguous intepretation, it could be "a// *" or "a / /*",
then if we apply the "maximal much strategy", it should use the second
one and parse the code to

a / /*
// */ b

which is a/b.

I think I am confused at somewhere, could you shed some light?

What does "maximal munch strategy" mean?

--
pete


li*****@hotmail.com said:

Hi,
I am reading book <<Expert C Programming>>, it has the following
quiz,

a //*
//*/ b

In C and C++ compiler what does the above code trun out?

I think it is simple for C compiler, it is a/b.

It''s fairly simple, but nowadays it is not quite as simple as you make
out. What PvdL didn''t realise was that //-comments would be introduced
into C in the 1999 language revision!

But for C++ compiler, the book says it is a. The reason is "//"
makes the rest of line comment.

Yeah.

I am wondering on this.

Just couple page back, it mentions that compiler has a "maximal
munch strategy".

Grab the biggest token you can, yes.

For me when the C++ compiler reads the 1st line,

there is ambiguous intepretation, it could be "a// *" or "a / /*",

No, it can''t be either of those. It could be

a / /*

or

a // *

and maximal munch dictates the second.

Whether C++ actually has a maximal munch rule is a question that our
friends in comp.lang.c++ would undoubtedly be able to answer.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


pete <pf*****@mindspring.comwrites:
[...]

What does "maximal munch strategy" mean?

It means that, when determining the next token, the compiler grabs as
many characters as possible to get a valid token.

For example, this:

x+++++y

is tokenized as

x ++ ++ + y

which results in a syntax error, even though this:

x ++ + ++ y

would result in a valid parse. (Tokenization doesn''t account for
later phases.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于关于C和C ++编译器之间的注释解析的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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