1

I'm writing a compiler of clike language in JS using Jison as an lexer/parser generator with angular frontend. I nearly got the result I expected, but there is one thing that is puzzling me - how to make Jison ignore comments (both /* block */ and // line)?

Is there any easy way to achieve it? Keeping in mind that the comment can potentially be inserted in the middle of any statement/expression?

Vees
  • 703
  • 4
  • 9

1 Answers1

3

You ignore comments the same way you ignore whitespace: with a lexer rule that has no action.

For example:

\s+                                   /* IGNORE */
"//".*                                /* IGNORE */
[/][*][^*]*[*]+([^/*][^*]*[*]+)*[/]   /* IGNORE */

The first line ignores whitespace. The second ignores single-line comments. And the third ignores block comments.

rici
  • 234,347
  • 28
  • 237
  • 341
  • That's the idea! Although the block comment didn't work for me (im using .jison file format) it gave me a clue and I've found following regexp that does the job: `"/*"((\*+[^/*])|([^*]))*\**"*/"` – Vees Dec 12 '15 at 18:17
  • @Vees: Yep, there was a typo (the star after the close parenthesis was missing). Fixed. Thanks. – rici Dec 13 '15 at 01:04