Programming Language Grammars – How Are Comments Expressed?

commentsgrammarparsing

I'm learning how to build parsers using grammars, but I got stuck trying to express comments, because they can appear almost anywhere.

This indicates that comments can be stripped from the token stream before parsing takes place.

Is that the standard practice, or are comments ever/often specified in grammars?

Best Answer

It is very common to treat it as some form of white space. Much in the same way as newlines in semicolon-oriented languages like C.

Once it is some form of white-space, you frequently just ignore it higher in the parser.

Related Topic