about CSharp Parser

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

Hi:
I work in Csharp's parser files by LEX/YACC.Now I have only CSharp-lex.l
and CSharp.y file,but they not have CSharp'comment Parse. this is a part of
CSharp-lex.l.

.....................
/***** Comments *****/
"/*" { yy_push_state(IN_COMMENT); }
<IN_COMMENT>. { ; /* ignore */ }
<IN_COMMENT>\n { ; /* ignore */ }
<IN_COMMENT>"*/" { yy_pop_state(); }

{single_line_comment} { ; /* ignore */ }
...................

CSharp.y file not have any more comment regular.

Now I need comment parser in CSharp-lex.l. and CSharp.y files. Who give me
the files?

THS
EKEY
05.1.31
 
/***** Comments *****/
"/*" { yy_push_state(IN_COMMENT); }
<IN_COMMENT>. { ; /* ignore */ }
<IN_COMMENT>\n { ; /* ignore */ }
<IN_COMMENT>"*/" { yy_pop_state(); }

{single_line_comment} { ; /* ignore */ }


I'm afraid I didn't understand your question, but the above snippet is using
lex start conditions to parse C-style comments, as used in C#. You can read
more about them here:

http://www.gnu.org/software/flex/manual/html_chapter/flex_11.html#SEC11

On this page you will also find a much more efficient snippet of code for
parsing the same thing:

"/*" BEGIN(comment);

<comment>[^*\n]* /* eat anything that's not a '*' */
<comment>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */
<comment>\n ++line_num;
<comment>"*"+"/" BEGIN(INITIAL);

It's more efficient because it "eats" large strings of comment characters,
instead of eating one character at a time. You would simply add it somewhere
in your .y file. (line_num is assumed to be an integer variable tracking the
current line number). I hope this helps.
 
Back
Top