I’m using Bison and I’ve generated a quite complex grammar. The trouble is that my first test case is failing- but Bison will only say “syntax error”. Is there any way to ask Bison to output the rule that failed to match and the token that is a problem? I used
%define parse.trace true
but still only get syntax error as the output.
None of the Yacc-based parsers does anything much better than ‘syntax error’ when there is a problem; it is largely up to you to improve on that.
There are a couple of things you can do fairly readily.
One is to instrument your lexical analyzer so it prints out the tokens it finds as it returns them to the parser proper. This tells you which token the grammar is failing on, and what tokens were supplied beforehand.
The other is to compile with Yacc debug enabled, and then turn it on. This requires
-DYYDEBUG=1and setting the variableyydebugto a non-zero value (conventionally 1). The first step compiles the extra information into the grammar; the second step enables the output.From the Bison 2.4.3 manual: