我使用yy_scan_string
和yyparse()
来解析一些文字。我想在出现语法错误时继续解析下一个字符串,但它不起作用。
yacc文件片段:
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;
}
我该怎么办,请帮助我!
答案 0 :(得分:1)
错误地恢复了一些您应该知道的原则:
error
令牌作为redu(done)yyerrok
(未完成)您也可以使用yyclearin来丢弃当前令牌
PS;执行年表:
在错误的情况下,yyerror被称为yyerrstate等于1之后,yyerrok被调用它将错误状态重新初始化为0,显然你可以在之后调用任何宏...
|error { yyerrok; yyclearin;printf("set error abourt!");}
;