Advertisement
KDuarte

Untitled

May 5th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.08 KB | None | 0 0
  1. /*Os_Accepted*/
  2. #include "tabela_simbolos.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "estruturas.h"
  7. #include "errors.h"
  8. #include "mod_tree.h"
  9. #include <ctype.h>
  10.  
  11. symbol_table *create_symbol_table(char *title, int definition){
  12. symbol_table *tmp;
  13. tmp = (symbol_table*)malloc(sizeof(symbol_table));
  14. tmp->title = (char*)malloc(strlen(title)+1);
  15. strcpy(tmp->title, title);
  16. tmp->symbol = NULL;
  17. tmp->next = NULL;
  18. tmp->definition = definition;
  19. return tmp;
  20. }
  21.  
  22. /*adicionar/criar um simbolo*/
  23. void add_symbol(symbol_table *table, char *name, char *type, int param, param_type *pt){
  24. symbol_element *tmp_element;
  25. symbol_element *tmp_list_elements;
  26.  
  27. tmp_element = (symbol_element*)malloc(sizeof(symbol_element));
  28. if(name != NULL)
  29. {tmp_element->name = (char*)malloc(strlen(name)+1);
  30. strcpy(tmp_element->name, name);}
  31. else{tmp_element->name = (char*)malloc(5);
  32. strcpy(tmp_element->name, "Null");
  33. }
  34. if(type != NULL){
  35. tmp_element->type = (char*)malloc(strlen(type)+1);
  36. strcpy(tmp_element->type, type);
  37. tmp_element->type[0] = tolower(tmp_element->type[0]);
  38. }else{
  39. tmp_element->type = (char*)malloc(5);
  40. strcpy(tmp_element->type, "Null");
  41. }
  42.  
  43. tmp_element->param = param;
  44. tmp_element->paramtype = pt;
  45. tmp_element->next = NULL;
  46. tmp_list_elements = table->symbol;
  47. if(tmp_list_elements == NULL){table->symbol = tmp_element; return;}
  48.  
  49. while(tmp_list_elements->next!=NULL){
  50. tmp_list_elements = tmp_list_elements->next;
  51. }
  52. tmp_list_elements->next = tmp_element;
  53. return;
  54. }
  55.  
  56. /*fazer lista de parametros de entrada, retorna o primeiro da lista*/
  57. param_type *add_entry_param(param_type *pt, char *param){
  58. param_type *tmp;
  59. if(pt == NULL){
  60. tmp = (param_type*)malloc(sizeof(param_type));
  61. tmp->type_name=(char*)malloc(strlen(param)+1);
  62. strcpy(tmp->type_name, param);
  63. tmp->next = NULL;
  64. return tmp;
  65. }
  66. else{
  67. tmp = pt;
  68. while(tmp->next != NULL){
  69. tmp = tmp->next;
  70. }
  71. tmp->next = (param_type*)malloc(sizeof(param_type));
  72. tmp->next->type_name=(char*)malloc(strlen(param)+1);
  73. strcpy(tmp->next->type_name, param);
  74. tmp->next->next = NULL;
  75. return pt;
  76. }
  77. }
  78.  
  79. symbol_table *start_table(Node *start, symbol_table *gtable){
  80. /*function Definition, function declaration, declaration*/
  81. Node *tmp;
  82. param_type *pttmp;
  83. /*criar tabela com campos default*/
  84. gtable = create_symbol_table("Global Symbol Table", 1);
  85.  
  86. /*add atoi int(char*)*/
  87. pttmp = add_entry_param(NULL, "char*");
  88. add_symbol(gtable, "atoi", "int", 0, pttmp);
  89. gtable->next = create_symbol_table("atoi", 0);
  90.  
  91. /*add itoa char*(int,char*)*/
  92. gtable->next->next = create_symbol_table("itoa", 0);
  93. pttmp = NULL;
  94. pttmp=add_entry_param(NULL, "int");
  95. pttmp=add_entry_param(pttmp, "char*");
  96. add_symbol(gtable, "itoa", "char*", 0, pttmp);
  97.  
  98. /*add puts int(char*)*/
  99. gtable->next->next->next = create_symbol_table("puts", 0);
  100. pttmp = NULL;
  101. pttmp=add_entry_param(NULL, "char*");
  102. add_symbol(gtable, "puts", "int", 0, pttmp);
  103.  
  104. tmp = start->child;
  105. if(tmp==NULL){return NULL;}
  106. while(tmp!= NULL){
  107. if(strcmp(tmp->node_type,"FuncDefinition")==0){func_definition_sem(tmp, gtable);}
  108. else if(strcmp(tmp->node_type,"FuncDeclaration")==0){func_declaration_sem(tmp, gtable);}
  109. else if(strcmp(tmp->node_type, "ArrayDeclaration") == 0 || strcmp(tmp->node_type, "Declaration") == 0){declaration_arr_var(gtable, NULL, tmp);}
  110. tmp = tmp->brother;
  111. }
  112. return gtable;
  113. }
  114.  
  115. Node *add_str_pointer(char **strpointer, Node *node){ /*pointers*/
  116. int pcount = 0,i;
  117. Node *type = node; /* tipo */
  118. node = node->brother;
  119. while(node != NULL && strcmp(node->node_type, "Pointer")==0){
  120. pcount++;
  121. node = node->brother;} /*conta * */
  122.  
  123. *strpointer = (char*)malloc(strlen(type->node_type)+pcount+1);
  124. strcpy(*strpointer, type->node_type);
  125. *strpointer[0]=tolower(*strpointer[0]);
  126. for(i=0; i<pcount; i++)
  127. strcat(*strpointer, "*");
  128. /*if(flag_check_void != 1)
  129. if(check_void(*strpointer, node)){
  130. *strpointer = (char*)malloc(6);
  131. strcpy(*strpointer, "undef");
  132. return node;
  133. }*/
  134. return node; /*retorna o node do id ou null*/
  135. }
  136.  
  137.  
  138. void declaration_arr_var(symbol_table *gtable, symbol_table *ltable, Node *no_actual){
  139. char *type, *ident, *type2=NULL;
  140. int var_len;
  141. Node *tmp_child, *tmp_id;
  142. tmp_child = no_actual->child; /*tipo*/
  143.  
  144. tmp_child = add_str_pointer(&type, tmp_child); /*retorna no depois do ultimo pointer - ID*/
  145. if(check_void(type, tmp_child)){
  146. if(ltable == NULL){
  147. add_symbol(gtable, tmp_child->value, "undef", 0, NULL);
  148. return;
  149. }
  150. else{
  151. add_symbol(ltable, tmp_child->value, "undef", 0, NULL);
  152. return;
  153. }
  154. }
  155. ident = tmp_child->value;
  156.  
  157. tmp_id = tmp_child;
  158. tmp_child = tmp_child->brother; /*NULL ou POINTER*/
  159.  
  160. if(tmp_child != NULL && ltable == NULL){ /*ARRAY*/ /*GLOBAL TABLE*/
  161. type2 = (char*)malloc(strlen(type)+strlen(tmp_child->value)+3);
  162. if(tmp_child->value[0] == '0'){
  163. sscanf(tmp_child->value, "%o", &var_len); /*conversao para decimal*/
  164. sprintf(type2, "%s[%d]", type, var_len);
  165. if(is_defined(tmp_id, gtable, ltable, type2) != 0){
  166. return;
  167. }
  168. }else{
  169. sprintf(type2, "%s[%s]", type, tmp_child->value);
  170. if(is_defined(tmp_id, gtable, ltable, type2) != 0){
  171. return;
  172. }
  173. }
  174. add_symbol(gtable, ident, type2, 0, NULL);
  175. }
  176. else if(tmp_child != NULL && ltable != NULL){ /*ARRAY*/ /*LOCAL TABLE*/
  177. type2 = (char*)malloc(strlen(type)+strlen(tmp_child->value)+3);
  178. if(tmp_child->value[0] == '0'){
  179. sscanf(tmp_child->value, "%o", &var_len);
  180. sprintf(type2, "%s[%d]", type, var_len);
  181. if(is_defined(tmp_id, gtable, ltable, type2) != 0){
  182. return;
  183. }
  184. }else{
  185. sprintf(type2, "%s[%s]", type, tmp_child->value);
  186. if(is_defined(tmp_id, gtable, ltable, type2) != 0){
  187. return;
  188. }
  189. }
  190. add_symbol(ltable, ident, type2, 0, NULL);
  191. }
  192. else if (ltable == NULL){ /*VAR LOCAL TABLE*/
  193. if(is_defined(tmp_id, gtable, ltable, type) != 0){
  194. return;
  195. }
  196. add_symbol(gtable, ident, type, 0, NULL);
  197. }
  198. else if (ltable != NULL){ /*ARR GLOBAL TABLE*/
  199. if(is_defined(tmp_id, gtable, ltable, type) != 0){
  200. return;
  201. }
  202. add_symbol(ltable, ident, type, 0, NULL);
  203. }
  204. }
  205.  
  206.  
  207. /*cria tabela e adicionar uma linha à global*/
  208. void func_declaration_sem(Node *node, symbol_table *global_table){
  209. symbol_table *tmp_st, *tabela_aux;
  210. symbol_element *anothertmp;
  211. Node *tmp_node, *func_name;
  212. char *strpointer, *return_type;
  213. param_type *paramlist=NULL;
  214.  
  215. /*obter o nome da funcao*/
  216. tmp_node = node->child;
  217. func_name = add_str_pointer(&return_type, tmp_node);
  218.  
  219.  
  220. anothertmp = global_table->symbol;
  221. if(invalid_use_void_decl(func_name))
  222. return;
  223.  
  224. if(check_params(func_name->brother)){
  225. return;
  226. }
  227. /*verifica se já foi declarada*/
  228. while(anothertmp != NULL){ /*Se já foi declarada antes, verifica parametros*/
  229. if(strcmp(func_name->value, anothertmp->name)==0){
  230. //check_params(func_name, anothertmp->paramtype);
  231. return;
  232. }
  233. anothertmp = anothertmp->next;
  234. }
  235.  
  236.  
  237.  
  238. tmp_st = create_symbol_table(func_name->value, 0); /*0 - cria tabela nao impressa*/
  239. /*ligar tabela*/
  240. tabela_aux = global_table;
  241. while(tabela_aux->next !=NULL)
  242. tabela_aux = tabela_aux->next;
  243.  
  244. tabela_aux->next = tmp_st;
  245. /*ler parametros da funcao*/
  246. tmp_node = tmp_node->brother;
  247. while(tmp_node != NULL && strcmp(tmp_node->node_type, "ParamList") != 0){
  248. tmp_node = tmp_node->brother;
  249. }
  250.  
  251. if(tmp_node == NULL){
  252. return;
  253. }
  254. tmp_node = tmp_node->child; /*primeiro paramDeclaration*/
  255.  
  256. while(tmp_node != NULL){
  257. strpointer = NULL;
  258. add_str_pointer(&strpointer, tmp_node->child);
  259.  
  260. if(strpointer != NULL){
  261. paramlist = add_entry_param(paramlist, strpointer);
  262. }
  263.  
  264. tmp_node = tmp_node->brother;
  265. }
  266. add_symbol(global_table, func_name->value, return_type, 0, paramlist);
  267. }
  268.  
  269. void func_definition_sem(Node *node, symbol_table *global_table){
  270. symbol_table *tmp_st = NULL, *tabela_aux, *tmp_st1;
  271. symbol_element *anothertmp;
  272. Node *tmp_node, *id_node, *func_name;
  273. char *strpointer, *return_type;
  274. param_type *paramlist=NULL;
  275.  
  276. tmp_node = node->child; /*tipo da funcao*/
  277. /* terceiro parametro - flag para a funcao CHECK_VOID */
  278. func_name = add_str_pointer(&return_type, tmp_node);
  279.  
  280. anothertmp = global_table->symbol;
  281. if(invalid_use_void_decl(func_name))
  282. return;
  283. if(check_params(func_name->brother)){
  284. return;
  285. }
  286. /*verifica se já foi declarada*/
  287. while(anothertmp != NULL){ /*Se já foi declarada antes, verifica parametros*/
  288. if(strcmp(func_name->value, anothertmp->name)==0){
  289. //if(check_params(func_name, anothertmp->paramtype))
  290. //return;
  291. break;
  292. }
  293. anothertmp = anothertmp->next;
  294. }
  295.  
  296.  
  297. /*Se nao foi declarada*/
  298. if(anothertmp == NULL){
  299. tmp_st1 = create_symbol_table(func_name->value, 1);
  300. add_symbol(tmp_st1, "return", return_type, 0, NULL);
  301. tabela_aux = global_table;
  302. while(tabela_aux->next !=NULL)
  303. tabela_aux = tabela_aux->next;
  304. tabela_aux->next = tmp_st1;
  305. tmp_st = tabela_aux->next;
  306. }
  307. else{ /*Se ja foi declarada*/
  308. tabela_aux = global_table;
  309. while(tabela_aux != NULL){
  310. if(strcmp(tabela_aux->title, func_name->value) == 0){
  311. tabela_aux->definition = 1;
  312. add_symbol(tabela_aux, "return", return_type, 0, NULL);
  313. tmp_st = tabela_aux;
  314. break;
  315. }
  316. tabela_aux = tabela_aux->next;
  317. }
  318. if(tabela_aux==NULL) return;
  319. }
  320.  
  321. /*ler parametros da funcao*/
  322. tmp_node = node->child;
  323. while(tmp_node != NULL && strcmp(tmp_node->node_type, "ParamList") != 0){
  324. tmp_node = tmp_node->brother;
  325. }
  326. if(tmp_node == NULL) return;
  327.  
  328. tmp_node = tmp_node->child; /*primeiro paramDeclaration*/
  329.  
  330. while(tmp_node != NULL){
  331. strpointer = NULL;
  332. id_node = add_str_pointer(&strpointer, tmp_node->child);
  333. /*Adiciona a local*/
  334. if(id_node!= NULL){
  335. if(is_defined(id_node, global_table, tmp_st1, strpointer)==0){
  336. add_symbol(tmp_st, id_node->value, strpointer, 1, NULL);
  337. paramlist = add_entry_param(paramlist, strpointer);
  338. }
  339. }else{
  340. paramlist = add_entry_param(paramlist, strpointer);
  341. }
  342.  
  343. tmp_node = tmp_node->brother;
  344. }
  345. tmp_node = NULL;
  346.  
  347.  
  348. if(anothertmp == NULL) /*Se nao foi ainda declarada, adiciona entrada na GLOBAL*/
  349. add_symbol(global_table, func_name->value, return_type, 0, paramlist);
  350.  
  351. /*variaveis locais - funcbody*/
  352. tmp_node = node->child;
  353. while(tmp_node != NULL && strcmp(tmp_node->node_type, "FuncBody") != 0)
  354. tmp_node = tmp_node->brother;
  355. if(tmp_node == NULL) return;
  356.  
  357. /*encontrou funcbody*/
  358. tmp_node = tmp_node->child;
  359. while(tmp_node != NULL && (strcmp(tmp_node->node_type, "Declaration")== 0 || strcmp(tmp_node->node_type, "ArrayDeclaration")== 0)){
  360. declaration_arr_var(global_table, tmp_st, tmp_node);
  361. tmp_node = tmp_node->brother;
  362. }
  363. if(tmp_node != NULL){
  364. find_expr(tmp_st, global_table, tmp_node);
  365. }
  366. }
  367.  
  368.  
  369. void print_tables(symbol_table *tabela_inicial){
  370. symbol_table *tmp_table = tabela_inicial;
  371. symbol_element *tmp_symbol;
  372. param_type *pttmp;
  373. /*tabela global*/
  374. printf("===== %s =====\n", tabela_inicial->title);
  375. tmp_symbol = tabela_inicial->symbol;
  376. while(tmp_symbol != NULL){
  377. if(tmp_symbol->paramtype == NULL)
  378. printf("%s\t%s\n",tmp_symbol->name, tmp_symbol->type);
  379. else{
  380. printf("%s\t%s(",tmp_symbol->name, tmp_symbol->type);
  381. pttmp = tmp_symbol->paramtype;
  382. printf("%s", pttmp->type_name);
  383. pttmp = pttmp->next;
  384. while(pttmp != NULL){
  385. printf(",%s", pttmp->type_name);
  386. pttmp=pttmp->next;
  387. }
  388. printf(")\n");
  389. }
  390. tmp_symbol=tmp_symbol->next;
  391. }
  392.  
  393. /*Restantes tabelas*/
  394. tmp_symbol = NULL;
  395. tmp_table = tmp_table->next;
  396. while(tmp_table != NULL){
  397. if(tmp_table->definition == 1){
  398. printf("\n===== Function %s Symbol Table =====\n",tmp_table->title);
  399. tmp_symbol = tmp_table->symbol;
  400. while(tmp_symbol != NULL){
  401. if(tmp_symbol->param==0)
  402. printf("%s\t%s\n",tmp_symbol->name, tmp_symbol->type);
  403. else
  404. printf("%s\t%s\tparam\n",tmp_symbol->name, tmp_symbol->type);
  405. tmp_symbol = tmp_symbol->next;
  406. }
  407. }
  408. tmp_table = tmp_table->next;
  409. }
  410. printf("\n");
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement