fork download
  1. /*
  2.   Jacob Sanchez
  3.   September 1, 2022
  4.   CS 370, LAB2_1
  5.  
  6.   Added int line, to count line numbers.
  7.   Changed fprintf of error message to match the line number to where the error occurred.
  8.   Added regex and code to ignore newlines inside of comments.
  9.   Changed debug to 1 so that error messages display.
  10.   */
  11.  
  12. /* simple lex program which removes comments from a source program
  13.   The main key is that a variable "comment" is set when the start of a comment
  14.   is seen and then unset when the ending set is seen. It is possible to have
  15.   two starts closed by on end.
  16.  
  17.   Shaun Cooper
  18.   January 2015
  19.  
  20.   */
  21. %{
  22. int line = 1; /* New variable for counting line number*/
  23. int comment = 0;
  24. int debug=1; /* prints out debug statements if desired */
  25. %}
  26. %%
  27. "\n" {line++;
  28. if (!comment) printf("\n");} /* New rule to ignore new lines when they're in comments*/
  29. "/*" {
  30. if (comment && debug) fprintf(stderr,
  31. " >>>>>>>> line %d: Possible Nested comment <<<<<<<<<<\n", line); /*Added ", line" inside fprintf to match line to %d*/
  32. comment = 1;
  33. }
  34.  
  35. "*/" {
  36. if (!comment) printf("%s",yytext); /*print out if it is not in a comment */
  37. comment = 0;
  38. }
  39. . { if (!comment) printf("%s",yytext);}
  40.  
  41. %%
  42.  
  43. int yywrap(void)
  44. { return 1;
  45. }
  46.  
  47. int main()
  48. {
  49. yylex();
  50.  
  51. }
  52.  
  53.  
Success #stdin #stdout #stderr 0.03s 6904KB
stdin
/* this is a sample comment /
    and this represents lines of code.
    / this, on the other hand
    /* is a nested comment
    /* terminnated here */
    / second termination
/
 *
 */

    see how it works
*/
stdout
Standard output is empty
stderr
ERROR: /home/JS6o5d/prog:22:12: Syntax error: Operator expected
ERROR: /home/JS6o5d/prog:52:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ? % Execution Aborted
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit