ROSE 编译器框架/处理编译指示
外观
使用编译指示来指导翻译器通常很有用。
提供了一组解析器构建函数来帮助创建递归下降解析器。
包含头文件 AstFromString.h(位于 src/frontend/SageIII/astFromString)后,就可以访问命名空间中定义的变量和函数。
有一个示例项目对编译指示进行解析,并将结果保存到 AST 属性中。 https://github.com/rose-compiler/rose-develop/tree/master/projects/pragmaParsing
左递归
递归规则被转换为重复规则
/* YACC left recursion rule argument_expression_list : assignment_expression | argument_expression_list ',' assignment_expression Or in ANTLR as repetition argument_expression_list : assignment_expression (',' assignment_expression)* ; */ bool afs_match_argument_expression_list() { bool result =false; const char* old_char = c_char; SgExprListExp* parameters = NULL; if (afs_match_assignment_expression()) { SgExpression* arg1 = isSgExpression(c_parsed_node); assert (arg1 != NULL); parameters = buildExprListExp(arg1); c_parsed_node = parameters; result = true; } else { // immediate return false when first required term is not matched c_char = old_char; return false; } // match optional additional expressions old_char = c_char; // set rollback point while (afs_match_char(',')) { if (afs_match_assignment_expression()) { SgExpression* argx = isSgExpression(c_parsed_node); assert(argx != NULL); appendExpression(parameters, argx); c_parsed_node = parameters; // must set it again since it was rewritten in match_assignment_expression() } else { c_char = old_char; // optional match fails, rollback // printf("error. afs_match_argument_expression_list() expects assignment_expression after matching ','\n"); // assert (0); break; // and break out } // prepare next round old_char = c_char; } assert (parameters != NULL); // c_parsed_node = parameters; // this is necessary since the while loop may rewrite c_parsed_node assert (c_parsed_node == parameters); return true; }