0

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

gideon
  • 19,329
  • 11
  • 72
  • 113
  • Hve you tried 'man bison' – stark Nov 26 '14 at 14:33
  • @stark yes, when I did it gives me an **empty** page in the bash terminal that says on the top **Manual page bison(1) line ?/? (END) (press h for help or q to quit**. **But** I remember seeing `-d` somewhere as for bison so I tried it. – gideon Nov 26 '14 at 14:36
  • You can also type 'man bison' into google although I am not responsible for your safe search settings. – stark Nov 26 '14 at 15:08
  • The segfault problem probably comes from the fact that recent versions of `flex` initialize `yyin` and `yyout` to `NULL` instead of `stdin` and `stdout`. You can get the traditional behaviour by giving `flex` the `--stdinit` command-line flag. There might be other incompatibilities between modern `flex` and the version of `lex` used in that book. – rici Nov 26 '14 at 15:59
  • @rici yep. Exactly what you said and thats in the marked answer :) – gideon Nov 27 '14 at 01:58

2 Answers2

1

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.

  • really? So Using `ch1-05.tab.c` instead of `y.tab.c` was _correct_ ? – gideon Nov 26 '14 at 14:38
  • [Screenie, worked, with segfault](https://www.dropbox.com/s/9t8nbr6gvzq5m5d/Screenshot%20from%202014-11-26%2020%3A18%3A15.png?dl=0). However, I double and triple checked the `ch1-04` code that this is based on and it's exactly like the book and is pretty correct to me atleast. – gideon Nov 26 '14 at 14:51
  • Yes so now you have a segfault and you need to debug it. There are tools that might help, like gdb. I feel like there should be a canonical "How To Trace A Segfault" question on stackoverflow but I don't see it anywhere. [This one](http://stackoverflow.com/questions/2876357/determine-the-line-of-c-code-that-causes-a-segmentation-fault) is a good start I guess. –  Nov 26 '14 at 15:02
1

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();
   }
}
xbug
  • 1,394
  • 1
  • 11
  • 18
  • I linked my binary according to the book : `gcc -o example-05.n lex.yy.o ch1-05.tab.o -ll` – gideon Nov 26 '14 at 14:42
  • Here is what is looks like with the segfault : https://www.dropbox.com/s/9t8nbr6gvzq5m5d/Screenshot%20from%202014-11-26%2020%3A18%3A15.png?dl=0 – gideon Nov 26 '14 at 14:50
  • Apparently it fails because `yyin` wasn't initialized. I just found a similar question [here](http://stackoverflow.com/questions/3317572/segmentation-fault-with-bison-and-flex). – xbug Nov 26 '14 at 15:00
  • @gideon: just add `yyin = stdin;` first thing in `main()` and it should be fine. – xbug Nov 26 '14 at 15:12
  • thank you VERY much. It worked :) The program somehow doesn't seem to recognize valid sentences still, saying "syntax error" but that I shall debug and figure out. – gideon Nov 26 '14 at 15:19