y.tab.c not generated on fedora with bison -d

841 views Asked by At

Update: So turns out all I had to do to make bison generate t.tab.c was to use the -y switch. However, my original assumption of a problem with the code when the problem gave me a segfault seems correct according to the answers posted. Here is what it all looks like now:

Segfaulting code

The source is exactly the same as downloaded from the O'reilly website and I triple checked the ch1-04 code that this is based on. It's all extremely simple stuff. A program that recognises a sentence as a subject VERB and object Here is the code : ch1-05.l, ch1-05.y, ch1-05y.h

Original Question:


So succinctly I'm working my way to learning how to build a compiler.

I'm not very used to C\C++ and have never used lex and yacc before, this I'm following this book: Lex & Yacc Cover


Now, in the book on page 21 this is what it says:

Lex & Yacc Page 21

Now being on fedora 2 I don't have yacc. I installed bison.

Here is what the scene on my fedora looks like:

Terminal (Shot5)

Do notice I am using -d.

If you'd really like to see the code here is : ch1-05.l, ch1-05.y, ch1-05y.h


I was actually silly enough and thought y.tab.c was an mistake in the book and they actually meant ch1-05.tab.c. I tried compiling with that and it gave me a segmentation fault :/ It took me a day to realize there is supposed to be a y.tab.c there.

I'm quite constantly checking SO so anything you need from me I will respond quick. I'd really like to get this done with because I'm on holiday due to a brain haemorrhage and I'd like to get this book complete soon. I have quite a list of books and quite a holiday :D

2

There are 2 answers

5
xbug On BEST ANSWER

Thinking that bison would produce ch1-05.tab.c instead of y.tab.c wasn't silly at all, in fact that's what it did. Bison is GNU's version of Yacc, and though it is compatible with it in terms of grammar description, there are some differences in usage.

If you want to revert to conservative Yacc mode, try bison -y [...]:

   -y
   --yacc
   --fixed-output-files
          Equivalent  to  -o  y.tab.c;  the  parser  output file is called
          y.tab.c, and the other outputs are called y.output and  y.tab.h.
          The purpose of this switch is to imitate yacc’s output file name
          conventions.  Thus, the following shell  script  can  substitute
          for yacc and is often installed as yacc:

          bison -y "$@"

If you get a segfault running you program, it's more likely due to a programming error on your side. How did you link you binary ?

( ... some googling later ... )

Try this in ch1-05.y:

extern FILE *yyin;

main()
{  
   yyin = stdin;  /* initialize yyin */
   while(!feof(yyin)) {
      yyparse();
   }
}
3
AudioBubble On

The default output file name is one of the differences between yacc and bison. If you use bison -y then it will act more like yacc and produce a y.tab.c (and y.tab.h if -d is also used).

The -y option will just change the name of ch1-05.tab.c to y.tab.c, it won't solve the bug that caused your segfault.