Redian新闻
>
请教大牛们一个问题
avatar
请教大牛们一个问题# Java - 爪哇娇娃
X*w
1
想用java实现一个comments converter,就是把java程序 (文本文件)里的line
comments和block comments能实现互换,不能影响程序的可运行性。
具体把block换成line的时候如何在行头插入"/*"?
如果line comments正好是满一行,后边没有空插入"/*"怎么办?
avatar
g*g
2
Fixed width is required? Otherwise I don't get why you can't do that.
If fixed width, break the comment into 2 lines.

【在 X******w 的大作中提到】
: 想用java实现一个comments converter,就是把java程序 (文本文件)里的line
: comments和block comments能实现互换,不能影响程序的可运行性。
: 具体把block换成line的时候如何在行头插入"/*"?
: 如果line comments正好是满一行,后边没有空插入"/*"怎么办?

avatar
X*w
3
txt文本文件的行宽怎么定义呢?或者怎么知道是不是fixed呢?
谢谢谢谢

【在 g*****g 的大作中提到】
: Fixed width is required? Otherwise I don't get why you can't do that.
: If fixed width, break the comment into 2 lines.

avatar
g*g
4
It's not defined, some code convention will define a width,
it's not biggie if you don't follow it in comment.

【在 X******w 的大作中提到】
: txt文本文件的行宽怎么定义呢?或者怎么知道是不是fixed呢?
: 谢谢谢谢

avatar
X*w
5
谢谢,有啥好的建议把block换成line的?
我想逐行读近,然后判断开头是不是"/*",如果是,我就换成“//”,block有很多行的
话,只有最后一行的结尾是“*/”,中间的行用什么来判定呢?
谢谢

【在 g*****g 的大作中提到】
: It's not defined, some code convention will define a width,
: it's not biggie if you don't follow it in comment.

avatar
k*u
6
After you read the beginning /*, keep reading the following lines, for each
line, see if it has the ending */, if not, keep reading, if it does, then
this is the end of the block comment; then, all the lines in between are
part of the block comments and should be converted to //.
avatar
c*t
7
Trivial.
Learn to use lex / yacc. Then you can use CookCC easily write one
under 10 minutes.

【在 X******w 的大作中提到】
: 想用java实现一个comments converter,就是把java程序 (文本文件)里的line
: comments和block comments能实现互换,不能影响程序的可运行性。
: 具体把block换成line的时候如何在行头插入"/*"?
: 如果line comments正好是满一行,后边没有空插入"/*"怎么办?

avatar
k*u
8
he may have to spend 10 days on lex/yacc before that 10 min, :-)

【在 c*****t 的大作中提到】
: Trivial.
: Learn to use lex / yacc. Then you can use CookCC easily write one
: under 10 minutes.

avatar
X*w
9
thanks a lot.
是不是可以把整个block读成一个string?在 */之前看到\n 就在后面插入//?

each

【在 k****u 的大作中提到】
: After you read the beginning /*, keep reading the following lines, for each
: line, see if it has the ending */, if not, keep reading, if it does, then
: this is the end of the block comment; then, all the lines in between are
: part of the block comments and should be converted to //.

avatar
X*w
10
真是惭愧,刚开始学,还没有接触到lex/yacc. 多谢

【在 k****u 的大作中提到】
: he may have to spend 10 days on lex/yacc before that 10 min, :-)
avatar
g*g
11
It takes 10 min to code one without knowing lex/yacc.

【在 c*****t 的大作中提到】
: Trivial.
: Learn to use lex / yacc. Then you can use CookCC easily write one
: under 10 minutes.

avatar
h*0
12
为啥要读成byte string?

【在 X******w 的大作中提到】
: thanks a lot.
: 是不是可以把整个block读成一个string?在 */之前看到\n 就在后面插入//?
:
: each

avatar
X*w
13
修改了,就是string。
谢谢

【在 h*****0 的大作中提到】
: 为啥要读成byte string?
avatar
k*u
14
After a second thought, there are some special cases that are hard to deal
with, for example:
String trickyOne = "/* this is not a block comment */";
in this case, you have to make sure /* and */ are not enclosed in double
quotes. This alone is making your logic convoluted.
You best bet is to get to the parse tree of the source code, then just
traverse the tree and look for block comments. This solution is easy, clean
and maintainable.
For getting the parse tree, JavaSE 6 introduced some

【在 X******w 的大作中提到】
: thanks a lot.
: 是不是可以把整个block读成一个string?在 */之前看到\n 就在后面插入//?
:
: each

avatar
X*w
15
Thank you so much.
Based on a little what I have learned so far, I will first try to create a
program not designed to handle inline block comments (such as: class.method(
/* some comments */);), and the case you mentioned, which is enclosed in
double quotes.
After that I definitely will follow your guide.
Again thanks.

clean
link

【在 k****u 的大作中提到】
: After a second thought, there are some special cases that are hard to deal
: with, for example:
: String trickyOne = "/* this is not a block comment */";
: in this case, you have to make sure /* and */ are not enclosed in double
: quotes. This alone is making your logic convoluted.
: You best bet is to get to the parse tree of the source code, then just
: traverse the tree and look for block comments. This solution is easy, clean
: and maintainable.
: For getting the parse tree, JavaSE 6 introduced some

avatar
m*t
16
Heh, this reminds me of a funny (unless you are the one troubleshooting of
course)
trick back in the C days - what's wrong with this code:
println(1.0/*fptr);

clean
link
compiler-apis.html

【在 k****u 的大作中提到】
: After a second thought, there are some special cases that are hard to deal
: with, for example:
: String trickyOne = "/* this is not a block comment */";
: in this case, you have to make sure /* and */ are not enclosed in double
: quotes. This alone is making your logic convoluted.
: You best bet is to get to the parse tree of the source code, then just
: traverse the tree and look for block comments. This solution is easy, clean
: and maintainable.
: For getting the parse tree, JavaSE 6 introduced some

avatar
k*u
17

could possibly divide by 0

【在 m******t 的大作中提到】
: Heh, this reminds me of a funny (unless you are the one troubleshooting of
: course)
: trick back in the C days - what's wrong with this code:
: println(1.0/*fptr);
:
: clean
: link
: compiler-apis.html

avatar
m*t
18
No, hint: what is LZ trying to parse?

【在 k****u 的大作中提到】
:
: could possibly divide by 0

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。