0

I tried this, but it doesn't work. I think it only needs to be corrected slightly. I think I am not using the priority of the symbols correctly in one line. help?

%{
   #include<stdio.h>
   #include<stdlib.h>
   int nest = 0;
%}
%x COMMENT
%%
"/*"            {BEGIN(COMMENT); ++nest;}
"//".*          

<COMMENT>[^*)]* {}
<COMMENT>"(*"   {++nest;}
<COMMENT>"*)"   {if (--nest== 0) BEGIN(INITIAL);}
<COMMENT>[*)]   {}


([^/]*([/][^(*])*)* {fprintf(yyout, yytext);}  
%%
int main(int argc, char* argv[])
{
    yyin = fopen("input.txt","r");
    yyout = fopen("output.txt", "w");
    yylex();
    fclose(yyin);
    fclose(yyout);
    return 0;
}

And I also need to eliminate { ... } type of comments, if someone could help with this, much appreciation!

 For example:
 1.a.input:   show (* commenting  *)show
     output:  show show
   b.input:   show { commenting  }show
     output:  show show

 2.input:   show \\  afqqe
   output:  show 

 3.a.input:   show (* commenting .. comment..
              comment....*) show
     output:  show show
   b.input:   show { commenting .. comment..
              comment....} show
     output:  show show

 4.a.input:   show (* commenting {.. }comment..
              comment....*) comment show
     output:  show comment show
   b.input:   show { commenting (* .. comment..
              comment..*)..} show
     output:  show show
Jerry West
  • 151
  • 5
  • It would help if you explained what the comments you are trying to match look like. Also, "it doesn't work" is not an adequate explanation of what the problem is. – rici Apr 08 '16 at 13:34
  • Cool. Now, show how you modified the code from http://stackoverflow.com/a/12949439/1566221 to handle nested `(*...*)` comments and explain why that didn't meet your needs. – rici Apr 08 '16 at 16:53
  • I modified that code like this, and no comment was ignored whatsoever. I modified it in another why, same result. I think the main problem is the line ([^/]*([/][^(*])*)* {fprintf(yyout, yytext);} – Jerry West Apr 08 '16 at 17:24
  • The main problem is `"/*" {BEGIN(COMMENT); ++nest;}`; if you don't fix that, then you will never recognize the beginning of the comment. You're correct that the other pattern needs to change as well; I'd suggest using the simpler `.|\n { ECHO; }`, as pointed out in a comment in the original code, IIRC. – rici Apr 08 '16 at 19:17

0 Answers0