Extraction of line contents fails in flex/bison -


i try extract contents of line , print them when input in line rejected bison. try reproduce these suggestions: http://archive.oreilly.com/pub/a/linux/excerpts/9780596155971/error-reporting-recovery.html when input rejected next line printed instead of line rejected, while number of line correctly printed.

flex:

%{ #include <stdio.h> #include "parser.tab.h" int line_number = 0; char linebuf[500]; %} ...  %%    \n.*  { ++line_number; strncpy(linebuf, yytext+1, sizeof(linebuf)); /* save next line */                 yyless(1);      /* give \n rescan */               } %% 

bison:

 %{     #include <stdio.h>     #include <assert.h>     #include <string.h>     #include <stdlib.h>     #include "parser.tab.h"      extern int yylex(void);     extern int line_number;     extern char line_contents[500];     void yyerror(char const *s);     %} ... %% int main(){ if( yyparse() == 0) printf("accepted\n"); else printf("syntax error in line %d: %s\n" line_number, linebuf); ... 

on input rejected bison approach above printd next line 1 contains grammatical error.

input: result = function //(semicolon expected) else 

output:

syntax error in line 1: else 

i believe lexical rule \n.* or yytext+1 drives output next line lexical rule correct one?

this happens because bison uses 1-token lookahead parse. missing semicolon not noticed (or diagnosed) until after scanner reads , returns else token. @ point, preceeding rule (which expecting semicolon or make longer expression) can't match (no shift or reduce action on token else in state).

once error noticed, parser calls yyerror prints message (and read line, 1 else token).


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -