I know how to skip these comments using SKIP declarations, but all I need to do is to take a C source and output the same source without comments.
So I declared a token <GENERIC_TEXT: (~[])+ >, that gets copied to output, and comments aren’t skipped. I suspect this token takes all the input for itself.
Can someone help me, please?
Thank you
Don’t use
(~[])+: it will gobble up all your input. That is probably why you didn’t see tokens being skipped.In your default lexer mode, change to a different state when you encounter
"/*"(the beginning of a multi-line comment). And in this different stat, either match"*/"(and swicth back to the default lexer-state), or match any char~[](not(~[])+!).A quick demo:
CommentStripParser.jj
Given the test file:
Test.java
Run the demo like this (assuming you have the files CommentStripParser.jj, Test.java and the JAR javacc.jar in the same directory):
the following would be printed to your console:
(no comments anymore)
Note that you will still need to account for string literals that might look like this:
and char literals: