hi, As I understand, you want to refer to the user's exact input symbol in the error message rather than the grammar symbol associated with it.. ?
Well, flex provides a global variable 'yytext' that contains the value of the symbol. Suppose you have a definition like this in your lexer: [0-9]+ { return TOK_NUM; } then, what bison knows about is ONLY the symbol TOK_NUM and nothing else (i.e if the number is 5 or 100 or whatever); so you have to catch yytext and store it in yylval like this: [0-9]+ { yylval = atoi(yytext); return TOK_NUM; } //assuming your YYSTYPE is int -- otherwise use the appropriate field of the union. and then you can use yylval to print the value in the error message... (infact now it becomes the $x of the token in your grammar): Hope this helps. ./satya. On 6/22/06, Guillaume Rousse <[EMAIL PROTECTED]> wrote:
Hello. I'm trying for two days now to produce better output messages in case of parsing failure. I'm using flex + bison for a standard (non-reentrant) parser. I've had to redefine YY_INPUT in scanner.l, as I'm parsing small, line-based messages, and default version was blocking: #define YY_INPUT(buf,result,max_size) \ if (yy_current_buffer->yy_is_interactive ) { \ int c = '*', n; \ for (n = 0; n < max_size && \ (c = getc( yyin )) != EOF && c != '\n'; ++n) \ buf[n] = (char) c; \ if (c == '\n') \ buf[n++] = (char) c; \ if (c == EOF && ferror( yyin )) \ YY_FATAL_ERROR("input in flex scanner failed"); \ result = n; \ } else { \ if ((fgets(buf, max_size, yyin) == NULL) && ferror(yyin)) \ YY_FATAL_ERROR("input in flex scanner failed"); \ result = strlen(buf); \ } >From flex and bison manuals, I've understood the following informations: - yyerror function receive a simple string error message, such as "syntax error" - using %error-verbose bison directive make this error message a bit more verbose, such as "syntax error, unexpected VALUE_WORD, expecting ARR1D_STOP" - defining %yylineno flex directive make input line also available as global variable yylineno from yy_error - defining %locations bison directive make global variable yylloc available, with four fields defining first and last lines and columns where error occurs However, all I could achieve by combining those values was something as: syntax error, unexpected VALUE_WORD, expecting ARR1D_STOP Location: line 7, columns 0-0 Which is really not meaningful for user :/ I'd prefer to at least display current line, and avoid refering to internal grammar symbols. I've searched mailing-list without success. I also read generated scanner.c file, in order to access yy_current_buffer object with the proper character range, but without success. -- Guillaume Rousse Projet Estime, INRIA Domaine de Voluceau Rocquencourt - B.P. 105 78153 Le Chesnay Cedex - France _______________________________________________ help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison
-- "When you have eliminated the impossible, whatever remains, however improbable, must be the truth". -Sherlock Holmes, The sign of four. _______________________________________________ help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison