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

2
  • 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 6, 2010 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() ! Nov 6, 2010 at 19:31

2 Answers 2

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.

1
  • Thanks for pointing out,I've been oblivious to this great tool for so long! Nov 8, 2010 at 4:17
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.