int direct_abstract_declarator(void) { if( lookaheadT.type == LPAREN ) { match(LPAREN); if( abstract_declarator() ) { } else if( parameter_type_list() ) { } match(RPAREN); } else if ( lookaheadT.type == LBRACKET ) { match(LBRACKET); if( constant_expression() ) {} match(RBRACKET); } else if( direct_abstract_declarator() ) { match(LBRACKET); if( constant_expression() ) {} match(RBRACKET); } else if( direct_abstract_declarator() ) { match(LPAREN); if( parameter_type_list() ) {} match(RPAREN); } else { abort(); } }
/** 直接声明符后缀 <direct_declarator_postfix>::={<TK_OPENBR><TK_CINT><TK_CLOSEBR> |<TK_OPENBR><TK_CLOSEBR> |<TK_OPENPA><parameter_type_list><TK_CLOSEPA> |<TK_OPENPA><TK_CLOSEPA>} */ void direct_declarator_postfix(){ int n; if(TK_OPENPA == token) parameter_type_list(); else if(TK_OPENBR == token){ get_token(); if(TK_CINT == token){ get_token(); n = tkvalue; } skip(TK_CLOSEBR); direct_declarator_postfix(); get_token(); } }
int direct_declarator(void) { if( lookaheadT.type == ID ) { match(ID); } else if( lookaheadT.type == LPAREN ) { match(LPAREN); declarator(); match(RPAREN); } else if( direct_declarator() ) { if( lookaheadT.type == LBRACKET ) { match(LBRACKET); if( constant_expression() ) { } match(RBRACKET); } else if( lookaheadT.type == LPAREN ) { match(LPAREN); if( parameter_type_list() ) { } else if ( identifier_list() ) { } match(RPAREN); } } }
TOKEN function_definition(SYMBOL s) { TOKEN func_definition = NULL; //we didn't find a global var so assume we found a function //we already parsed the declaration specifiers and the declarator in the earlier parsing attempt //so just repurpose the information we collected instead of backtracking and re-parsing expect(DELIMITER_TOKEN, OPEN_PAREN, NO_ERROR_HANDLER); TOKEN params = parameter_type_list(); //we already found the return type and function name earlier, so we just pick up from there expect(DELIMITER_TOKEN, CLOSE_PAREN, NO_ERROR_HANDLER); SYMBOL return_type = s; insertfn(s->namestring, return_type, get_token_symbol_type(params)); //FIXME: extend this to more than just one parameter TOKEN func_body = compound_statement(); //FIXME: this is a hack to retrieve the function name in TOKEN form //figure out a better way to deal with this TOKEN func_name = make_token(); set_token_type(func_name, IDENTIFIER_TOKEN); set_token_string_value(func_name, s->namestring); set_token_symbol_table_entry(func_name, s); params = make_statement_list(params); func_definition = make_function_definition(func_name, params, func_body); return func_definition; }