Bison 语法错误后如何继续语法分析器
我使用Bison 语法错误后如何继续语法分析器,bison,yacc,lex,Bison,Yacc,Lex,我使用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 con
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;
}
我该怎么办,请帮帮我 在错误恢复过程中,您应该了解一些原则:
- 我们应该添加
标记作为减少的替代(完成)错误
- 我们应该告诉解析器错误是正常的,我们调用
(未完成)yyerrok
- 您还可以使用yyclearin放弃当前令牌
聚苯乙烯;执行年表:
在错误的情况下,yyerror被称为yyerrstate=1,在yyerrok被称为之后,它将在0处重新初始化错误状态,显然,在此之后您可以调用任何宏
|error { yyerrok; yyclearin;printf("set error abourt!");} ;
int-bracket_-flag=0;int blank_close_标志=0代码>遇到错误时需要将he设置为0,否则将不需要第二次解析,非常感谢。