You are not logged in.

1

Tuesday, March 8th 2005, 2:59pm

Please help! reading comments in nest!

Hi! Im trying to write app, whitch will read from file comments (even in nest). For example... i have a file with string like this:

Source code

1
2
3
4
5
6
7
8
9
something before the comment

/*
comment
/*comment in nest*/

*/

something after comment


i need to read everything in comments... I was trying it for a week... and i have no more idea how to code it... im taking all text from file to QTextStream and than i read it to string. setting up first comment open is easy... myQString.find("/*", 0, TRUE); but then... how to read a whole comment not only to firs "*/". Please Help me!
GOD BLESS THE MANUALS AND FORUMS!

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

2

Tuesday, March 8th 2005, 3:26pm

RE: Please help! reading comments in nest!

Use a stack. Besides, do you know that such comments are invalid in programming languages? You can't nest /* */

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

3

Tuesday, March 8th 2005, 3:36pm

RE: Please help! reading comments in nest!

Try this:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
int start = str.find("/*");
int pos = start+2;
int b, e;

do {
   b = str.find("/*", pos);
   e = str.find("*/", pos);
   pos = e + 2;
} while( b != -1 && b < e );

if( e != -1) end = pos;
else end = -1;

  • "antonio.fasolato" is male

Posts: 36

Location: Padova, Italy

Occupation: Student

  • Send private message

4

Tuesday, March 8th 2005, 9:49pm

Well, the most elegant solution should be using regular expression. I'm not an expert about this topic, but I think it can be done.

QRegexp
--
Antonio

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Tuesday, March 8th 2005, 10:07pm

QString string;

Source code

1
2
3
4
5
6
QRegExp rx("/\*(.*)\*/");
while(rx.search(string)>=0){
    QString nstring = rx.cap(1);
    // do something
    string = nstring;
}


Something like this MIGHT work :)

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

6

Tuesday, March 8th 2005, 10:46pm

Quoted

Originally posted by antonio.fasolato
Well, the most elegant solution should be using regular expression. I'm not an expert about this topic, but I think it can be done.

IMO it isn't possible. Regular expressions can be modelled as a finite state machine, which reads characters one by one and changes its states accordingly. If it ends in correct state --- it means that it matched the string. IMO such FSM for this problem would have states representing each level of nesting, thus having infinite number of states.

In other words, this problem is solvable with regular expression, if this (a bit informal) grammar:

Source code

1
2
comment := '/*' ( text | comment )* '*/'
text --- any string that does not contain /* or */
is regular.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

7

Tuesday, March 8th 2005, 10:50pm

This is just a recursive regular expression. The expression has to match the longest string it can find (thus the last closing */) -- yacc works this way. I don't know if QRegExp works like this too.

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

8

Tuesday, March 8th 2005, 10:54pm

Quoted

Originally posted by wysota
This is just a recursive regular expression. The expression has to match the longest string it can find (thus the last closing */) -- yacc works this way. I don't know if QRegExp works like this too.

But then it would allow only one top-level comment --- that's isn't very usefull.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

9

Tuesday, March 8th 2005, 11:02pm

Look at my code few posts earlier. There is a recursive search inside a loop. Each iteration finds one comment and substitues the string to be search with the content of the comment.

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

10

Tuesday, March 8th 2005, 11:07pm

Quoted

Originally posted by wysota
Look at my code few posts earlier. There is a recursive search inside a loop. Each iteration finds one comment and substitues the string to be search with the content of the comment.


I meant such situation:

Quoted


/* aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa */
/* aaa aaa aaa /* aaa aaa aaa aaa */ aaa aaa aaa */
/* aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa */

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

11

Tuesday, March 8th 2005, 11:16pm

Hmm... It will not work. If it finds the longest string then it will catch the beggining of the first comment and the closing of the last which will suck. If it finds the shortest string, it will find beginning of the first comment and the closing of the first most internal one, which will suck too. Without the knowledge of how QRegExp works, one can't find a suitable expression to do it. But I don't say it is impossible.

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

12

Tuesday, March 8th 2005, 11:22pm

Quoted

From Qt docs:
Note that quantifiers are "greedy". They will match as much text as they can. For example, 0+ will match as many zeros as it can from the first zero it finds, e.g. '2.0005'. Quantifiers can be made non-greedy, see setMinimal().

13

Wednesday, March 9th 2005, 9:23am

i havent check those methods yet, but it is possible, that there will be more comments after first... i want to read only first comment including nest(comments inside the comment)
GOD BLESS THE MANUALS AND FORUMS!

  • "antonio.fasolato" is male

Posts: 36

Location: Padova, Italy

Occupation: Student

  • Send private message

14

Wednesday, March 9th 2005, 9:37am

OK, regexp wasn't a good idea. I can see only a solution for your problem: recursion.

You have to use a stack and parse your text recursively (or iteratively, as recursion can be transformed in an iteration). It's the same problem as in reading parenthesized mathematical expression.

If you search in any decent book of algorithms, the problem should be faced, or at least hinted at.

Hi!

ps:
It's just a silly question, and it should be considered as a joke, but why hurting yourself with nested comments ?( (no programming language I know of permits this)
--
Antonio

15

Wednesday, March 9th 2005, 8:38pm

The comments are some kind of "help" in scripts... i need it to my app... becouse it use external scripts witch need parameters sometimes... in first comment is contained a little "how to explanation"... thats why i need it... and i've found a solution... i think so... it just reads text line by line... then count all "/*" and "*/" in a line... when it founds a "/*" it make int nest++ and when found a "*/" nest--... when nest goes to 0 it removes in this line all after "*/" and stops reading lines... I think it will work fine...
GOD BLESS THE MANUALS AND FORUMS!

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

16

Wednesday, March 9th 2005, 9:06pm

What will it do with:

Source code

1
/* /* /* */
?

  • "antonio.fasolato" is male

Posts: 36

Location: Padova, Italy

Occupation: Student

  • Send private message

17

Wednesday, March 9th 2005, 9:42pm

Quoted

it just reads text line by line... then count all "/*" and "*/" in a line...

This solution works as the one with the stack, but the stack would let you parse how many lines of code you want.

Hi
--
Antonio

18

Wednesday, March 9th 2005, 10:10pm

It has a loop checking whole file for how many "/*" and how many "*/" if there is more "/*" then "*/" it just puts whole text from first "/*" to end of file
GOD BLESS THE MANUALS AND FORUMS!

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

19

Wednesday, March 9th 2005, 10:13pm

Quoted

Originally posted by naresh
It has a loop checking whole file for how many "/*" and how many "*/" if there is more "/*" then "*/" it just puts whole text from first "/*" to end of file


That's not a very good thing, don't you think?

  • "antonio.fasolato" is male

Posts: 36

Location: Padova, Italy

Occupation: Student

  • Send private message

20

Thursday, March 10th 2005, 8:07am

Well, it should work. Perhaps not very elegant :D
--
Antonio