Posted By:
Monty_Zukowski
Posted On:
Wednesday, February 27, 2002 06:15 AM
You will have to figure out for yourself how to preserve comments. If this is the only thing you want to do, you won't even need a parser, just use a lexer and make the COMMENTs real tokens. Have another class that reads every token and writes it out, changing the format of the COMMENT if it needs to.
If this is in conjunction with a parser and/or tree parser then you need to figure out how to preserve the comments because they won't be in your tree since they are skipped.
Some people attach comments to the following token, but that doesn't work so well if you do things like discard semicolons in your tree, which is a common practice.
You could also remember where the comments were by absolute character positions and also keep such information in your tokens. When you print out tokens, always check to see if there was a comment between those two tokens, if so then translate it and print it.
There are lots of ways to do this. It really depends on the scope of your project.