Perform no Semantic Actions on Error Bison

242 Views Asked by At

I have to build a compiler in bison. I want my compiler to work as most compilers, if there are any syntax errors, list them all and then bail out. The problem is I have semantic actions for each rule, and my compiler reports all the syntax errors but still performs all the semantic actions. Is there a way to tell bison to ignore semantic actions if there is a syntax error, but to keep looking for all the syntax errors?

Thanks!

1

There are 1 best solutions below

0
On

Not clear what you are asking. Do you want the bison parser to not execute any actions at all if there are any syntax errors anywhere in your program? This is difficult, as bison executes the actions immediately as it reduces the rules, so there's no good way for it to know if there might be an error in the future somewhere. For actions after a syntax error (assuming you have error recovery rules -- it will exit after the first syntax error if you don't), you can have a global flag and simply make all actions test that flag before they do anything.

If you want to disable actions before the first error, the only thing I can think of is to have two parsers with the same grammar -- one that does no actions and one that does them. Use the first parser to determine if there are any errors, and then rewind the input and use the second parser only if there are no errors.