void start_symbol_table(){ if(DEBUG_SYMBOL_TABLE){ printf("#ST - start_symbol_table\n"); } Table *outer; Table *func; Symbol *new_symbol; Symbol *aux; /* ----- creates Outer Symbol Table ----- */ outer=insert_table(_outer_table_); new_symbol = create_symbol("boolean", _type_, _constant_, "_boolean_", -1, -1); outer->child = new_symbol; aux = new_symbol; new_symbol = create_symbol("integer", _type_, _constant_, "_integer_", -1, -1); aux->next = new_symbol; aux = new_symbol; new_symbol = create_symbol("real", _type_, _constant_, "_real_", -1, -1); aux->next = new_symbol; aux = new_symbol; new_symbol = create_symbol("false", _boolean_, _constant_, "_false_", -1, -1); aux->next = new_symbol; aux = new_symbol; new_symbol = create_symbol("true", _boolean_, _constant_, "_true_", -1, -1); aux->next = new_symbol; aux = new_symbol; new_symbol = create_symbol("paramcount", _function_, _none_, NULL, -1, -1); aux->next = new_symbol; aux = new_symbol; new_symbol = create_symbol("program", _program_, _none_, NULL, -1, -1); aux->next = new_symbol; aux = new_symbol; /* ----- creates Function Symbol Table for 'paramcount' ----- */ func = insert_table(_function_table_); new_symbol = create_symbol("paramcount", _integer_, _return_, NULL, -1, -1); func->child = new_symbol; program_table = insert_table(_program_table_); }
void check_funcdefinition(node *root){ node *aux = root->son; node *scnd_aux = NULL, *param_aux = NULL; char *type = NULL; char *f_name = NULL; int num_params = 0; int n_pointers = 0; int i; check_funcdeclaration(root); type = (char*)calloc(strlen(aux->str_type)+1,sizeof(char)); strcpy(type,tolower_str(aux->str_type)); for(aux = aux->brother; strcmp(aux->str_type,"Pointer")==0; aux=aux->brother){ type = (char*)realloc(type,2); strcat(type,"*"); } f_name = (char*)calloc(strlen(aux->value)+1,sizeof(char)); strcpy(f_name,aux->value); insert_table(f_name,type); aux = aux->brother; /*if(equal_params(aux,f_name)==0){ remove_table(f_name); return; }*/ num_params = aux->n_sons; for(i=0,scnd_aux = aux->son; i<num_params; i++,scnd_aux=scnd_aux->brother){ n_pointers = 0; param_aux = scnd_aux->son; type = (char*)calloc(strlen(param_aux->str_type)+1,sizeof(char)); strcpy(type,tolower_str(param_aux->str_type)); param_aux = param_aux->brother; if(param_aux!=NULL){ for(; param_aux!=NULL && strcmp(param_aux->str_type,"Pointer") == 0; param_aux=param_aux->brother){ n_pointers++; } if(param_aux != NULL){ insert_element(f_name,type,n_pointers,param_aux->value,NULL,0,NULL,1,0); } } } aux = aux->brother; check_funcbody(aux,f_name); }
table_element *insert_funcdef2(Node *aux, environment_list *env, environment_list *outter_table){ table_element *temp; char *s=(char*)strdup(aux->value.s); lower(s); for(temp=env->locals;temp!=NULL;temp=temp->next){ if(strcmp(temp->name,s)==0 && temp->type==_function_){ if(temp->header->defined){ printf("Line %d, col %d: Symbol %s already defined\n",aux->line, aux->col, aux->value.s); exit(0); } temp->header=insert_table(aux->next,temp->header,env,outter_table); temp->header->defined=1; return temp; } else if(strcmp(temp->name,s)==0){ printf("Line %d, col %d: Function identifier expected\n",aux->line,aux->col); exit(0); } } return NULL; printf("Line %d, col %d: Symbol %s not defined\n",aux->line,aux->col,aux->value.s); exit(0); }
int search_node_func_def(Node *node){ if(DEBUG_SYMBOL_TABLE){ printf("#ST: search_node_func_def\n"); } int errorcount=0; Table *new_table; Symbol *symbol; Node *aux, *previous; /* creates the symbol element for the Program Table and adds it */ /* TO DO - verificar se variavel ja existe !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ symbol = create_symbol(node->first_child->value, _function_, _none_, NULL, -1, -1); insert_symbol_in_table(program_table, symbol); /* gets the function return type */ for(aux=node->first_child; aux->brother->node_type != NODE_VAR_PART; aux=aux->brother); Symbol_type function_return_type = get_symbol_type(aux->value); /* if(error_semantic == 0 && function_return_type == _bad_symbol_type_){ error_semantic = 1; printf("Line %d, col %d: Type identifier expected\n", previous->line, previous->column); errorcount++; } */ /* creates the new Function table */ new_table = insert_table(_function_table_); /* creates the symbol element function and adds it to the table */ /* TO DO - verificar se variavel ja existe !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ symbol = create_symbol(node->first_child->value, function_return_type, _return_, NULL, -1, -1); insert_symbol_in_table(new_table, symbol); /* travels to the NODE_FUNC_PARAMS node and runs through their childrens and grandchildrens */ errorcount += search_node_func_params(new_table, node->first_child->brother); for(aux=node->first_child; aux->node_type != NODE_VAR_PART; aux = aux->brother); errorcount += search_node_var_part_function(aux, new_table); return errorcount; }
void do_announce (CTL_MSG * mp, CTL_RESPONSE * rp) { struct hostent *hp; CTL_MSG *ptr; int result; result = find_user (mp->r_name, mp->r_tty); if (result != SUCCESS) { rp->answer = result; return; } hp = gethostbyaddr ((char *) &os2sin_addr (mp->ctl_addr), sizeof (struct in_addr), AF_INET); if (!hp) { rp->answer = MACHINE_UNKNOWN; return; } ptr = find_request (mp); if (!ptr) { insert_table (mp, rp); rp->answer = announce (mp, hp->h_name); return; } if (mp->id_num > ptr->id_num) { /* Explicit re-announce: update the id_num to avoid duplicates and re-announce the talk. */ ptr->id_num = new_id (); rp->id_num = htonl (ptr->id_num); rp->answer = announce (mp, hp->h_name); } else { /* a duplicated request, so ignore it */ rp->id_num = htonl (ptr->id_num); rp->answer = SUCCESS; } }
void do_announce(CTL_MSG *mp, CTL_RESPONSE *rp) { struct hostent *hp; CTL_MSG *ptr; int result; /* see if the user is logged */ result = find_user(mp->r_name, mp->r_tty); if (result != SUCCESS) { rp->answer = result; return; } #define satosin(sa) ((struct sockaddr_in *)(void *)(sa)) hp = gethostbyaddr(&satosin(&mp->ctl_addr)->sin_addr, sizeof (struct in_addr), AF_INET); if (hp == NULL) { rp->answer = MACHINE_UNKNOWN; return; } ptr = find_request(mp); if (ptr == NULL) { insert_table(mp, rp); rp->answer = announce(mp, hp->h_name); return; } if (mp->id_num > ptr->id_num) { /* * This is an explicit re-announce, so update the id_num * field to avoid duplicates and re-announce the talk. */ ptr->id_num = new_id(); rp->id_num = htonl(ptr->id_num); rp->answer = announce(mp, hp->h_name); } else { /* a duplicated request, so ignore it */ rp->id_num = htonl(ptr->id_num); rp->answer = SUCCESS; } }
environment_list* semantic_analysis(Node *myprogram){ environment_list* env = malloc(sizeof(environment_list)); env->previous=NULL; env->name="Outer"; env->locals=insert_outter_table(env,env->locals,"boolean",_type_,"constant",_boolean_); env->locals=insert_outter_table(env,env->locals,"integer", _type_, "constant", _integer_); env->locals=insert_outter_table(env,env->locals,"real", _type_, "constant", _real_); env->locals=insert_outter_table(env,env->locals,"false" ,_boolean_ ,"constant" ,_false_); env->locals=insert_outter_table(env,env->locals,"true", _boolean_, "constant", _true_); env->locals=insert_outter_table(env,env->locals,"paramcount", _function_, "", _NULL_); env->locals=insert_outter_table(env,env->locals,"program", _program_, "", _NULL_); table_element *temp; for(temp=env->locals;temp->type!=_program_;temp=temp->next); temp->header = insert_table(myprogram, temp->header, env, env); return env; }
table_element *insert_func(int is_defined,Node *aux, environment_list *env, environment_list *outter_table){ check_if_defined(aux,env); lower(aux->value.s);//value to lowercase table_element *nodeFuncDef = malloc(sizeof(table_element));//cria elemento que vai ser introduzido na tabela nodeFuncDef->name=aux->value.s; nodeFuncDef->type=_function_; nodeFuncDef->flag=""; nodeFuncDef->value=_NULL_; nodeFuncDef->header=malloc(sizeof(environment_list));//cria new table ligada ao elemento nodeFuncDef->header->defined=is_defined; nodeFuncDef->header->name="Function"; nodeFuncDef->header->previous=env; //Primeira linha da new table contem ID da function, tipo e flag //tipo da function está localizado em aux->next->next->value.s //insere primeira linha da new table tolower(aux->next->next->value.s); nodeFuncDef->header->locals=insert_line(nodeFuncDef->header->locals,aux->value.s,check_type(aux->next->next,env),"return",_NULL_); //insere o resto das linhas da new table nodeFuncDef->header=insert_table(aux->next,nodeFuncDef->header,env,outter_table); return nodeFuncDef; }
void process_request(CTL_MSG *mp, CTL_RESPONSE *rp) { CTL_MSG *ptr; char *s; rp->vers = TALK_VERSION; rp->type = mp->type; rp->id_num = htonl(0); if (mp->vers != TALK_VERSION) { syslog(LOG_WARNING, "bad protocol version %d", mp->vers); rp->answer = BADVERSION; return; } mp->id_num = ntohl(mp->id_num); mp->addr.sa_family = ntohs(mp->addr.sa_family); if (mp->addr.sa_family != AF_INET) { syslog(LOG_WARNING, "bad address, family %d", mp->addr.sa_family); rp->answer = BADADDR; return; } mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family); if (mp->ctl_addr.sa_family != AF_INET) { syslog(LOG_WARNING, "bad control address, family %d", mp->ctl_addr.sa_family); rp->answer = BADCTLADDR; return; } for (s = mp->l_name; *s; s++) if (!isprint(*s)) { syslog(LOG_NOTICE, "illegal user name. Aborting"); rp->answer = FAILED; return; } mp->pid = ntohl(mp->pid); if (debug) print_request("process_request", mp); switch (mp->type) { case ANNOUNCE: do_announce(mp, rp); break; case LEAVE_INVITE: ptr = find_request(mp); if (ptr != NULL) { rp->id_num = htonl(ptr->id_num); rp->answer = SUCCESS; } else insert_table(mp, rp); break; case LOOK_UP: ptr = find_match(mp); if (ptr != NULL) { rp->id_num = htonl(ptr->id_num); rp->addr = ptr->addr; rp->addr.sa_family = htons(ptr->addr.sa_family); rp->answer = SUCCESS; } else rp->answer = NOT_HERE; break; case DELETE: rp->answer = delete_invite(mp->id_num); break; default: rp->answer = UNKNOWN_REQUEST; break; } if (debug) print_response("process_request", rp); }
environment_list* insert_table(Node* node, environment_list* env, environment_list* previous, environment_list *outter_table){ Node *aux; Node *aux2; table_element *temp; table_element *nodeFunc; switch (node->type){ case VarDecl: ; aux=node; while(aux!=NULL){ aux2=aux->childs; Node *value=get_type(aux2); while(aux2->next!=NULL){ check_duplicates(aux2,env->locals); lower(aux2->value.s); env->locals=insert_line(env->locals,aux2->value.s,check_type(value,env),"",_NULL_); aux2=aux2->next; } aux=aux->next; } return env; case FuncDecl: case FuncDef2: case FuncDef: ; aux=node; while(aux!=NULL){ if(aux->type==FuncDef) nodeFunc=insert_func(1,aux->childs,env,outter_table); else if(aux->type==FuncDef2) nodeFunc=insert_funcdef2(aux->childs,env,outter_table); else if(aux->type==FuncDecl) nodeFunc=insert_func(0,aux->childs,env,outter_table); if(env->locals==NULL){ env->locals=nodeFunc; } else if(aux->type==FuncDef2){ if(nodeFunc!=NULL){ for(temp=env->locals;temp->next!=NULL;temp=temp->next){ if(strcmp(temp->name,nodeFunc->name)==0){ temp=nodeFunc; break; } } } } else{ for(temp=env->locals;temp->next!=NULL;temp=temp->next); temp->next=nodeFunc; } aux=aux->next; } return env; case Params: case VarParams: ; aux=node; while(aux!=NULL){ aux2=aux->childs; char *flag; if(aux->type==Params) flag="param"; else flag="varparam"; Node *value=get_type(aux2); while(aux2->next!=NULL){ check_duplicates(aux2,env->locals); lower(aux2->value.s); env->number_of_arguments++; env->locals=insert_line(env->locals,aux2->value.s,check_type(value,env),flag,_NULL_); aux2=aux2->next; } aux=aux->next; } return env; case IfElse: ; //check_statements(node->childs,"if",env); break; case While: ; //check_statements(node->childs,"while",env); break; case ValParam: ; //check_statements(node->childs,"val-paramstr",env); break; case Repeat: ; //check_statements(node->childs->next,"repeat-until",env); break; case Assign: ; //check_expressions(); break; case WriteLn: ; //check_writeln(node); break; case Call: ; check_number_of_arguments(node->childs,env); break; default: break; } if(node->childs){ env = insert_table(node->childs,env,previous,outter_table); } if(node->next) env = insert_table(node->next, env,previous,outter_table); return env; }
int process_request (CTL_MSG * msg, struct sockaddr_in *sa_in, CTL_RESPONSE * rp) { CTL_MSG *ptr; if (debug) { print_request ("process_request", msg); } if (acl_match (msg, sa_in)) { syslog (LOG_NOTICE, "dropping request: %s@%s", msg->l_name, inet_ntoa (sa_in->sin_addr)); return 1; } rp->vers = TALK_VERSION; rp->type = msg->type; rp->id_num = htonl (0); if (msg->vers != TALK_VERSION) { syslog (LOG_ERR, "Bad protocol version %d", msg->vers); rp->answer = BADVERSION; return 0; } msg->id_num = ntohl (msg->id_num); msg->addr.sa_family = ntohs (msg->addr.sa_family); if (msg->addr.sa_family != AF_INET) { syslog (LOG_ERR, "Bad address, family %d", msg->addr.sa_family); rp->answer = BADADDR; return 0; } msg->ctl_addr.sa_family = ntohs (msg->ctl_addr.sa_family); if (msg->ctl_addr.sa_family != AF_INET) { syslog (LOG_WARNING, "Bad control address, family %d", msg->ctl_addr.sa_family); rp->answer = BADCTLADDR; return 0; } /* FIXME: compare address and sa_in? */ msg->pid = ntohl (msg->pid); switch (msg->type) { case ANNOUNCE: do_announce (msg, rp); break; case LEAVE_INVITE: ptr = find_request (msg); if (ptr) { rp->id_num = htonl (ptr->id_num); rp->answer = SUCCESS; } else insert_table (msg, rp); break; case LOOK_UP: ptr = find_match (msg); if (ptr) { rp->id_num = htonl (ptr->id_num); rp->addr = ptr->addr; rp->addr.sa_family = htons (ptr->addr.sa_family); rp->answer = SUCCESS; } else rp->answer = NOT_HERE; break; case DELETE: rp->answer = delete_invite (msg->id_num); break; default: rp->answer = UNKNOWN_REQUEST; break; } if (debug) print_response ("process_request response", rp); return 0; }
void process_request(CTL_MSG *mp, CTL_RESPONSE *rp) { CTL_MSG *ptr; char *s; rp->vers = TALK_VERSION; rp->type = mp->type; rp->id_num = htonl(0); if (mp->vers != TALK_VERSION) { syslog(LOG_WARNING, "Bad protocol version %d", mp->vers); rp->answer = BADVERSION; return; } mp->id_num = ntohl(mp->id_num); if (ntohs(mp->addr.sa_family) != AF_INET) { syslog(LOG_WARNING, "Bad address, family %d", ntohs(mp->addr.sa_family)); rp->answer = BADADDR; return; } if (ntohs(mp->ctl_addr.sa_family) != AF_INET) { syslog(LOG_WARNING, "Bad control address, family %d", ntohs(mp->ctl_addr.sa_family)); rp->answer = BADCTLADDR; return; } for (s = mp->l_name; *s; s++) if (!isprint((unsigned char)*s)) { syslog(LOG_NOTICE, "Illegal user name. Aborting"); rp->answer = FAILED; return; } if (memcmp(&satosin(&rp->addr)->sin_addr, &satosin(&mp->ctl_addr)->sin_addr, sizeof(struct in_addr))) { char buf1[32], buf2[32]; strlcpy(buf1, inet_ntoa(satosin(&rp->addr)->sin_addr), sizeof(buf1)); strlcpy(buf2, inet_ntoa(satosin(&mp->ctl_addr)->sin_addr), sizeof(buf2)); syslog(LOG_WARNING, "addresses are different, %s != %s", buf1, buf2); } rp->addr.sa_family = 0; mp->pid = ntohl(mp->pid); if (debug) print_request("process_request", mp); switch (mp->type) { case ANNOUNCE: do_announce(mp, rp); break; case LEAVE_INVITE: ptr = find_request(mp); if (ptr != (CTL_MSG *)0) { rp->id_num = htonl(ptr->id_num); rp->answer = SUCCESS; } else insert_table(mp, rp); break; case LOOK_UP: ptr = find_match(mp); if (ptr != (CTL_MSG *)0) { rp->id_num = htonl(ptr->id_num); rp->addr = ptr->addr; rp->addr.sa_family = ptr->addr.sa_family; rp->answer = SUCCESS; } else rp->answer = NOT_HERE; break; case DELETE: rp->answer = delete_invite(mp->id_num); break; default: rp->answer = UNKNOWN_REQUEST; break; } if (debug) print_response("process_request", rp); }