Unable to use error recovery with bison

f.tomassetti@gmail.com (Federico Tomassetti)
11 Apr 2005 00:15:41 -0400

          From comp.compilers

Related articles
Unable to use error recovery with bison f.tomassetti@gmail.com (2005-04-11)
Re: Unable to use error recovery with bison haberg@math.su.se (2005-04-16)
Re: Unable to use error recovery with bison f.tomassetti@gmail.com (2005-04-26)
| List of all articles for this month |

From: f.tomassetti@gmail.com (Federico Tomassetti)
Newsgroups: comp.compilers
Date: 11 Apr 2005 00:15:41 -0400
Organization: http://groups.google.com
Keywords: parse, yacc, errors
Posted-Date: 11 Apr 2005 00:15:40 EDT

I spent the last days trying to make error recovery working but I
can't catch one and yyerror is always invoked:

I report a portion from the lex source, yacc source and file parsed.

I always obtain the message "syntax error, unexpected NEWLINE,
expecting SPACE"

I use bison 2.0 and flex 2.5.31

(I'm a bit frustrated so tryed to pollute my code with "error" to
solve the problem but seems it not worked...)

YACC SOURCE
-----------

start: /* empty */
| components
;

components:
| components component
| components voidspace
| error voidspace { printf("ERROR\n"); }
| error ENDOFFILE { printf("ERROR\n"); }
;

voidspace: SPACE | TAB | NEWLINE
;

component: class_decl
| error voidspace { printf("ERROR\n");}
| error NEWLINE { printf("ERROR\n"); }
| error ENDOFFILE { printf("ERROR\n"); }
;

class_decl: class_decl_header block_decls NEWLINE KWENDC
{
if (State::verbose_) {
print_pos();
printf("Exiting decl of class %s\n", $1);
}
}
| class_decl_header error KWENDC {printf("E\n");};
;

class_decl_header:
KWCLASS SPACE CLASSNAME
{
$$=$3;

if (State::verbose_) {
print_pos();
std::cout << "Entering decl of class " << $3 << std::endl;
}

try_start_class_decl( std::string( $3 ), pos_line, pos_char );
}
| KWCLASS error { printf("ciao\n");fflush(stdout);} NEWLINE
| error { printf("ciao\n");fflush(stdout);}
| KWCLASS error voidspace { printf("1a\n");}
| error voidspace { printf("1a\n");}
| error NEWLINE { printf("1a\n");}
| KWCLASS error NEWLINE { printf("1a\n");}
;

LEX SOURCE
----------

  /* Save the whole line */

  ^.*/\n {
char str[MAXLINELENGTH+30];
snprintf(str, MAXLINELENGTH+30, "LINE READ (%s)", yytext);
verbose_info( str );
strncpy(current_line,yytext,MAXLINELENGTH);
REJECT;
  }

  /* Comments */

"//".*\n {
// ignore comments
}

  /* Keywords */

"class" {
increment_len( strlen(yytext), 0 );
verbose_info("CLASS");
return KWCLASS;
}

"endc" {
increment_len( strlen(yytext), 0 );
verbose_info("ENDC");
return KWENDC;
}

"block" {
increment_len( strlen(yytext), 0 );
verbose_info("BLOCK");
return KWBLOCK;
}

[A-Z]+[_[A-Z]+]* {
char str[100];
increment_len( strlen(yytext), 0 );
snprintf( str, 100, "classname(%s)\n", yytext);
verbose_info( str );
yylval.string=strdup(yytext);
return CLASSNAME;
}

[a-z]+[_[a-z]+]* {
char str[100];
increment_len( strlen(yytext), 0 );
snprintf( str, 100, "methodname(%s)\n", yytext);
verbose_info( str );
yylval.string=strdup(yytext);
return METHODNAME;
}

"\n" {
increment_len( 0, 1 );
verbose_info( "NEWLINE" );
return NEWLINE;
}

\t {
increment_len( TABSIZE, 0 );
verbose_info("TAB");
return TAB;
}

\b {
   increment_len( strlen(yytext), 0 );
verbose_info("SPACE");
   return SPACE;
}

  <<EOF>> {
increment_len(0,0); /* causa aggiornamento posizione */
verbose_info( "ENDOFFILE" );
   return ENDOFFILE;
}

. {
char str[100];
   increment_len( strlen(yytext), 0 );
snprintf( str, 100, "unexpected char (%c)\n", yytext[0]);
verbose_info(str);
lex_error(str);
}

/* Example file */
------------------
class

class

class
------------------

Thanks in advance for the help you can give me,

Federico Tomassetti


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.