To remove all comments in a JavaScript file, do we need just a scanner or also a parser? I was asked how to remove comments in a JavaScript program, but once I gave the regular expression solution, I was asked what if there are comments like text inside of a string:

let hi = " // here ";
let foo = "   use this: /*   ";
let foo2 = "   \" and that */   ";


it also can get complicated with the cases with string being able to be quoted by ' and the backtick `:

let hi2 = 'here //';
let i = 123;
let hi3 = `here //  ${i  /* don't use j */}`;
let hi4 = `here //  ${i  // don't use j 
                         // because j is not good
                     }`;
let hi5 = ` and use there ' " \' \" \`  /*  */`;
let hi6 = ` this is really closing, not quoted backtick \\`;
let hi7 = `here //  ${i  /* don't use j in `` */}`;


So I mentioned a scanner (lexical analyzer) and a parser (like yacc or bison) can be used. So we can build a tree of node representing the program, and then wherever it is a comment type of node, we can remove it. (comment nodes have no children, I think? If they do, then can just change the comment to nothing, the empty string).

But is it true that all we need is a scanner?  We can tokenize all the text, and then we get each element which is a string, an operator, the left and right operands, and also the comment elements.

And then we can just remove all the comment elements and then reconstruct the program using all the tokens.

So probably we don't even need the program code to be represented as a tree, but just as a series of tokens.  So if we already have a scanner and parser, all we need is a scanner, and when we supply it with the proper grammar rules, we can remove all the comments?

(is the following considered grammar or just finite state automata and do we just use it to form the tokens?)
Scanner (tokenizer) will be enough. Most programming languages do not allow comment nesting, so there is no need to use recursive parsers to strip all comments.