/* Find the location of all functions in the program and store global variables. */ void prescan(void) { char *p, *tp; char temp[32]; int datatype; int brace = 0; /* When 0, this var tells us that current source position is outside of any function. */ p = prog; func_index = 0; do { while(brace) { /* bypass code inside functions */ get_token(); if(*token == '{') brace++; if(*token == '}') brace--; } tp = prog; /* save current position */ get_token(); /* global var type or function return type */ if(tok==CHAR || tok==INT) { datatype = tok; /* save data type */ get_token(); if(token_type == IDENTIFIER) { strcpy(temp, token); get_token(); if(*token != '(') { /* must be global var */ prog = tp; /* return to start of declaration */ decl_global(); } else if(*token == '(') { /* must be a function */ func_table[func_index].loc = prog; func_table[func_index].ret_type = datatype; strcpy(func_table[func_index].func_name, temp); func_index++; while(*prog != ')') prog++; prog++; /* now prog points to opening curly brace of function */ } else putback(); } } else if(*token == '{') brace++; } while(tok != FINISHED); prog = p; }
void prescan() { char *p; char temp[32]; int brace = 0; p = prog; func_index = 0; do { while(brace) { get_token(); if(*token=='{') brace++; if(*token=='}') brace--; } get_token(); if(tok==CHAR || tok==INT) { putback(); decl_global(); } else if(token_type==IDENTIFIER) { strcpy1(temp, token); get_token(); if(*token=='(') { func_table[func_index].loc = prog; strcpy1(func_table[func_index].func_name, temp); func_index++; while(*prog!=')') prog++; prog++; } else putback(); } else if(*token=='{') brace++; } while(tok!=FINISHED); prog = p; }