Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating comments in Lex and Yacc

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?

like image 775
jellies Avatar asked May 17 '16 02:05

jellies


People also ask

How do you make a comment in Lex?

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.

What is $$ in yacc?

those $$ , $1 , $3 are the semantic values for for the symbols and tokens used in the rule in the order that they appear. The semantic value is that one that you get in yylval when the scanner gets a new token.

How does Lex and yacc work together?

lex and yacc are a pair of programs that help write other programs. Input to lex and yacc describes how you want your final program to work. The output is source code in the C programming language; you can compile this source code to get a program that works the way that you originally described.

How do you comment on bison?

Indented text is copied verbatim to the generated file, but a comment is a comment. Yacc/Bison: Use /* ... */ anywhere in the grammar. In bison, but possibly not other yacc derivatives, you can also use // comments.


1 Answers

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!
like image 178
jellies Avatar answered Sep 19 '22 14:09

jellies