sinory
sinory

Reputation: 804

How to continue parser after syntax error

I use yy_scan_string and yyparse() to parse some text. I want to continue parse next string when appears syntax error,but it not work.

yacc file snippet:

set:SET PARENTHESIS reference EQUAL expression CLOSE_PARENTHESIS {$$ = set_directive($3,$5); }
|error { printf("set error abourt!");YYACCEPT;}//when appears error,I want to continue parsing the next string.I hava used YYABORT,but it not work as I want
;
...

int main(){

 yy_scan_string("#set($b ==9)"); //this string has syntax error.
    yyparse();
    yylex_destroy();
    printf("=====================11111========================\n");

    traverse(snode); //print the ast
    free_tree(snode); // release the memory


    yy_scan_string("#if($r==5) wewqewqe #end"); //this string is right,I want to continue to parse this after paser the string on it: "#set($b ==9)"
    yyparse();
    yylex_destroy();
    printf("=====================222222========================\n");

    traverse(snode);
    free_tree(snode);

    return 1;
}

int yywrap(){
    return 1;
}


int yyerror(char* s){
    printf("=====================error %s========================\n",s);
    //reset_input();
    //yyclearin;

    return 0;
}

How should I do, please help me!

Upvotes: 2

Views: 3285

Answers (1)

Aymanadou
Aymanadou

Reputation: 1220

In error recovering there some principals you should know:

  • we should add the error token as an alternative to the reduction (done)
  • we should tell our parser that the error is ok and we call for that yyerrok (not done)
  • you can also use the yyclearin to discard current token

    PS; the chronology of execution:

    in error case, yyerror is called the yyerrstate equals 1 after that yyerrok is called it reinitialize the error status at 0 and you can obviously call any macro after that...

      |error { yyerrok; yyclearin;printf("set error abourt!");}
      ;
    

Upvotes: 2

Related Questions