Error handling 在yacc中,如果给定的输入与任何规则都不匹配,我们如何在输出中以相同的格式返回输入
请在下面找到我的代码:Error handling 在yacc中,如果给定的输入与任何规则都不匹配,我们如何在输出中以相同的格式返回输入,error-handling,default,yacc,Error Handling,Default,Yacc,请在下面找到我的代码: function_definition : declaration_specifiers declarator declaration_list {printf("in function definition \n");}compound_statement | declaration_specifiers declarator {printf("in function definition \n");}compound_statement |
function_definition
: declaration_specifiers declarator declaration_list {printf("in function definition \n");}compound_statement
| declaration_specifiers declarator {printf("in function definition \n");}compound_statement
| declarator declaration_list {printf("in function definition \n");} compound_statement
| declarator {printf("in function definition \n");} compound_statement {my_fun($<block_item_struct>2); printf("in function definition\n");}
| direct_declarator ';' {
$<fun_ptyp>$ = $<multi_type>1;
}
| error {char * text ; $<symbol_table>$->cb = malloc(sizeof(struct code_block));text = strdup(yytext);$<symbol_table>$->cb->function_call= text;$<symbol_table>$->cb->pos =yylineno;
*yytext=NULL;printf("error: %s at line no %d\n",$<symbol_table>$->cb->function_call,$<symbol_table>$->cb->pos);**
}
;
简而言之,你不能。输入已标记化。NB
yacc
不使用正则表达式。有没有办法绕过lex的标记化,在yacc中有一个默认规则来匹配所有与其他规则不匹配的规则。
Ex: /* This function takes care of tree implementation .
The parameters passed are char* and int */