Creating comments in Lex and Yacc

Stegosaurus picture Stegosaurus · May 17, 2016 · Viewed 11.2k times · Source

How does one make a comment in Lex and Yacc?

So far I haven't tried Yacc, but in Lex I have tried /* comment */ and // comment, but neither of these compile. I am on a Mac, using the builtin Lex and Yacc compilers, (or maybe the X-Code ones, I don't know). What is the correct syntax for comments in Lex or Yacc, or preferably both?

Answer

Stegosaurus picture Stegosaurus · May 17, 2016

Any C comment is acceptable as a comment anywhere in a program in both Yacc and Lex, BUT:

  • When using Lex or Yacc whitespace is important, so /* comment */ written into your program touching the LHS of your text will NOT WORK!
    • to make it work, you must add a tab or space to the beginning of the line, to shove it to C code, and not Lex or Yacc code.
  • gcc is a nice compiler and loves you very much, so it accepts // comment comments, Lex and Yacc are not nice. these comments, while will work in a C program, will NOT WORK!