0

I wrote a simple assembler using flex and bison . It worked perfectly until I made a small change . Was a harmless additional rule in the lex and yacc specifications . After that it throws a segmentation fault each time I run it . I tried to trace the source of this seg fault but it turns out it occurs before the main in the lex file is executed . Then I removed the addition I made and recompiled it , It still shows the same error.

What's wrong???

I did something like this

  • Lookup Table , Symbol Table , Code Generator in separate files
  • Parser in a ".y" file importing the above mentioned files
  • Lexical Scanner in a ".l" file importing the "*.tab.h" file
  • linked lex.yy.c and parser.tab.c to make an executable

Thanks in Advance

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
abc def foo bar
  • 2,360
  • 6
  • 30
  • 41
  • do you know exactly where your code breaks? If you could give the exact line where the code segfaults, it'd probably help. – rtpg Nov 06 '10 at 19:28
  • well that's what i've been trying to find out , by placing printf()s at various places, I placed one before the call to yyparse() , It wasn't printed ! . It seems like its not even going to main() ! – abc def foo bar Nov 06 '10 at 19:31

2 Answers2

3
$ cc -g whatever...
$ gdb a.out
(gdb) run
<boom>
(gdb)bt

And if the answer isn't at that point obvious, select the entire above sequence and update your question above.

BTW, my guess is that you have changed the way you are building it. Perhaps you should just just cut your program back to hello, world, and then start adding in the other components one by one.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
1

Please provide more information such as GDB backtrace and the code of the corresponding flex and bison rules.

One debugging tip I have is to put printf() statements inside your flex rules. For example, suppose you wanted your new rule to match something like THISLOOP: LWU R2, 0(R3). Then in flex, you would put printf() inside any rules matching anything from that bison rule. But again, without any code it's impossible to debug.

Kizaru
  • 2,443
  • 3
  • 24
  • 39