I have a grammar which uses parentheses and square brackets as delimiters. When the parser generated by bison is given input with unbalanced delimiters, the error location in the YYLTYPE*
passed to yyerror
is the end of the input. So, for example, on input xx(yy
, within void yyerror(YYLTYPE* yylloc, Context* ctx, const char* msg)
I have that yylloc->first_column == yylloc->last_column == 5
. Unfortunately, the end of the input is not the most salient location for indicating an unmatched delimiter. Much more useful would be the position of the left parenthesis or left square bracket for which there was no match. (In the example, that would be the left parenthesis at offset 2.) I gather that that this information is available in the parse stack---there has to be some n such that $-n
is the unmatched (
or [
token and @-n
is the YYLTYPE
struct holding its position---but none of that appears to be available from yyerror
. I'm aware that I could keep a stack of my own for tracking the offsets of delimiters and stash that in the Context
I'm already passing to yyerror
, but that seems inelegant and duplicative, as bison has already to be tracking this.
So: How can one prise out of bison the position of the first unbalanced delimiter it encounters in input, so that this is available when producing the message for a parse error?