Advertisement
Guest User

Untitled

a guest
May 27th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.19 KB | None | 0 0
  1. #include "semantics.h"
  2. char* type_spec[] = {"char","int","void","short","double","undef"};
  3. Symbol* new_symbol(char* id,symbol_type s_type){
  4. Symbol* elem = (Symbol*)malloc(sizeof(Symbol));
  5. elem->id = id;
  6. elem->s_type = s_type;
  7. elem->is_param=0;
  8. elem->params = (Symbol**)malloc(2000*sizeof(Symbol*));
  9. elem->type = "undef";
  10. elem->num_params = 0;
  11. elem->func_definition = NULL;
  12. elem->next = NULL;
  13. return elem;
  14. }
  15. char* tolowercase(char* type){
  16. if(strcmp(type,"Char")==0)
  17. return "char";
  18. else if(strcmp(type,"Int")==0)
  19. return "int";
  20. else if(strcmp(type,"Void")==0)
  21. return "void";
  22. else if(strcmp(type,"Short")==0)
  23. return "short";
  24. else if(strcmp(type,"Double")==0)
  25. return "double";
  26. return "null";
  27.  
  28. }
  29. Symbol* init_symbol_table(Node* node){
  30. Symbol* st = new_symbol(node->type,Global);
  31. Symbol* putchar = new_symbol("putchar",FuncDecl);
  32. Symbol* getchar = new_symbol("getchar",FuncDecl);
  33. putchar->type = "int";
  34. putchar->num_params = 1;
  35. putchar->params[0] = new_symbol("var",Variable);
  36. putchar->params[0]->type = "int";
  37. getchar->type = "int";
  38. getchar->num_params=1;
  39. getchar->params[0] = new_symbol("var",Variable);
  40. getchar->params[0]->type = "void";
  41. putchar->next = getchar;
  42. st->next = putchar;
  43. return st;
  44.  
  45.  
  46. }
  47. Symbol* search_symbol(char *type,Symbol* function){
  48. Symbol* symbol = NULL;
  49. if(function!=NULL){
  50. Symbol *aux = function->next;
  51. while(aux != NULL){
  52. if(strcmp(aux->id, type)==0)
  53. symbol = aux;
  54. aux = aux->next;
  55. }
  56. }
  57. if(symbol==NULL || function==NULL){
  58. Symbol *aux = ST->next;
  59. while(aux != NULL){
  60. if(strcmp(aux->id, type)==0)
  61. symbol = aux;
  62. aux = aux->next;
  63. }
  64. }
  65. return symbol;
  66.  
  67. }
  68. Symbol* search_function(char *type,Node* node){
  69. Symbol* symbol = NULL;
  70. Symbol *aux = ST->next;
  71. while(aux != NULL){
  72. if(strcmp(aux->id, type)==0){
  73. if(aux->num_params == 1 && strcmp(aux->params[0]->type,"void")==0 && node->numChilds==1){
  74. return aux;
  75. }
  76. else if(aux->num_params == node->numChilds-1){
  77. return aux;
  78. }
  79. else
  80. symbol = aux;
  81.  
  82. }
  83. aux = aux->next;
  84. }
  85. return symbol;
  86.  
  87. }
  88. Symbol* search_variable(char *type,Symbol* function){
  89. Symbol* symbol = NULL;
  90. if(function!=NULL){
  91. Symbol *aux = function->next;
  92. while(aux != NULL){
  93. if(strcmp(aux->id, type)==0 && aux->num_params==0)
  94. symbol = aux;
  95. aux = aux->next;
  96. }
  97. }
  98. if(symbol==NULL || function==NULL){
  99. Symbol *aux = ST->next;
  100. while(aux != NULL){
  101. if(strcmp(aux->id, type)==0 && aux->num_params==0)
  102. symbol = aux;
  103. aux = aux->next;
  104. }
  105. }
  106. return symbol;
  107.  
  108. }
  109. Symbol* search_on_function(char *type,Symbol* function){
  110. if(function!=NULL){
  111. Symbol *aux = function->next;
  112. while(aux != NULL){
  113. if(strcmp(aux->id, type)==0)
  114. return aux;
  115. aux = aux->next;
  116. }
  117. }
  118. return NULL;
  119.  
  120. }
  121. int check_params(Node *node,Symbol* ST){
  122. int i,j;
  123. char* params1 = (char*)malloc(512*sizeof(char)),*params2 = (char*)malloc(512*sizeof(char));
  124. sprintf(params1,"%s(",tolowercase(node->childs[0]->type));
  125. sprintf(params2,"%s(",ST->type);
  126. for(i = 0;i< node->childs[2]->numChilds;i++){
  127. Symbol* newsymbol;
  128. if(node->childs[2]->numChilds>1 && strcmp(tolowercase(node->childs[2]->childs[i]->childs[0]->type),"void")==0){//se for mais do que um parametro, e um deles for void
  129. error_semantic = 1;
  130. printf("Line %d, col %d: Invalid use of void type in declaration\n",node->childs[2]->childs[i]->childs[0]->countline,node->childs[2]->childs[i]->childs[0]->countcol);
  131. return 0;
  132. }else if(node->childs[2]->numChilds==1 && strcmp(tolowercase(node->childs[2]->childs[i]->childs[0]->type),"void")==0 && node->childs[2]->childs[i]->numChilds>=2){// se for só 1 parametro, mas for void + variavel
  133. error_semantic = 1;
  134. printf("Line %d, col %d: Invalid use of void type in declaration\n",node->childs[2]->childs[i]->childs[0]->countline,node->childs[2]->childs[i]->childs[0]->countcol);
  135. return 0;
  136. }else if(node->childs[2]->childs[i]->numChilds>=2 && search_on_function(node->childs[2]->childs[i]->childs[1]->value,ST->func_definition)){
  137. error_semantic = 1;
  138. printf("Line %d, col %d: Symbol %s already defined\n",node->childs[2]->childs[i]->childs[1]->countline,node->childs[2]->childs[i]->childs[1]->countcol,node->childs[2]->childs[i]->childs[1]->value);
  139. return 0;
  140. }else if(i<ST->num_params && strcmp(ST->params[i]->type,tolowercase(node->childs[2]->childs[i]->childs[0]->type))!=0){//se no caso de a funcao ter sido declarada previamente, os tipos dos parametros nao coincidirem
  141.  
  142. error_semantic = 1;
  143. for(j=0;j<ST->num_params;j++){
  144. if(j>0){
  145. strcat(params1,",");
  146. strcat(params1,tolowercase(node->childs[2]->childs[j]->childs[0]->type));
  147. strcat(params2,",");
  148. strcat(params2,ST->params[j]->type);
  149. }
  150. else{
  151. strcat(params1,tolowercase(node->childs[2]->childs[j]->childs[0]->type));
  152. strcat(params2,ST->params[j]->type);
  153. }
  154.  
  155. }
  156. strcat(params1,")");
  157. strcat(params2,")");
  158. printf("Line %d, col %d: Conflicting types (got %s, expected %s)\n",node->childs[1]->countline,node->childs[1]->countcol,params1,params2);
  159. return 0;
  160. }else if(strcmp(node->childs[2]->childs[i]->childs[0]->type,"Void")!=0 && node->childs[2]->childs[i]->numChilds>=2){
  161. newsymbol = new_symbol(node->childs[2]->childs[i]->childs[1]->value,Variable);
  162. newsymbol->is_param = 1;
  163. newsymbol->type = (char*)strdup(tolowercase(node->childs[2]->childs[i]->childs[0]->type));
  164. add_symbol(ST->func_definition,newsymbol);
  165. }
  166.  
  167. }
  168. return 1;
  169.  
  170. }
  171. void check_funcdefinition(Node* node, Symbol* ST){ //verificar se tem o mesmo nº de parametros
  172. int i;
  173.  
  174. Symbol* aux = search_function(node->childs[1]->value,node->childs[2]);
  175.  
  176. Symbol* newsymbol = new_symbol(node->childs[1]->value,FuncTable);
  177. char* params1 = (char*)malloc(512*sizeof(char)),*params2 = (char*)malloc(512*sizeof(char));
  178. int checkparam;
  179. newsymbol->type = tolowercase(node->childs[0]->type);
  180. if(aux == NULL){
  181. check_funcdeclaration(node,ST);
  182. aux = search_function(node->childs[1]->value,node->childs[2]);
  183. }
  184.  
  185. if(aux!=NULL){
  186. /*aux->func_definition = newsymbol;
  187. check_return(aux->func_definition);
  188. checkparam = check_params(node,aux);*/
  189. checkparam = 1;
  190. if(checkparam && aux->func_definition !=NULL){
  191. //error_semantic = 1;
  192. //printf("Line %d, col %d: Symbol %s already defined\n",node->childs[1]->countline,node->childs[1]->countcol,node->childs[1]->value);
  193. Node* childs = node->childs[3];
  194. aux->func_definition = newsymbol;
  195. for(i = 0; i < childs->numChilds;i++){
  196. check_variable(childs->childs[i],aux->func_definition);
  197. }
  198. }
  199. if(checkparam && aux->num_params==node->childs[2]->numChilds){
  200. Node* childs = node->childs[3];
  201. aux->func_definition = newsymbol;
  202. check_return(aux->func_definition);
  203. checkparam = check_params(node,aux);
  204. for(i = 0; i < childs->numChilds;i++){
  205. check_variable(childs->childs[i],aux->func_definition);
  206. }
  207. }
  208.  
  209. if(checkparam && aux->num_params!=node->childs[2]->numChilds){
  210. error_semantic = 1;
  211. sprintf(params1,"%s(",newsymbol->type);
  212. if(aux->num_params ==0)
  213. sprintf(params2,"%s",aux->type);
  214. else
  215. sprintf(params2,"%s(",aux->type);
  216. for(i=0;i<node->childs[2]->numChilds || i<aux->num_params;i++){
  217. if(i>0){
  218. if(i<node->childs[2]->numChilds)
  219. strcat(params1,",");
  220. if(aux->num_params>0 && i<aux->num_params)
  221. strcat(params2,",");
  222.  
  223. }
  224. if(i<node->childs[2]->numChilds)
  225. strcat(params1,tolowercase(node->childs[2]->childs[i]->childs[0]->type));
  226. if(aux->num_params>0 && i<aux->num_params)
  227. strcat(params2,aux->params[i]->type);
  228.  
  229.  
  230. }
  231. strcat(params1,")");
  232. if(aux->num_params>0)
  233. strcat(params2,")");
  234. printf("Line %d, col %d: Conflicting types (got %s, expected %s)\n",node->childs[1]->countline,node->childs[1]->countcol,params1,params2);
  235. aux->func_definition =NULL;
  236. }else if(checkparam && strcmp(aux->type,tolowercase(node->childs[0]->type))!=0){
  237. sprintf(params1,"%s(",tolowercase(node->childs[0]->type));
  238. sprintf(params2,"%s(",aux->type);
  239. error_semantic = 1;
  240. for(i=0;i<aux->num_params;i++){
  241. if(i>0){
  242. strcat(params1,",");
  243. strcat(params2,",");
  244.  
  245. }
  246. strcat(params1,tolowercase(node->childs[2]->childs[i]->childs[0]->type));
  247. strcat(params2,aux->params[i]->type);
  248. }
  249. strcat(params1,")");
  250. strcat(params2,")");
  251. printf("Line %d, col %d: Conflicting types (got %s, expected %s)\n",node->childs[1]->countline,node->childs[1]->countcol,params1,params2);
  252. aux->func_definition = NULL;
  253.  
  254. }
  255.  
  256. }
  257.  
  258.  
  259. }
  260. void check_ifwhile(Node* node, Symbol* ST){
  261. int i;
  262. for(i=0;i<node->numChilds;i++){
  263. check_variable(node->childs[i],ST);
  264. }
  265.  
  266. }
  267. Node* check_program(Node* node){
  268. error_semantic = 0;
  269. int i;
  270. ST = init_symbol_table(node);
  271. if(strcmp(node->type,"Program")==0){
  272. for(i=0;i<node->numChilds;i++){
  273. if(strcmp(node->childs[i]->type,"Declaration")==0){
  274. check_declaration(node->childs[i],ST,0);
  275. }
  276. else if(strcmp(node->childs[i]->type,"FuncDeclaration")==0){
  277. check_funcdeclaration(node->childs[i],ST);
  278. }
  279. else if(strcmp(node->childs[i]->type,"FuncDefinition")==0){
  280. check_funcdefinition(node->childs[i],ST);
  281. }
  282. }
  283. }
  284. return node;
  285.  
  286. }
  287. void check_declaration(Node* node, Symbol* ST,int is_on_function){
  288. Symbol* newsymbol = new_symbol(node->childs[1]->value,Variable);
  289. newsymbol->type = tolowercase(node->childs[0]->type);
  290. Symbol* symb;
  291. int add = 1;
  292. if(is_on_function){
  293. symb = search_on_function(node->childs[1]->value,ST);
  294. if(strcmp(node->childs[0]->type,"Void")==0){ // se for do tipo void
  295. add = 0;
  296. error_semantic = 1;
  297. printf("Line %d, col %d: Invalid use of void type in declaration\n",node->childs[1]->countline,node->childs[1]->countcol);
  298. }
  299. if(symb != NULL){ // se ja tiver sido declarado na funcao
  300. add = 0;
  301. error_semantic = 1;
  302. printf("Line %d, col %d: Symbol %s already defined\n",node->childs[1]->countline,node->childs[1]->countcol,node->childs[1]->value);
  303. }
  304. }else{
  305. if(strcmp(node->childs[0]->type,"Void")==0){ // se for do tipo void
  306. add = 0;
  307. error_semantic = 1;
  308. printf("Line %d, col %d: Invalid use of void type in declaration\n",node->childs[1]->countline,node->childs[1]->countcol);
  309. }
  310. else if((symb = search_symbol(node->childs[1]->value,NULL))!=NULL && symb->num_params>=1){ //se existir e for uma funcao
  311. add = 0;
  312. error_semantic = 1;
  313. printf("Line %d, col %d: Symbol %s already defined\n",node->childs[1]->countline,node->childs[1]->countcol,node->childs[1]->value);
  314. }else if(symb!=NULL && strcmp(symb->type,tolowercase(node->childs[0]->type))!=0){ // se existir e for de tipo diferente
  315. error_semantic = 1;
  316. add = 0;
  317. printf("Line %d, col %d: Symbol %s already defined\n",node->childs[1]->countline,node->childs[1]->countcol,node->childs[1]->value);
  318. }else if(symb!=NULL) // se existir, não volta a adicionar na tabela de simbolos
  319. add = 0;
  320. }
  321.  
  322. if(add)
  323. add_symbol(ST,newsymbol);
  324. if(node->numChilds>=3)
  325. check_variable(node->childs[2],ST);
  326.  
  327.  
  328.  
  329. }
  330.  
  331. void check_variable(Node* terminal,Symbol *ST){
  332. int i;
  333. if(strcmp(terminal->type,"IntLit")==0){
  334. terminal->s_type = "int";
  335. }
  336. else if(strcmp(terminal->type,"ChrLit")==0)
  337. terminal->s_type = "int";
  338. else if(strcmp(terminal->type,"RealLit")==0)
  339. terminal->s_type = "double";
  340. else if(strcmp(terminal->type,"Call")==0){
  341. check_call(terminal,ST);
  342. }
  343. else if(strcmp(terminal->type,"Id")==0){
  344. check_id(terminal,ST);
  345. }
  346. else if(strcmp(terminal->type,"Add")==0 || strcmp(terminal->type,"Sub")==0 || strcmp(terminal->type,"Mul")==0 || strcmp(terminal->type,"Div")==0){
  347. check_addsubmultdiv(terminal,ST);
  348. }
  349. else if(strcmp(terminal->type,"Mod")==0){
  350. check_mod(terminal,ST);
  351.  
  352. }
  353. else if(strcmp(terminal->type,"Eq")==0 || strcmp(terminal->type,"Ne")==0 || strcmp(terminal->type,"Lt")==0 || strcmp(terminal->type,"Le")==0 || strcmp(terminal->type,"Ge")==0 || strcmp(terminal->type,"Gt")==0)
  354. check_eq_ne_lt_le_ge(terminal,ST);
  355. else if(strcmp(terminal->type,"Store")==0)
  356. check_store(terminal,ST);
  357. else if(strcmp(terminal->type,"Minus")==0 || strcmp(terminal->type,"Not")==0 || strcmp(terminal->type,"Plus")==0)
  358. check_minusplusnot(terminal,ST);
  359. else if(strcmp(terminal->type,"Declaration")==0){
  360. check_declaration(terminal,ST,1);
  361. }
  362. else if(strcmp(terminal->type,"Return")==0){
  363. verify_return(terminal,ST);
  364. }
  365. else if(strcmp(terminal->type,"Or")==0 || strcmp(terminal->type,"And")==0)
  366. check_and_or(terminal,ST);
  367. else if(strcmp(terminal->type,"BitWiseAnd")==0 || strcmp(terminal->type,"BitWiseOr")==0 || strcmp(terminal->type,"BitWiseXor")==0)
  368. check_bitwise(terminal,ST);
  369. else if(strcmp(terminal->type,"StatList")==0){
  370. for(i=0;i<terminal->numChilds;i++)
  371. check_variable(terminal->childs[i],ST);
  372. }
  373. else if(strcmp(terminal->type,"If")==0 || strcmp(terminal->type,"While")==0){
  374. check_ifwhile(terminal,ST);
  375. }
  376. else if(strcmp(terminal->type,"Comma")==0){
  377. check_comma(terminal,ST);
  378. }
  379.  
  380. }
  381. void check_comma(Node* terminal, Symbol* ST){
  382. int i;
  383. for(i=0;i<terminal->numChilds;i++)
  384. check_variable(terminal->childs[i],ST);
  385. terminal->s_type = terminal->childs[1]->s_type;
  386. }
  387. void check_and_or(Node* terminal, Symbol* ST){
  388. int i;
  389. for(i=0;i<terminal->numChilds;i++){
  390. check_variable(terminal->childs[i],ST);
  391. }
  392.  
  393. if(strcmp(terminal->childs[0]->s_type,"void")==0){
  394. error_semantic = 1;
  395. if(strcmp(terminal->type,"And")==0)
  396. printf("Line %d, col %d: Operator && cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol,terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  397. else
  398. printf("Line %d, col %d: Operator || cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol,terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  399.  
  400. }
  401.  
  402. else
  403. terminal->s_type = "int";
  404.  
  405.  
  406. }
  407. void check_bitwise(Node* terminal, Symbol* ST){
  408. int i;
  409. for(i=0;i<terminal->numChilds;i++){
  410. check_variable(terminal->childs[i],ST);
  411. }
  412. if(strcmp(terminal->childs[0]->s_type,"int")==0 && strcmp(terminal->childs[1]->s_type,"int")==0)
  413. terminal->s_type = "int";
  414. else{
  415. error_semantic = 1;
  416. if(strcmp(terminal->type,"BitWiseAnd")==0)
  417. printf("Line %d, col %d: Operator & cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol,terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  418. else if(strcmp(terminal->type,"BitWiseOr")==0)
  419. printf("Line %d, col %d: Operator | cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol,terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  420. else
  421. printf("Line %d, col %d: Operator ^ cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol,terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  422.  
  423. }
  424. terminal->s_type = "int";
  425.  
  426. }
  427. void verify_return(Node* terminal,Symbol* ST){
  428. int i;
  429.  
  430. for(i=0;i<terminal->numChilds;i++){
  431. check_variable(terminal->childs[i],ST);
  432. /*if(i == 0){
  433. if(strcmp(ST->type,terminal->childs[0]->s_type)!=0){
  434. error_semantic = 1;
  435. printf("Line %d, col %d: Conflicting types (got %s, expected %s)\n",terminal->countline,terminal->countcol,terminal->childs[0]->s_type,ST->type);
  436. }
  437.  
  438. }*/
  439. }
  440.  
  441. }
  442. void check_minusplusnot(Node* terminal,Symbol* ST){
  443.  
  444. check_variable(terminal->childs[0],ST);
  445. if(strcmp(terminal->childs[0]->s_type,"void")==0){
  446. error_semantic = 1;
  447. if(strcmp(terminal->type,"Minus")==0)
  448. printf("Line %d, col %d: Operator - cannot be applied to type %s\n",terminal->childs[0]->countline,terminal->childs[0]->countcol,terminal->childs[0]->s_type);
  449. else if(strcmp(terminal->type,"Plus")==0)
  450. printf("Line %d, col %d: Operator + cannot be applied to type %s\n",terminal->childs[0]->countline,terminal->childs[0]->countcol, terminal->childs[0]->s_type);
  451. else
  452. printf("Line %d, col %d: Operator ! cannot be applied to type %s\n",terminal->childs[0]->countline,terminal->childs[0]->countcol, terminal->childs[0]->s_type);
  453. }
  454. else if(strcmp(terminal->type,"Not")==0)
  455. terminal->s_type = (char*)strdup("int");
  456. else
  457. terminal->s_type = terminal->childs[0]->s_type;
  458. if(strcmp(terminal->type,"Minus")== 0){
  459. char aux[100] = "";
  460. aux[0] = '-';
  461. strcat((char*)aux,(char*)terminal->childs[0]->value);
  462. terminal->value = strdup(aux);
  463. }
  464. else
  465. terminal->value = terminal->childs[0]->value;
  466.  
  467. }
  468. void check_store(Node* terminal,Symbol* function){
  469. int i;
  470.  
  471. for(i=0;i<terminal->numChilds;i++){
  472.  
  473.  
  474. check_variable(terminal->childs[i],function);
  475. }
  476.  
  477. if(strcmp(terminal->childs[0]->type,"Id")!=0){
  478. error_semantic = 1;
  479. printf("Line %d, col %d: Lvalue required\n",terminal->childs[0]->countline,terminal->childs[0]->countcol);
  480. }
  481. else{
  482. terminal->s_type = terminal->childs[0]->s_type;
  483. }
  484. }
  485. void check_eq_ne_lt_le_ge(Node* terminal, Symbol* function){
  486. int i;
  487. for(i=0;i<terminal->numChilds;i++){
  488. check_variable(terminal->childs[i],function);
  489. }
  490. if(strcmp(terminal->childs[0]->s_type,"void")==0 || strcmp(terminal->childs[1]->s_type,"void") == 0){
  491. error_semantic = 1;
  492. if(strcmp(terminal->type,"Eq")==0)
  493. printf("Line %d, col %d: Operator == cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  494.  
  495. else if(strcmp(terminal->type,"Ne")==0)
  496. printf("Line %d, col %d: Operator != cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  497.  
  498. else if(strcmp(terminal->type,"Lt")==0)
  499. printf("Line %d, col %d: Operator < cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  500.  
  501. else if(strcmp(terminal->type,"Gt")==0)
  502. printf("Line %d, col %d: Operator > cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  503.  
  504.  
  505. else if(strcmp(terminal->type,"Le")==0)
  506. printf("Line %d, col %d: Operator <= cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  507. else if(strcmp(terminal->type,"Ge")==0)
  508. printf("Line %d, col %d: Operator >= cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  509. }
  510.  
  511.  
  512. else
  513. terminal->s_type = (char*)strdup("int");
  514.  
  515. }
  516. void check_id(Node* terminal, Symbol* function){
  517. Symbol* symbol = search_variable(terminal->value,function);
  518. if(symbol!=NULL){
  519. terminal->s_type = (char*)strdup(symbol->type);
  520.  
  521. }else{
  522. error_semantic =1;
  523. printf("Line %d, col %d: Unknown symbol %s\n",terminal->countline,terminal->countcol,terminal->value);
  524. terminal->s_type = (char*)strdup("undef2");
  525. }
  526.  
  527. }
  528. void check_mod(Node* terminal,Symbol* ST){
  529.  
  530. int i;
  531. for(i=0;i<terminal->numChilds;i++){
  532. check_variable(terminal->childs[i],ST);
  533. }
  534. if(strcmp(terminal->childs[0]->s_type,"int")==0 && strcmp(terminal->childs[1]->s_type,"int")==0){
  535. terminal->s_type = (char*)strdup(terminal->childs[0]->s_type);
  536. }
  537. else{
  538. error_semantic = 1;
  539. printf("Line %d, col %d: Operator %% cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  540. terminal->s_type = (char*)strdup("int");
  541. }
  542.  
  543. }
  544. void check_addsubmultdiv(Node* terminal,Symbol* ST){
  545. int i;
  546. for(i=0;i<terminal->numChilds;i++){
  547. check_variable(terminal->childs[i],ST);
  548. }
  549. if(strcmp(terminal->childs[0]->s_type,"void")==0 || strcmp(terminal->childs[1]->s_type,"void") == 0){
  550. error_semantic = 1;
  551. if(strcmp(terminal->type,"Add")==0)
  552. printf("Line %d, col %d: Operator + cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  553.  
  554. else if(strcmp(terminal->type,"Sub")==0)
  555. printf("Line %d, col %d: Operator - cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  556.  
  557. else
  558. printf("Line %d, col %d: Operator / cannot be applied to types %s, %s\n",terminal->countline,terminal->countcol, terminal->childs[0]->s_type,terminal->childs[1]->s_type);
  559.  
  560. }else{
  561. if(strcmp(terminal->childs[0]->s_type,terminal->childs[1]->s_type)==0){
  562. terminal->s_type = terminal->childs[0]->s_type;
  563. }
  564. else if((strcmp(terminal->childs[0]->s_type,"double")==0 && (strcmp(terminal->childs[1]->s_type,"char")==0 || strcmp(terminal->childs[1]->s_type,"int")==0 || strcmp(terminal->childs[1]->s_type,"short")==0)) || (strcmp(terminal->childs[1]->s_type,"double")==0 && (strcmp(terminal->childs[0]->s_type,"char")==0 || strcmp(terminal->childs[0]->s_type,"int")==0 || strcmp(terminal->childs[0]->s_type,"short")==0)))
  565. terminal->s_type = "double";
  566. else if((strcmp(terminal->childs[0]->s_type,"char")==0 && strcmp(terminal->childs[1]->s_type,"int")==0) || (strcmp(terminal->childs[1]->s_type,"char")==0 && strcmp(terminal->childs[0]->s_type,"int")==0))
  567. terminal->s_type = "int";
  568. else if((strcmp(terminal->childs[0]->s_type,"short")==0 && strcmp(terminal->childs[1]->s_type,"int")==0) || (strcmp(terminal->childs[1]->s_type,"short")==0 && strcmp(terminal->childs[0]->s_type,"int")==0))
  569. terminal->s_type = "int";
  570. else if((strcmp(terminal->childs[0]->s_type,"short")==0 && strcmp(terminal->childs[1]->s_type,"char")==0) || (strcmp(terminal->childs[1]->s_type,"short")==0 && strcmp(terminal->childs[0]->s_type,"char")==0))
  571. terminal->s_type = "short";
  572. else
  573. terminal->s_type = (char*)strdup(terminal->childs[0]->s_type);
  574. }
  575.  
  576. }
  577. void check_call(Node* call,Symbol* function){
  578. int i;
  579. Symbol* symbol = search_function(call->childs[0]->value,call);
  580. if(symbol!=NULL){
  581. if(symbol->num_params>0 && ((strcmp(symbol->params[0]->type,"void")==0 && symbol->num_params==call->numChilds) ||(strcmp(symbol->params[0]->type,"void")!=0 && symbol->num_params==call->numChilds-1))){
  582. call->childs[0]->is_function=1;
  583. call->childs[0]->s_type = symbol->type;
  584. call->childs[0]->num_params = symbol->num_params;
  585. for(i=0;i<symbol->num_params;i++){
  586. call->childs[0]->params[i] = symbol->params[i]->type;
  587.  
  588. }
  589. for(i=1;i<call->numChilds;i++){
  590. check_variable(call->childs[i],function);
  591. }
  592. call->s_type = symbol->type;
  593. }else if(symbol->num_params==0){
  594. error_semantic = 1;
  595. printf("Line %d, col %d: Symbol %s is not a function\n",call->childs[0]->countline,call->childs[0]->countcol,call->childs[0]->value);
  596. call->s_type = (char*)strdup(call->childs[0]->s_type);
  597. }else if(symbol->num_params>0 && strcmp(symbol->params[0]->type,"void")==0){
  598. error_semantic = 1;
  599. printf("Line %d, col %d: Wrong number of arguments to function %s (got %d, required %d)\n",call->childs[0]->countline,call->childs[0]->countcol,call->childs[0]->value,call->numChilds-1,symbol->num_params-1);
  600. }else{
  601. error_semantic = 1;
  602. printf("Line %d, col %d: Wrong number of arguments to function %s (got %d, required %d)\n",call->childs[0]->countline,call->childs[0]->countcol,call->childs[0]->value,call->numChilds-1,symbol->num_params);
  603. }
  604. }else{
  605. error_semantic = 1;
  606. for(i=0;i<call->numChilds;i++){
  607. check_variable(call->childs[i],function);
  608. }
  609. call->s_type = (char*)strdup(call->childs[0]->s_type);
  610. printf("Line %d, col %d: Symbol %s is not a function\n",call->childs[0]->countline,call->childs[0]->countcol,call->childs[0]->value);
  611.  
  612. }
  613. }
  614. void check_return(Symbol* ST){
  615. Symbol* newsymbol = new_symbol("return",Return);
  616. newsymbol->type = (char*)strdup(ST->type);
  617. add_symbol(ST,newsymbol);
  618.  
  619. }
  620. void check_funcdeclaration(Node* node,Symbol* ST){
  621. Symbol* newsymbol = new_symbol(node->childs[1]->value,FuncDecl);
  622. Symbol* symb;
  623. int add=1;
  624. newsymbol->type = (char*)strdup(tolowercase(node->childs[0]->type));
  625. int i,j,k;
  626. int errorvoid = 0;
  627. char* params1=(char*)malloc(512*sizeof(char)),*params2=(char*)malloc(512*sizeof(char));
  628. newsymbol->num_params = node->childs[2]->numChilds;
  629. char* var = (char*)malloc(512*sizeof(char));
  630. for(i=0;i<newsymbol->num_params;i++){
  631. sprintf(var,"%s%d","var",i);
  632. if(node->childs[2]->childs[i]->numChilds>=2)
  633. newsymbol->params[i] = new_symbol(node->childs[2]->childs[i]->childs[1]->value,Variable);
  634. else
  635. newsymbol->params[i] = new_symbol(var,Variable);
  636. for(k=0;k<i;k++){
  637.  
  638. if(node->childs[2]->childs[i]->numChilds>=2){
  639. if(strcmp(newsymbol->params[k]->id,node->childs[2]->childs[i]->childs[1]->value)==0){
  640. printf("Line %d, col %d: Symbol %s already defined\n",node->childs[2]->childs[i]->childs[1]->countline,node->childs[2]->childs[i]->childs[1]->countcol,node->childs[2]->childs[i]->childs[1]->value);
  641. error_semantic = 1;
  642. errorvoid = 1;
  643. break;
  644. }
  645. }
  646.  
  647. }
  648. newsymbol->params[i]->type = tolowercase(node->childs[2]->childs[i]->childs[0]->type);
  649.  
  650. if(!errorvoid && newsymbol->num_params>1 && strcmp(newsymbol->params[i]->type,"void")==0){
  651. add = 0;
  652. error_semantic = 1;
  653. printf("Line %d, col %d: Invalid use of void type in declaration\n",node->childs[2]->childs[i]->childs[0]->countline,node->childs[2]->childs[i]->childs[0]->countcol);
  654. errorvoid = 1;
  655.  
  656. }else if(!errorvoid && newsymbol->num_params==1 && strcmp(newsymbol->params[i]->type,"void")==0 && node->childs[2]->childs[i]->numChilds>=2){
  657. add = 0;
  658. error_semantic = 1;
  659. printf("Line %d, col %d: Invalid use of void type in declaration\n",node->childs[2]->childs[i]->childs[0]->countline,node->childs[2]->childs[i]->childs[0]->countcol);
  660. errorvoid = 1;
  661.  
  662. }
  663.  
  664. }
  665. if((symb = search_function(node->childs[1]->value,node->childs[2]))!=NULL && symb->num_params!=newsymbol->num_params){
  666. add = 0;
  667. if(!errorvoid){
  668. error_semantic = 1;
  669. sprintf(params1,"%s(",newsymbol->type);
  670. if(symb->num_params ==0)
  671. sprintf(params2,"%s",symb->type);
  672. else
  673. sprintf(params2,"%s",symb->type);
  674. for(i=0;i<newsymbol->num_params;i++){
  675. if(i>0){
  676. strcat(params1,",");
  677. if(symb->num_params>0)
  678. strcat(params2,",");
  679.  
  680. }
  681. strcat(params1,newsymbol->params[i]->type);
  682. if(symb->num_params>0)
  683. strcat(params2,symb->params[i]->type);
  684.  
  685.  
  686. }
  687. strcat(params1,")");
  688. if(symb->num_params>0)
  689. strcat(params2,")");
  690. printf("Line %d, col %d: Conflicting types (got %s, expected %s)\n",node->childs[1]->countline,node->childs[1]->countcol,params1,params2);
  691.  
  692. }
  693. }
  694. else if(symb!=NULL && symb->num_params==newsymbol->num_params){
  695. add = 0;
  696.  
  697. if(!errorvoid){
  698. sprintf(params1,"%s(",tolowercase(node->childs[0]->type));
  699. sprintf(params2,"%s(",symb->type);
  700. for(i=0;i<symb->num_params;i++){
  701. if(strcmp(symb->params[i]->type,newsymbol->params[i]->type)!=0 || strcmp(symb->type,newsymbol->type)!=0){
  702. error_semantic = 1;
  703. for(j=0;j<symb->num_params;j++){
  704. if(j>0){
  705. strcat(params1,",");
  706. strcat(params1,tolowercase(node->childs[2]->childs[j]->childs[0]->type));
  707. strcat(params2,",");
  708. strcat(params2,symb->params[j]->type);
  709. }
  710. else{
  711. strcat(params1,tolowercase(node->childs[2]->childs[j]->childs[0]->type));
  712. strcat(params2,symb->params[j]->type);
  713. }
  714.  
  715. }
  716. strcat(params1,")");
  717. strcat(params2,")");
  718. printf("Line %d, col %d: Conflicting types (got %s, expected %s)\n",node->childs[1]->countline,node->childs[1]->countcol,params1,params2);
  719. break;
  720. }
  721.  
  722. }
  723. }
  724.  
  725. }
  726. if(add)
  727. add_symbol(ST,newsymbol);
  728.  
  729. }
  730.  
  731. void add_symbol(Symbol* ST, Symbol* new_symbol){
  732. Symbol* cur_node = ST;
  733. while(cur_node->next !=NULL){
  734. cur_node = cur_node->next;
  735. }
  736. cur_node->next = new_symbol;
  737.  
  738. }
  739.  
  740. void printST(){
  741. Symbol *child_symbols;
  742. Symbol *cur_symbol = ST;
  743. while (cur_symbol !=NULL){
  744. print_element(cur_symbol);
  745. cur_symbol = cur_symbol->next;
  746. }
  747. cur_symbol = ST->next;
  748. while(cur_symbol !=NULL){
  749. if(cur_symbol->func_definition !=NULL){
  750. child_symbols = cur_symbol->func_definition;
  751. while(child_symbols!=NULL){
  752. print_element(child_symbols);
  753. child_symbols = child_symbols->next;
  754. }
  755. }
  756. cur_symbol = cur_symbol->next;
  757. }
  758.  
  759. }
  760. void print_element(Symbol* symbol){
  761. int i;
  762. if(symbol->s_type==Global){
  763. printf("===== Global Symbol Table =====\n");
  764. }
  765. else if(symbol->s_type==FuncDecl){
  766. printf("%s\t%s(",symbol->id,symbol->type);
  767. if(symbol->num_params>0){
  768. printf("%s",symbol->params[0]->type);
  769. for(i=1;i<symbol->num_params;i++){
  770. printf(",%s",symbol->params[i]->type);
  771. }
  772. }
  773. printf(")\n");
  774. }
  775. else if(symbol->s_type==Variable){
  776. printf("%s\t%s",symbol->id,symbol->type);
  777. if(symbol->is_param)
  778. printf("\tparam");
  779. printf("\n");
  780. }
  781. else if(symbol->s_type==Return){
  782. printf("return\t%s\n",symbol->type);
  783. }
  784. else if(symbol->s_type==FuncTable){
  785. printf("\n===== Function %s Symbol Table =====\n",symbol->id);
  786. }
  787.  
  788.  
  789. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement