WhaleSpunk

Untitled

May 4th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 102.39 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #define DEBUG 0
  7.  
  8. typedef struct AST_Tree{
  9. char *type;
  10. struct AST_Tree* child;
  11. struct AST_Tree* brother;
  12. char *value;
  13. char *notacao;
  14. struct AST_Tree* next;
  15. }AST_Tree_node;
  16.  
  17. typedef struct Table{
  18. char *name;
  19. char *type;
  20. char *paramsType;
  21. char *flag;
  22. struct Table* brother;
  23. struct Table* child;
  24. }Table_node;
  25.  
  26. Table_node *head_table = NULL;
  27.  
  28.  
  29. AST_Tree_node *head = NULL;
  30.  
  31. AST_Tree_node* insertNode(char* node);
  32. AST_Tree_node* insert_value_node(char* node, char* value);
  33. void insert_child(AST_Tree_node* father, AST_Tree_node* child);
  34. void insert_brother(AST_Tree_node* brother, AST_Tree_node* node);
  35. void printTree(AST_Tree_node* aux, int level);
  36. void printTerminal(AST_Tree_node *node);
  37. void printPoints(int n);
  38.  
  39.  
  40. int tolower(int c);
  41.  
  42.  
  43. void printTable(Table_node *node);
  44. Table_node* insertNode_Table(char* node);
  45. Table_node* insert_value_node_Table(char* name, char* type, char* paramsType, char* flag);
  46. void insert_child_Table(Table_node* father, Table_node* child);
  47. void insert_brother_Table(Table_node* brother, Table_node* node);
  48. Table_node* find_symbol(char* name);
  49. Table_node* find_symbol_Method(char *name, char *params);
  50. Table_node *check_later_func(char *name, char *type);
  51. Table_node *check_later_vars_func(char *name);
  52. void insert_next(AST_Tree_node* first, AST_Tree_node *node);
  53.  
  54. int parse = 0;
  55. int flag=0;
  56. int aux2 = 0;
  57. int imprimir=0;
  58. int imprimir3=0;
  59. int yylex(void);
  60. void yyerror(const char *s);
  61.  
  62. extern int n_linha;
  63. extern int n_coluna;
  64. extern char * yytext;
  65. extern int yyleng;
  66.  
  67. Table_node *aux_node_table;
  68.  
  69. AST_Tree_node *nodeAux;
  70. AST_Tree_node *nodeAux2;
  71. AST_Tree_node *nodeAux3;
  72. AST_Tree_node *nodeAux4;
  73. AST_Tree_node *nodeAux5;
  74. Table_node *nodeAux_Table;
  75. Table_node *nodeAux_Table2;
  76. Table_node *nodeAux_Table3;
  77. Table_node *nodeAux_Table4;
  78.  
  79.  
  80.  
  81. Table_node *varDecl;
  82.  
  83. Table_node *current_Method;
  84.  
  85. AST_Tree_node *check_later;
  86. AST_Tree_node *check_later_vars;
  87.  
  88.  
  89.  
  90. %}
  91.  
  92. %union{
  93. char *str;
  94. struct AST_Tree *node_struct;
  95. }
  96.  
  97. %token <str> ID STRLIT BOOLLIT DECLIT REALLIT
  98.  
  99. %token PUBLIC BOOL INT DOUBLE SEMI COMMA PARSEINT OCURV OSQUARE CSQUARE CCURV ASSIGN AND OR EQ GEQ GT LEQ LT NEQ PLUS MINUS STAR DIV MOD NOT DOTLENGTH STATIC STRING VOID CLASS OBRACE CBRACE IF ELSE WHILE DO PRINT RETURN RESERVED
  100.  
  101. %type <node_struct> Program FieldDecl_MethodDecl_SEMI FieldDecl CommaID MethodDecl MethodHeader FormalParams CommaTypeID MethodBody VarDecl_Statement VarDecl Type Statement Statement_aux Expr ExprNew Assignment MethodInvocation CommaExpr ParseArgs
  102.  
  103.  
  104.  
  105. %left COMMA
  106. %right ASSIGN
  107. %left OR
  108. %left AND
  109. %left EQ NEQ
  110. %left LT GT LEQ GEQ
  111. %left MINUS PLUS
  112. %left STAR DIV MOD
  113. %right NOT
  114. %left OCURV OBRACE OSQUARE CCURV CBRACE CSQUARE
  115.  
  116. %nonassoc ELSE
  117.  
  118. %%
  119.  
  120.  
  121.  
  122.  
  123. Program: CLASS ID OBRACE FieldDecl_MethodDecl_SEMI CBRACE {if(flag == 0)
  124. {$$ = head = insertNode("Program");nodeAux = insert_value_node("Id", $2); insert_child($$, nodeAux);
  125. //head_table = insert_value_node_Table($2, "Class", "", "");
  126. //printf("\nHEAD TABLE TYPE: %s\n", head_table->type);
  127. head_table->name = $2;
  128. head_table->type = "Class";
  129.  
  130.  
  131.  
  132.  
  133. if($4!= NULL){
  134. insert_child($$, $4);
  135.  
  136. }
  137. if(imprimir3==1){
  138. char s[5] = " - ";
  139. char *token;
  140. while(check_later_vars !=NULL){
  141.  
  142.  
  143. if(strcmp(check_later_vars->type, "Assign") == 0){ //FALTA FAZER PARA TODAS AS OUTRAS EXPRESSIONS (MINUS, STAR, DIV, MOD)...
  144. Table_node *var = check_later_vars_func(check_later_vars->child->value);
  145. //printf("\nCHECK LATER VAR VALUE: %s; VAR VALUE: %s\n", check_later_vars->child->value, var->type);
  146. if(strcmp(var->type, "") == 0){
  147. //printf("\n-------ERRO!-----\n");
  148. check_later_vars->notacao = (char *)malloc(sizeof(" - undef"));
  149. strcpy(check_later_vars->notacao, " - undef");
  150. check_later_vars->child->notacao = (char *)malloc(sizeof(" - undef"));
  151. strcpy(check_later_vars->child->notacao, " - undef");
  152. }else{
  153. char var_type[100];
  154.  
  155. strcpy(var_type, " - ");
  156.  
  157. strcat(var_type, var->type);
  158. check_later_vars->notacao = (char *)malloc(sizeof(var_type));
  159.  
  160. strcpy(check_later_vars->notacao, var_type);
  161.  
  162. check_later_vars->child->notacao = (char *)malloc(sizeof(var_type));
  163. strcpy(check_later_vars->child->notacao, var_type);
  164.  
  165. }
  166. }else if (strcmp(check_later_vars->type, "Id") == 0){
  167. Table_node *var = check_later_vars_func(check_later_vars->value);
  168. if(strcmp(var->type, "") == 0){
  169. check_later_vars->notacao = (char *)malloc(sizeof(" - undef"));
  170. strcpy(check_later_vars->notacao, " - undef");
  171. }else{
  172.  
  173. char var_type[100];
  174.  
  175. strcpy(var_type, " - ");
  176.  
  177. strcat(var_type, var->type);
  178. check_later_vars->notacao = (char *)malloc(sizeof(var_type));
  179.  
  180. strcpy(check_later_vars->notacao, var_type);
  181.  
  182. }
  183.  
  184. }
  185.  
  186.  
  187.  
  188. check_later_vars = check_later_vars->next;
  189.  
  190. }
  191.  
  192.  
  193.  
  194.  
  195. while(check_later != NULL){
  196. char type[100];
  197. char name[100];
  198.  
  199. strcpy(name, check_later->child->value);
  200.  
  201. strcpy(type, "(");
  202.  
  203. AST_Tree_node *aux = check_later->child->brother;
  204. while(aux != NULL){
  205.  
  206. token = strtok(aux->notacao, s);
  207. if(token != NULL){
  208. strcat(type, token);
  209.  
  210. if(aux->brother !=NULL){
  211. strcat(type, ",");
  212. }
  213. }
  214. aux = aux->brother;
  215.  
  216.  
  217. }
  218. strcat(type, ")\0");
  219. Table_node *method = find_symbol_Method(name, type);
  220. //printf("\nname: %s; type: %s; method->type: %s;\n", name, type, method->type);
  221. if(strcmp(method->type, "") == 0){
  222. //printf("\n-------ERRO!-----\n");
  223.  
  224.  
  225. }else{
  226. char call_type[100];
  227.  
  228. strcpy(call_type, " - ");
  229.  
  230. strcat(call_type, method->type);
  231. check_later->notacao = (char *)malloc(sizeof(call_type));
  232.  
  233. strcpy(check_later->notacao, call_type);
  234.  
  235.  
  236. char methodID_type[100];
  237.  
  238. strcpy(methodID_type, " - ");
  239.  
  240. strcat(methodID_type, type);
  241. check_later->child->notacao = (char *)malloc(sizeof(methodID_type));
  242.  
  243. strcpy(check_later->child->notacao, methodID_type);
  244.  
  245.  
  246. }
  247.  
  248. check_later = check_later->next;
  249. }
  250.  
  251.  
  252.  
  253.  
  254.  
  255. }
  256. }
  257.  
  258. }
  259. | CLASS ID OBRACE CBRACE {if(flag==0){
  260. $$ = head = insertNode("Program");
  261. nodeAux = insert_value_node("Id", $2);
  262. insert_child($$, nodeAux);head_table= insertNode_Table($2);
  263. aux_node_table=insertNode_Table($2);
  264. //head_table= insert_value_node_Table($2, "Class", "", "");
  265. head_table->name = $2;
  266. head_table->type = "Class";
  267. }
  268. }
  269. ;
  270.  
  271. FieldDecl_MethodDecl_SEMI: FieldDecl_MethodDecl_SEMI FieldDecl {if(flag ==0){if($1 != NULL){$$ =$1;insert_brother($$, $2);}else{$$ = $2;}}}
  272. | FieldDecl_MethodDecl_SEMI MethodDecl {if(flag == 0){if($1 != NULL){$$=$1; insert_brother($$, $2);}else{$$ = $2;}}}
  273. | FieldDecl_MethodDecl_SEMI SEMI {if(flag == 0){if($1 != NULL){$$ = $1;}else{$$ = NULL;}}}
  274. | FieldDecl {if(flag ==0 ){$$ = $1;}}
  275. | MethodDecl {if(flag == 0){$$ = $1 ;}}
  276. | SEMI {if(flag == 0){$$ = NULL;}}
  277. ;
  278.  
  279.  
  280.  
  281.  
  282.  
  283. FieldDecl: PUBLIC STATIC Type ID CommaID SEMI {if(flag ==0){
  284. int i=0;
  285. int length4 = strlen($3->type);
  286. char* lower4 = ( char* )malloc( length4 + 1 );
  287. if(strcmp($3->type, "Bool")==0){
  288. nodeAux_Table = insert_value_node_Table($4, "boolean", "", "");
  289. }else{
  290.  
  291. lower4[length4] = 0;
  292. for(i = 0; i < length4; i++ )
  293. {
  294. lower4[i] = tolower( $3->type[i]);
  295. }
  296.  
  297.  
  298. nodeAux_Table = insert_value_node_Table($4, lower4, "", "");
  299.  
  300.  
  301. }
  302.  
  303. insert_child_Table(head_table, nodeAux_Table);
  304.  
  305. $$ = insertNode("FieldDecl");
  306. insert_child($$, $3);
  307. AST_Tree_node *nodeAuxaux = insert_value_node("Id", $4);
  308. insert_child($$, nodeAuxaux);
  309. nodeAux2 = $5;
  310. AST_Tree_node *node = nodeAux2;
  311. AST_Tree_node *copy;
  312. AST_Tree_node *copy2;
  313.  
  314. while(node != NULL){
  315.  
  316.  
  317.  
  318. if(strcmp($3->type, "Bool")==0){
  319. insert_child_Table(head_table, insert_value_node_Table(node->value, "boolean", "", ""));
  320. }else{
  321.  
  322. insert_child_Table(head_table, insert_value_node_Table(node->value, lower4, "", ""));
  323. }
  324.  
  325.  
  326.  
  327. copy = insertNode($3->type);
  328. copy2 = insert_value_node("Id", node->value);
  329. nodeAux3 = insertNode("FieldDecl");
  330. insert_brother($$, nodeAux3);
  331. insert_child(nodeAux3, copy);
  332. insert_child(nodeAux3, copy2);
  333. node = nodeAux2->brother;
  334. nodeAux2 = nodeAux2->brother;
  335. }
  336. }
  337.  
  338. }
  339.  
  340. | PUBLIC STATIC Type ID SEMI {if(flag == 0){
  341. int i=0;
  342. int length7 = strlen($3->type);
  343. char* lower7 = ( char* )malloc( length7 + 1 );
  344. $$ = insertNode("FieldDecl");
  345. insert_child($$, $3);
  346. nodeAux = insert_value_node("Id", $4);
  347. insert_child($$, nodeAux);
  348.  
  349.  
  350.  
  351.  
  352. if(strcmp($3->type, "Bool")==0){
  353.  
  354. nodeAux_Table = insert_value_node_Table($4, "boolean", "", "");
  355. }else{
  356.  
  357. lower7[length7] = 0;
  358. for(i = 0; i < length7; i++ )
  359. {
  360. lower7[i] = tolower( $3->type[i] );
  361. }
  362.  
  363.  
  364. nodeAux_Table = insert_value_node_Table($4, lower7, "", "");
  365.  
  366.  
  367. }
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376. insert_child_Table(head_table, nodeAux_Table);
  377.  
  378.  
  379.  
  380. }
  381. }
  382. | error SEMI {flag = 1;$$=NULL;}
  383. ;
  384.  
  385. CommaID: CommaID COMMA ID {if(flag == 0){$$ = $1 ; nodeAux = insert_value_node("Id", $3); insert_brother($$, nodeAux);}}
  386. | COMMA ID {if(flag == 0){$$ = insert_value_node("Id", $2);}}
  387. ;
  388.  
  389. MethodDecl: PUBLIC STATIC MethodHeader MethodBody {if(flag ==0 ){
  390.  
  391. $$ = insertNode("MethodDecl");
  392. insert_child($$, $3);
  393. insert_child($$, $4);
  394.  
  395.  
  396. if(strcmp(current_Method->type, "") != 0){
  397. insert_child_Table(head_table, current_Method);
  398. current_Method= insert_value_node_Table("", "", "", "");
  399. }
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406. }
  407. }
  408. ;
  409.  
  410. MethodHeader: Type ID OCURV FormalParams CCURV {if(flag == 0){
  411. int i;
  412. $$ = insertNode("MethodHeader");
  413. insert_child($$, $1);
  414. nodeAux = insert_value_node("Id", $2);
  415. insert_child($$, nodeAux);
  416. insert_child($$, $4);
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423. nodeAux_Table = insertNode_Table($2);
  424.  
  425.  
  426.  
  427. nodeAux_Table->type = $1->type;
  428.  
  429.  
  430.  
  431. if(strcmp(nodeAux_Table->type, "Bool")==0){
  432. nodeAux_Table->type = "boolean";
  433. insert_child_Table(nodeAux_Table, insert_value_node_Table("return", "boolean", "", ""));
  434. }else{
  435. int length2 = strlen(nodeAux_Table->type);
  436. char* lower2 = ( char* )malloc( length2 + 1 );
  437. lower2[length2] = 0;
  438. for(i = 0; i < length2; i++ )
  439. {
  440. lower2[i] = tolower( nodeAux_Table->type[i] );
  441. }
  442. nodeAux_Table->type = lower2;
  443.  
  444. insert_child_Table(nodeAux_Table, insert_value_node_Table("return", lower2,"",""));
  445. }
  446.  
  447.  
  448.  
  449.  
  450.  
  451. nodeAux4 = $4;
  452.  
  453. char param[100];
  454. strcpy(param, "(");
  455. AST_Tree_node *node = nodeAux4->child;
  456. while(node!=NULL){
  457.  
  458. if(strcmp(node->child->type, "Bool")==0){
  459. strcat(param, "boolean");
  460. insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "boolean", "", "param"));
  461. }else if(strcmp(node->child->type, "StringArray")==0){
  462. strcat(param, "String[]");
  463. insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "String[]", "", "param"));
  464.  
  465. }else{
  466.  
  467. int length = strlen(node->child->type);
  468. char* lower = ( char* )malloc( length + 1 );
  469. lower[length] = 0;
  470. for(i = 0; i < length; i++ )
  471. {
  472. lower[i] = tolower( node->child->type[i] );
  473. }
  474.  
  475.  
  476. strcat(param, lower);
  477. insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, lower, "", "param"));
  478.  
  479. }
  480.  
  481. if( node->brother != NULL){
  482. strcat(param, ",");
  483. }
  484.  
  485.  
  486.  
  487. node = node->brother;
  488.  
  489.  
  490. }
  491. strcat(param, ")\0");
  492. nodeAux_Table->paramsType =(char *)malloc(sizeof(param)+1);
  493. strcpy(nodeAux_Table->paramsType, param);
  494. nodeAux_Table->flag = "";
  495.  
  496.  
  497.  
  498. //insert_child_Table(head_table, nodeAux_Table);
  499.  
  500.  
  501. current_Method = nodeAux_Table;
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516. }
  517. }
  518. | Type ID OCURV CCURV {if(flag == 0){
  519. int i=0;
  520. $$ = insertNode("MethodHeader");
  521. insert_child($$, $1);
  522. nodeAux = insert_value_node("Id", $2);
  523. insert_child($$, nodeAux);
  524. nodeAux3=insertNode("MethodParams");
  525. insert_child($$,nodeAux3);
  526.  
  527. nodeAux_Table = insertNode_Table($2);
  528.  
  529. nodeAux_Table->type = $1->type;
  530.  
  531. if(strcmp(nodeAux_Table->type, "Bool")==0){
  532. nodeAux_Table->type = "boolean";
  533. insert_child_Table(nodeAux_Table, insert_value_node_Table("return", "boolean", "", ""));
  534. }else{
  535. int length2 = strlen(nodeAux_Table->type);
  536. char* lower2 = ( char* )malloc( length2 + 1 );
  537. lower2[length2] = 0;
  538. for(i = 0; i < length2; i++ )
  539. {
  540. lower2[i] = tolower( nodeAux_Table->type[i] );
  541. }
  542. nodeAux_Table->type = lower2;
  543.  
  544. insert_child_Table(nodeAux_Table, insert_value_node_Table("return", lower2,"",""));
  545. }
  546.  
  547. char param[100];
  548. strcpy(param, "(");
  549. strcat(param, ")\0");
  550. nodeAux_Table->paramsType =(char *)malloc(sizeof(param)+1);
  551. strcpy(nodeAux_Table->paramsType, param);
  552. nodeAux_Table->flag = "";
  553.  
  554.  
  555.  
  556. //insert_child_Table(head_table, nodeAux_Table);
  557.  
  558.  
  559. current_Method = nodeAux_Table;
  560.  
  561. }
  562.  
  563. }
  564. | VOID ID OCURV FormalParams CCURV {if(flag == 0){
  565. int i=0;
  566. $$ = insertNode("MethodHeader");
  567. nodeAux = insertNode("Void");
  568. insert_child($$, nodeAux);
  569. nodeAux2 = insert_value_node("Id", $2);
  570. insert_child($$, nodeAux2);
  571. insert_child($$, $4);
  572.  
  573. nodeAux_Table = insertNode_Table($2);
  574.  
  575. nodeAux_Table->type = "void";
  576. insert_child_Table(nodeAux_Table, insert_value_node_Table("return", "void","",""));
  577.  
  578.  
  579.  
  580.  
  581. nodeAux4 = $4;
  582.  
  583. char param[100];
  584. strcpy(param, "(");
  585. AST_Tree_node *node = nodeAux4->child;
  586. while(node!=NULL){
  587.  
  588. if(strcmp(node->child->type, "Bool")==0){
  589. strcat(param, "boolean");
  590. insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "boolean", "", "param"));
  591. }else if(strcmp(node->child->type, "StringArray")==0){
  592. strcat(param, "String[]");
  593. insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "String[]", "", "param"));
  594.  
  595. }else{
  596.  
  597. int length = strlen(node->child->type);
  598. char* lower = ( char* )malloc( length + 1 );
  599. lower[length] = 0;
  600. for(i = 0; i < length; i++ )
  601. {
  602. lower[i] = tolower( node->child->type[i] );
  603. }
  604.  
  605.  
  606. strcat(param, lower);
  607. insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, lower, "", "param"));
  608.  
  609. }
  610.  
  611. if( node->brother != NULL){
  612. strcat(param, ",");
  613. }
  614.  
  615.  
  616.  
  617. node = node->brother;
  618.  
  619.  
  620. }
  621. strcat(param, ")\0");
  622. nodeAux_Table->paramsType =(char *)malloc(sizeof(param)+1);
  623. strcpy(nodeAux_Table->paramsType, param);
  624. nodeAux_Table->flag = "";
  625.  
  626. current_Method = nodeAux_Table;
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634. }
  635. }
  636. | VOID ID OCURV CCURV {if(flag == 0){
  637. $$ = insertNode("MethodHeader");
  638. nodeAux = insertNode("Void");
  639. insert_child($$, nodeAux);
  640. nodeAux2 = insert_value_node("Id", $2);
  641. insert_child($$, nodeAux2);
  642. nodeAux3=insertNode("MethodParams");
  643. insert_child($$,nodeAux3);
  644.  
  645.  
  646. nodeAux_Table = insertNode_Table($2);
  647.  
  648. nodeAux_Table->type = "void";
  649. insert_child_Table(nodeAux_Table, insert_value_node_Table("return", "void","",""));
  650.  
  651. char param[100];
  652. strcpy(param, "(");
  653. strcat(param, ")\0");
  654. nodeAux_Table->paramsType =(char *)malloc(sizeof(param)+1);
  655. strcpy(nodeAux_Table->paramsType, param);
  656. nodeAux_Table->flag = "";
  657.  
  658.  
  659.  
  660. //insert_child_Table(head_table, nodeAux_Table);
  661.  
  662.  
  663. current_Method = nodeAux_Table;
  664.  
  665.  
  666.  
  667.  
  668.  
  669. }
  670.  
  671. }
  672. ;
  673.  
  674.  
  675.  
  676.  
  677.  
  678. FormalParams: Type ID CommaTypeID {if(flag == 0){
  679. $$ = insertNode("MethodParams");
  680. nodeAux2 = insertNode("ParamDecl");
  681. insert_child($$, nodeAux2);
  682. insert_child(nodeAux2, $1);
  683. nodeAux = insert_value_node("Id", $2);
  684. insert_child(nodeAux2, nodeAux);
  685. nodeAux2 = $3;
  686. AST_Tree_node *node = nodeAux2;
  687. AST_Tree_node *copy;
  688. AST_Tree_node *copy2;
  689. while(node!=NULL){
  690.  
  691. copy = insertNode(node->type);
  692. copy2 = insert_value_node("Id", node->brother->value);
  693. nodeAux3 = insertNode("ParamDecl");
  694. insert_child($$, nodeAux3);
  695. insert_child(nodeAux3, copy);
  696. insert_child(nodeAux3, copy2);
  697. node = node->brother->brother;
  698.  
  699. }
  700.  
  701. }}
  702. | Type ID {if(flag == 0){$$ = insertNode("MethodParams"); nodeAux2 = insertNode("ParamDecl"); insert_child($$, nodeAux2); insert_child(nodeAux2, $1); nodeAux = insert_value_node("Id", $2); insert_child(nodeAux2, nodeAux);}}
  703. | STRING OSQUARE CSQUARE ID {if(flag == 0){$$ = insertNode("MethodParams"); nodeAux2 = insertNode("ParamDecl"); insert_child($$, nodeAux2); nodeAux = insertNode("StringArray"); insert_child(nodeAux2, nodeAux); nodeAux3 = insert_value_node("Id", $4); insert_child(nodeAux2, nodeAux3);}}
  704. ;
  705.  
  706.  
  707. CommaTypeID: COMMA Type ID {if(flag == 0){$$ = $2; nodeAux = insert_value_node("Id", $3); insert_brother($$, nodeAux);}}
  708. | CommaTypeID COMMA Type ID {if(flag ==0){$$ = $1; insert_brother($$, $3); nodeAux2 = insert_value_node("Id", $4); insert_brother($$, nodeAux2);}}
  709. ;
  710.  
  711.  
  712. MethodBody: OBRACE VarDecl_Statement CBRACE {if(flag == 0){$$ = insertNode("MethodBody"); if($2!=NULL){insert_child($$, $2);}}}
  713. | OBRACE CBRACE {if(flag == 0){$$ = insertNode("MethodBody");}}
  714. ;
  715.  
  716.  
  717. VarDecl_Statement: VarDecl {if(flag == 0){$$ = $1;}}
  718. | Statement {if(flag == 0){$$ = $1;}}
  719. | VarDecl_Statement VarDecl {if(flag == 0){if($1!=NULL){$$=$1;insert_brother($1,$2);}else{$$=$2;}}}
  720. | VarDecl_Statement Statement {if(flag == 0){if($1!=NULL){$$ = $1;if($2!=NULL){insert_brother($1,$2);}}else{$$=$2;}}}
  721.  
  722.  
  723. VarDecl: Type ID CommaID SEMI {if(flag == 0){
  724.  
  725. int i = 0;
  726. int length5 = strlen($1->type);
  727. char* lower5 = ( char* )malloc( length5 + 1 );
  728. $$ = insertNode("VarDecl");
  729. insert_child($$,$1);
  730.  
  731. nodeAux = insert_value_node("Id", $2);
  732. insert_child($$, nodeAux);
  733. nodeAux2 = $3;
  734. AST_Tree_node *node = nodeAux2;
  735. AST_Tree_node *copy;
  736. AST_Tree_node *copy2;
  737.  
  738. if(strcmp($1->type, "Bool") == 0){
  739.  
  740. if(strcmp(varDecl->type, "")==0){
  741. varDecl = insert_value_node_Table($2, "boolean", "", "");
  742. }else{
  743. insert_brother_Table(varDecl, insert_value_node_Table($2, "boolean", "", ""));
  744.  
  745. }
  746.  
  747.  
  748.  
  749.  
  750. }else{
  751.  
  752. lower5[length5] = 0;
  753. for(i = 0; i < length5; i++ )
  754. {
  755. lower5[i] = tolower( $1->type[i] );
  756. }
  757.  
  758.  
  759.  
  760. if(strcmp(varDecl->type, "") == 0){
  761. varDecl = insert_value_node_Table($2, lower5, "", "");
  762. }else{
  763. insert_brother_Table(varDecl, insert_value_node_Table($2, lower5, "", ""));
  764.  
  765. }
  766.  
  767. }
  768.  
  769. while(node != NULL){
  770.  
  771. if(strcmp($1->type, "Bool") == 0){
  772.  
  773. insert_brother_Table(varDecl, insert_value_node_Table(node->value, "boolean", "", ""));
  774.  
  775. }else{
  776. insert_brother_Table(varDecl, insert_value_node_Table(node->value, lower5, "", ""));
  777.  
  778. }
  779.  
  780.  
  781. copy = insertNode($1->type);
  782. copy2 = insert_value_node("Id", node->value);
  783. nodeAux3 = insertNode("VarDecl");
  784. insert_brother($$, nodeAux3);
  785. insert_child(nodeAux3, copy);
  786. insert_child(nodeAux3, copy2);
  787. node = nodeAux2->brother;
  788. nodeAux2 = nodeAux2->brother;
  789. }
  790.  
  791.  
  792.  
  793.  
  794. if(strcmp(varDecl->type, "")!=0){
  795. insert_child_Table(current_Method, varDecl);
  796. varDecl = insert_value_node_Table("", "", "", "");
  797. }
  798.  
  799.  
  800. }}
  801.  
  802. | Type ID SEMI {if(flag == 0){
  803. int i = 0;
  804. int length6 = strlen($1->type);
  805. char* lower6 = ( char* )malloc( length6 + 1 );
  806.  
  807.  
  808. if(strcmp($1->type, "Bool") == 0){
  809.  
  810. if(strcmp(varDecl->type, "")== 0){
  811.  
  812. varDecl = insert_value_node_Table($2, "boolean", "", "");
  813. }else{
  814.  
  815. insert_brother_Table(varDecl, insert_value_node_Table($2, "boolean", "", ""));
  816.  
  817. }
  818.  
  819. }else{
  820.  
  821. lower6[length6] = 0;
  822. for(i = 0; i < length6; i++ )
  823. {
  824. lower6[i] = tolower( $1->type[i] );
  825. }
  826.  
  827.  
  828. if(strcmp(varDecl->type, "")==0){
  829. varDecl = insert_value_node_Table($2, lower6, "", "");
  830. }else{
  831. insert_brother_Table(varDecl, insert_value_node_Table($2, lower6, "", ""));
  832.  
  833. }
  834.  
  835.  
  836. }
  837.  
  838.  
  839. if(strcmp(varDecl->type, "")!=0){
  840. insert_child_Table(current_Method, varDecl);
  841. varDecl = insert_value_node_Table("", "", "", "");
  842. }
  843.  
  844.  
  845.  
  846. $$ = insertNode("VarDecl");
  847. insert_child($$, $1);
  848. nodeAux2 = insert_value_node("Id", $2);
  849. insert_child($$, nodeAux2);
  850.  
  851. }
  852. }
  853. ;
  854.  
  855. Type: BOOL {if(flag == 0){$$ = insertNode("Bool");}}
  856. | INT {if(flag == 0){$$ = insertNode("Int");}}
  857. | DOUBLE {if(flag == 0){$$ =insertNode("Double");}}
  858. ;
  859.  
  860. Statement: OBRACE Statement_aux CBRACE {if(flag ==0){
  861. if($2!=NULL){
  862. if($2->brother!=NULL){
  863. nodeAux=insertNode("Block");
  864. insert_child(nodeAux,$2);
  865. $$=nodeAux;
  866. }
  867. else{
  868. $$=$2;
  869. }
  870. }
  871. else{
  872. $$=NULL;
  873. }
  874. }}
  875. | OBRACE CBRACE {if(flag==0){$$ = NULL;}}
  876. | IF OCURV Expr CCURV Statement {
  877. if(flag==0){
  878. $$=insertNode("If");
  879. insert_child($$,$3);
  880. nodeAux = $5;
  881. int num=0;
  882. if(nodeAux != NULL){
  883. AST_Tree_node *node = nodeAux;
  884. while(node!= NULL){
  885. node = node->brother;
  886. num++;
  887. }
  888. if(num==1){
  889. insert_child($$,nodeAux);
  890. nodeAux2 = insertNode("Block");
  891. insert_child($$, nodeAux2);
  892. }
  893. if(num>1){
  894. nodeAux2 = insertNode("Block");
  895. nodeAux3 = insertNode("Block");
  896. insert_child(nodeAux2, nodeAux);
  897. insert_child($$,nodeAux2);
  898. insert_brother(nodeAux2,nodeAux3);
  899. }
  900. }
  901. if(nodeAux==NULL)
  902. {
  903. nodeAux2 = insertNode("Block");
  904. nodeAux3 = insertNode("Block");
  905. insert_child($$, nodeAux2);
  906. insert_child($$, nodeAux3);
  907. }
  908. }
  909. }
  910.  
  911. | IF OCURV Expr CCURV Statement ELSE Statement {
  912. if(flag==0){
  913. $$=insertNode("If");
  914. insert_child($$,$3);
  915. nodeAux = $5;
  916. int num=0, num2=0;
  917.  
  918. if(nodeAux != NULL){
  919. AST_Tree_node *node = nodeAux;
  920. while(node!= NULL){
  921. node = node->brother;
  922. num++;
  923. }
  924. if(num==1){
  925. insert_child($$,nodeAux);
  926.  
  927. }
  928. if(num>1){
  929. nodeAux2 = insertNode("Block");
  930. insert_child(nodeAux2, nodeAux);
  931. insert_child($$,nodeAux2);
  932.  
  933. }
  934. }
  935. else if(nodeAux==NULL)
  936. {
  937. nodeAux2 = insertNode("Block");
  938. insert_child($$, nodeAux2);
  939.  
  940. }
  941.  
  942. if($7!=NULL)
  943. {
  944. AST_Tree_node *node2 = $7;
  945. while(node2!= NULL){
  946. node2 = node2->brother;
  947. num2++;
  948. }
  949. if(num2==1){
  950. insert_child($$,$7);
  951.  
  952. }
  953. if(num2>1){
  954. nodeAux2 = insertNode("Block");
  955. insert_child(nodeAux2, $7);
  956. insert_child($$,nodeAux2);
  957.  
  958. }
  959. }
  960.  
  961. else if($7==NULL)
  962. {
  963. nodeAux2 = insertNode("Block");
  964. insert_child($$, nodeAux2);
  965. }
  966. }
  967.  
  968.  
  969. }
  970. | WHILE OCURV Expr CCURV Statement {
  971. if(flag==0){
  972. $$=insertNode("While");
  973. insert_child($$,$3);
  974. nodeAux = $5;
  975. int num=0;
  976. if(nodeAux != NULL){
  977. AST_Tree_node *node = nodeAux;
  978. while(node!= NULL){
  979. node = node->brother;
  980. num++;
  981. }
  982. if(num==1){
  983. insert_child($$,nodeAux);
  984.  
  985. }
  986. if(num>1){
  987. nodeAux2 = insertNode("Block");
  988. insert_child(nodeAux2, nodeAux);
  989. insert_child($$,nodeAux2);
  990.  
  991. }
  992. }
  993. if(nodeAux==NULL)
  994. {
  995. nodeAux2 = insertNode("Block");
  996. insert_child($$, nodeAux2);
  997.  
  998. }
  999. }
  1000. }
  1001. | DO Statement WHILE OCURV Expr CCURV SEMI {
  1002. if(flag==0){
  1003. $$=insertNode("DoWhile");
  1004. if($2==NULL)
  1005. {
  1006. nodeAux2 = insertNode("Block");
  1007. insert_child($$, nodeAux2);
  1008.  
  1009. }else{
  1010. insert_child($$,$2);
  1011. }
  1012.  
  1013.  
  1014. nodeAux = $5;
  1015. int num=0;
  1016. if(nodeAux != NULL){
  1017. AST_Tree_node *node = nodeAux;
  1018. while(node!= NULL){
  1019. node = node->brother;
  1020. num++;
  1021. }
  1022. if(num==1){
  1023. insert_child($$,nodeAux);
  1024.  
  1025. }
  1026. if(num>1){
  1027. nodeAux2 = insertNode("Block");
  1028. insert_child(nodeAux2, nodeAux);
  1029. insert_child($$,nodeAux2);
  1030.  
  1031. }
  1032. }
  1033.  
  1034. }
  1035. }
  1036. | PRINT OCURV Expr CCURV SEMI {
  1037. if(flag==0){
  1038.  
  1039. $$=insertNode("Print");
  1040. insert_child($$,$3);
  1041. }
  1042. }
  1043. | PRINT OCURV STRLIT CCURV SEMI {
  1044. if(flag==0){
  1045.  
  1046.  
  1047. $$=insertNode("Print");
  1048. nodeAux = insert_value_node("StrLit", $3);
  1049. if(imprimir3==1){
  1050. nodeAux->notacao=" - String";
  1051. }
  1052. insert_child($$, nodeAux);
  1053. }
  1054. }
  1055. | SEMI {
  1056. if(flag==0){
  1057. $$=NULL;
  1058. }
  1059. }
  1060. | Assignment SEMI {
  1061. if(flag==0){
  1062. $$=$1;
  1063. }
  1064. }
  1065. | MethodInvocation SEMI {
  1066. if(flag==0){
  1067. $$=$1;
  1068. }
  1069. }
  1070. | ParseArgs SEMI {
  1071. if(flag==0){
  1072. $$=$1;
  1073. }
  1074. }
  1075. | RETURN SEMI {
  1076. if(flag==0){
  1077. $$ = insertNode("Return");
  1078. }
  1079. }
  1080. | RETURN Expr SEMI {
  1081. if(flag==0){
  1082. $$ = insertNode("Return");
  1083. insert_child($$,$2);
  1084.  
  1085. }
  1086. }
  1087. | error SEMI {flag = 1;$$=NULL;}
  1088. ;
  1089.  
  1090.  
  1091.  
  1092. Statement_aux: Statement {if(flag==0){$$=$1;}}
  1093. | Statement_aux Statement {if(flag==0){if($1!=NULL){$$ = $1; insert_brother($1,$2);}else{$$=$2;}}}
  1094. ;
  1095.  
  1096.  
  1097.  
  1098. Expr: ExprNew {if(flag == 0){$$ = $1;}}
  1099. | Assignment {if(flag == 0){$$ = $1;}}
  1100. ;
  1101.  
  1102. ExprNew: MethodInvocation {if(flag == 0){$$ = $1;}}
  1103. | ParseArgs {if(flag == 0){$$ = $1;}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - int"));strcpy($$->notacao, " - int");}}
  1104. | ExprNew AND ExprNew {if(flag == 0){$$ = insertNode("And"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - boolean"));strcpy($$->notacao, " - boolean");}}
  1105. | ExprNew OR ExprNew {if(flag == 0){$$ = insertNode("Or"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - boolean"));strcpy($$->notacao, " - boolean");}}
  1106. | ExprNew EQ ExprNew {if(flag == 0){$$ = insertNode("Eq"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - boolean"));strcpy($$->notacao, " - boolean");}}
  1107. | ExprNew GEQ ExprNew {if(flag == 0){$$ = insertNode("Geq"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - boolean"));strcpy($$->notacao, " - boolean");}}
  1108. | ExprNew GT ExprNew {if(flag == 0){$$ = insertNode("Gt"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - boolean"));strcpy($$->notacao, " - boolean");}}
  1109. | ExprNew LEQ ExprNew {if(flag == 0){$$ = insertNode("Leq"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - boolean"));strcpy($$->notacao, " - boolean");}}
  1110. | ExprNew LT ExprNew {if(flag == 0){$$ = insertNode("Lt"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - boolean"));strcpy($$->notacao, " - boolean");}}
  1111. | ExprNew NEQ ExprNew {if(flag == 0){$$ = insertNode("Neq"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - boolean"));strcpy($$->notacao, " - boolean");}}
  1112. | ExprNew PLUS ExprNew {if(flag == 0){$$ = insertNode("Add"); insert_child($$, $1); insert_child($$, $3);}
  1113. if(imprimir3==1){
  1114. int erro_aux = 0;
  1115. Table_node *aux;
  1116. Table_node *aux2;
  1117.  
  1118.  
  1119. char s[5] = " - ";
  1120. char *token;
  1121. if(strcmp($1->type, "Id") == 0){
  1122. aux = find_symbol($$->child->value);
  1123.  
  1124. }else{
  1125.  
  1126. aux = insert_value_node_Table("", "", "", "");
  1127. token = strtok($$->child->notacao, s);
  1128. char var2[100];
  1129. strcpy(var2, token);
  1130. aux->type = (char *)malloc(sizeof(var2)+1);;
  1131. strcpy(aux->type, var2);
  1132.  
  1133.  
  1134. }
  1135. if(strcmp($3->type, "Id") == 0){
  1136.  
  1137. aux2 = find_symbol($$->child->brother->value);
  1138. }
  1139. else{
  1140. aux2 = insert_value_node_Table("", "", "", "");
  1141. token = strtok($$->child->brother->notacao, s);
  1142. char var[100];
  1143. strcpy(var, token);
  1144. aux2->type = (char *)malloc(sizeof(var)+1);
  1145.  
  1146. strcpy(aux2->type, var);
  1147.  
  1148.  
  1149.  
  1150. }
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156. if(strcmp(aux->type, "") == 0){
  1157.  
  1158. //printf("\n-----ERRO!-----\n");
  1159. erro_aux = 1;
  1160.  
  1161. }
  1162. if(strcmp(aux2->type, "")==0){
  1163.  
  1164. //printf("\n-----ERRO!-----\n");
  1165. erro_aux =1;
  1166. }
  1167.  
  1168. if(erro_aux ==0){
  1169. char aux_string[100];
  1170.  
  1171. if(strcmp(aux->type, "String") ==0 || strcmp(aux2->type, "String")==0){
  1172. strcpy(aux_string, " - ");
  1173. strcat(aux_string, "String");
  1174. strcat(aux_string, "\0");
  1175. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1176. strcpy($$->notacao, aux_string);
  1177.  
  1178. }else if((strcmp(aux->type, "double") ==0 || strcmp(aux2->type, "double")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1179.  
  1180.  
  1181.  
  1182. strcpy(aux_string, " - ");
  1183. strcat(aux_string, "double");
  1184. strcat(aux_string, "\0");
  1185. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1186. strcpy($$->notacao, aux_string);
  1187.  
  1188. }else if((strcmp(aux->type, "int") ==0 || strcmp(aux2->type, "int")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1189. strcpy(aux_string, " - ");
  1190. strcat(aux_string, "int");
  1191. strcat(aux_string, "\0");
  1192. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1193. strcpy($$->notacao, aux_string);
  1194.  
  1195.  
  1196. }else{
  1197.  
  1198. //printf("\n-----ERRO!-----\n");
  1199.  
  1200.  
  1201. }
  1202.  
  1203.  
  1204. }
  1205.  
  1206.  
  1207.  
  1208. }}
  1209. | ExprNew MINUS ExprNew {if(flag == 0){$$ = insertNode("Sub"); insert_child($$, $1); insert_child($$, $3);}
  1210. if(imprimir3==1){
  1211.  
  1212.  
  1213. int erro_aux = 0;
  1214. Table_node *aux;
  1215. Table_node *aux2;
  1216.  
  1217.  
  1218. char s[5] = " - ";
  1219. char *token;
  1220. if(strcmp($1->type, "Id") == 0){
  1221.  
  1222. aux = find_symbol($$->child->value);
  1223.  
  1224. }else{
  1225.  
  1226. aux = insert_value_node_Table("", "", "", "");
  1227. token = strtok($$->child->notacao, s);
  1228. char var2[100];
  1229. strcpy(var2, token);
  1230. aux->type = (char *)malloc(sizeof(var2)+1);;
  1231.  
  1232. strcpy(aux->type, var2);
  1233.  
  1234.  
  1235. }
  1236. if(strcmp($3->type, "Id") == 0){
  1237.  
  1238. aux2 = find_symbol($$->child->brother->value);
  1239. }
  1240. else{
  1241. aux2 = insert_value_node_Table("", "", "", "");
  1242. token = strtok($$->child->brother->notacao, s);
  1243. char var[100];
  1244. strcpy(var, token);
  1245. aux2->type = (char *)malloc(sizeof(var)+1);
  1246.  
  1247. strcpy(aux2->type, var);
  1248.  
  1249.  
  1250.  
  1251. }
  1252.  
  1253.  
  1254. if(strcmp(aux->type, "") == 0){
  1255.  
  1256. //printf("\n-----ERRO!-----\n");
  1257. erro_aux = 1;
  1258.  
  1259. }
  1260. if(strcmp(aux2->type, "")==0){
  1261.  
  1262. //printf("\n-----ERRO!-----\n");
  1263. erro_aux =1;
  1264. }
  1265.  
  1266. if(erro_aux ==0){
  1267. char aux_string[100];
  1268.  
  1269. if(strcmp(aux->type, "String") ==0 || strcmp(aux2->type, "String")==0){
  1270. //printf("\n-----ERRO!-----\n");
  1271.  
  1272. }else if((strcmp(aux->type, "double") ==0 || strcmp(aux2->type, "double")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1273.  
  1274.  
  1275.  
  1276. strcpy(aux_string, " - ");
  1277. strcat(aux_string, "double");
  1278. strcat(aux_string, "\0");
  1279. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1280. strcpy($$->notacao, aux_string);
  1281.  
  1282. }else if((strcmp(aux->type, "int") ==0 || strcmp(aux2->type, "int")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1283. strcpy(aux_string, " - ");
  1284. strcat(aux_string, "int");
  1285. strcat(aux_string, "\0");
  1286. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1287. strcpy($$->notacao, aux_string);
  1288.  
  1289.  
  1290. }else{
  1291.  
  1292. //printf("\n-----ERRO!-----\n");
  1293.  
  1294.  
  1295. }
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303. }
  1304.  
  1305.  
  1306.  
  1307. }
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316. }
  1317. | ExprNew STAR ExprNew {if(flag == 0){$$ = insertNode("Mul"); insert_child($$, $1); insert_child($$, $3);}
  1318.  
  1319. if(imprimir3==1){
  1320.  
  1321. int erro_aux = 0;
  1322. Table_node *aux;
  1323. Table_node *aux2;
  1324.  
  1325.  
  1326. char s[5] = " - ";
  1327. char *token;
  1328. if(strcmp($1->type, "Id") == 0){
  1329. aux = find_symbol($$->child->value);
  1330.  
  1331. }else{
  1332.  
  1333. aux = insert_value_node_Table("", "", "", "");
  1334. token = strtok($$->child->notacao, s);
  1335. char var2[100];
  1336. strcpy(var2, token);
  1337. aux->type = (char *)malloc(sizeof(var2)+1);;
  1338. strcpy(aux->type, var2);
  1339.  
  1340.  
  1341. }
  1342. if(strcmp($3->type, "Id") == 0){
  1343.  
  1344. aux2 = find_symbol($$->child->brother->value);
  1345. }
  1346. else{
  1347. aux2 = insert_value_node_Table("", "", "", "");
  1348. token = strtok($$->child->brother->notacao, s);
  1349. char var[100];
  1350. strcpy(var, token);
  1351. aux2->type = (char *)malloc(sizeof(var)+1);
  1352.  
  1353. strcpy(aux2->type, var);
  1354.  
  1355.  
  1356.  
  1357. }
  1358.  
  1359. if(strcmp(aux->type, "") == 0){
  1360.  
  1361. //printf("\n-----ERRO!-----\n");
  1362. erro_aux = 1;
  1363.  
  1364. }
  1365. if(strcmp(aux2->type, "")==0){
  1366.  
  1367. //printf("\n-----ERRO!-----\n");
  1368. erro_aux =1;
  1369. }
  1370.  
  1371. if(erro_aux ==0){
  1372. char aux_string[100];
  1373.  
  1374. if(strcmp(aux->type, "String") ==0 || strcmp(aux2->type, "String")==0){
  1375. //printf("\n-----ERRO!-----\n");
  1376.  
  1377. }else if((strcmp(aux->type, "double") ==0 || strcmp(aux2->type, "double")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1378.  
  1379.  
  1380.  
  1381. strcpy(aux_string, " - ");
  1382. strcat(aux_string, "double");
  1383. strcat(aux_string, "\0");
  1384. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1385. strcpy($$->notacao, aux_string);
  1386.  
  1387. }else if((strcmp(aux->type, "int") ==0 || strcmp(aux2->type, "int")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1388. strcpy(aux_string, " - ");
  1389. strcat(aux_string, "int");
  1390. strcat(aux_string, "\0");
  1391. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1392. strcpy($$->notacao, aux_string);
  1393.  
  1394.  
  1395. }else{
  1396.  
  1397. //printf("\n-----ERRO!-----\n");
  1398.  
  1399.  
  1400. }
  1401.  
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408. }
  1409.  
  1410.  
  1411.  
  1412. }
  1413.  
  1414.  
  1415.  
  1416. }
  1417. | ExprNew DIV ExprNew {if(flag == 0){$$ = insertNode("Div"); insert_child($$, $1); insert_child($$, $3);}
  1418.  
  1419.  
  1420.  
  1421. if(imprimir3==1){
  1422.  
  1423. int erro_aux = 0;
  1424. Table_node *aux;
  1425. Table_node *aux2;
  1426.  
  1427.  
  1428. char s[5] = " - ";
  1429. char *token;
  1430. if(strcmp($1->type, "Id") == 0){
  1431. aux = find_symbol($$->child->value);
  1432.  
  1433. }else{
  1434.  
  1435. aux = insert_value_node_Table("", "", "", "");
  1436. token = strtok($$->child->notacao, s);
  1437. char var2[100];
  1438. strcpy(var2, token);
  1439. aux->type = (char *)malloc(sizeof(var2)+1);;
  1440. strcpy(aux->type, var2);
  1441.  
  1442.  
  1443. }
  1444. if(strcmp($3->type, "Id") == 0){
  1445.  
  1446. aux2 = find_symbol($$->child->brother->value);
  1447. }
  1448. else{
  1449. aux2 = insert_value_node_Table("", "", "", "");
  1450. token = strtok($$->child->brother->notacao, s);
  1451. char var[100];
  1452. strcpy(var, token);
  1453. aux2->type = (char *)malloc(sizeof(var)+1);
  1454.  
  1455. strcpy(aux2->type, var);
  1456.  
  1457.  
  1458.  
  1459. }
  1460. if(strcmp(aux->type, "") == 0){
  1461.  
  1462. //printf("\n-----ERRO!-----\n");
  1463. erro_aux = 1;
  1464.  
  1465. }
  1466. if(strcmp(aux2->type, "")==0){
  1467.  
  1468. //printf("\n-----ERRO!-----\n");
  1469. erro_aux =1;
  1470. }
  1471.  
  1472. if(erro_aux ==0){
  1473. char aux_string[100];
  1474.  
  1475. if(strcmp(aux->type, "String") ==0 || strcmp(aux2->type, "String")==0){
  1476. //printf("\n-----ERRO!-----\n");
  1477.  
  1478. }else if((strcmp(aux->type, "double") ==0 || strcmp(aux2->type, "double")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1479.  
  1480.  
  1481.  
  1482. strcpy(aux_string, " - ");
  1483. strcat(aux_string, "double");
  1484. strcat(aux_string, "\0");
  1485. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1486. strcpy($$->notacao, aux_string);
  1487.  
  1488. }else if((strcmp(aux->type, "int") ==0 || strcmp(aux2->type, "int")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1489. strcpy(aux_string, " - ");
  1490. strcat(aux_string, "int");
  1491. strcat(aux_string, "\0");
  1492. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1493. strcpy($$->notacao, aux_string);
  1494.  
  1495.  
  1496. }else{
  1497.  
  1498. //printf("\n-----ERRO!-----\n");
  1499.  
  1500.  
  1501. }
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509. }
  1510.  
  1511.  
  1512.  
  1513. }
  1514.  
  1515.  
  1516.  
  1517. }
  1518. | ExprNew MOD ExprNew {if(flag == 0){$$ = insertNode("Mod"); insert_child($$, $1); insert_child($$, $3);}
  1519. if(imprimir3==1){
  1520.  
  1521. int erro_aux = 0;
  1522. Table_node *aux;
  1523. Table_node *aux2;
  1524.  
  1525.  
  1526. char s[5] = " - ";
  1527. char *token;
  1528. if(strcmp($1->type, "Id") == 0){
  1529. aux = find_symbol($$->child->value);
  1530.  
  1531. }else{
  1532.  
  1533. aux = insert_value_node_Table("", "", "", "");
  1534. token = strtok($$->child->notacao, s);
  1535. char var2[100];
  1536. strcpy(var2, token);
  1537. aux->type = (char *)malloc(sizeof(var2)+1);;
  1538. strcpy(aux->type, var2);
  1539.  
  1540.  
  1541. }
  1542. if(strcmp($3->type, "Id") == 0){
  1543.  
  1544. aux2 = find_symbol($$->child->brother->value);
  1545. }
  1546. else{
  1547. aux2 = insert_value_node_Table("", "", "", "");
  1548. token = strtok($$->child->brother->notacao, s);
  1549. char var[100];
  1550. strcpy(var, token);
  1551. aux2->type = (char *)malloc(sizeof(var)+1);
  1552.  
  1553. strcpy(aux2->type, var);
  1554.  
  1555.  
  1556.  
  1557. }
  1558. if(strcmp(aux->type, "") == 0){
  1559.  
  1560. //printf("\n-----ERRO!-----\n");
  1561. erro_aux = 1;
  1562.  
  1563. }
  1564. if(strcmp(aux2->type, "")==0){
  1565.  
  1566. //printf("\n-----ERRO!-----\n");
  1567. erro_aux =1;
  1568. }
  1569.  
  1570. if(erro_aux ==0){
  1571. char aux_string[100];
  1572.  
  1573. if(strcmp(aux->type, "String") ==0 || strcmp(aux2->type, "String")==0){
  1574. //printf("\n-----ERRO!-----\n");
  1575.  
  1576. }else if((strcmp(aux->type, "double") ==0 || strcmp(aux2->type, "double")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1577.  
  1578.  
  1579.  
  1580. strcpy(aux_string, " - ");
  1581. strcat(aux_string, "double");
  1582. strcat(aux_string, "\0");
  1583. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1584. strcpy($$->notacao, aux_string);
  1585.  
  1586. }else if((strcmp(aux->type, "int") ==0 || strcmp(aux2->type, "int")==0) && (strcmp(aux->type, "boolean") !=0 || strcmp(aux2->type, "boolean")!=0)){
  1587. strcpy(aux_string, " - ");
  1588. strcat(aux_string, "int");
  1589. strcat(aux_string, "\0");
  1590. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1591. strcpy($$->notacao, aux_string);
  1592.  
  1593.  
  1594. }else{
  1595.  
  1596. //printf("\n-----ERRO!-----\n");
  1597.  
  1598.  
  1599. }
  1600.  
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607. }
  1608.  
  1609.  
  1610.  
  1611. }
  1612.  
  1613.  
  1614.  
  1615. }
  1616. | PLUS ExprNew %prec NOT {if(flag == 0){$$ = insertNode("Plus"); insert_child($$, $2);}
  1617. if(imprimir3==1){
  1618.  
  1619. //int erro_aux = 0;
  1620. Table_node *aux;
  1621.  
  1622.  
  1623.  
  1624. char s[5] = " - ";
  1625. char *token;
  1626. if(strcmp($2->type, "Id") == 0){
  1627. aux = find_symbol($$->child->value);
  1628.  
  1629. }else{
  1630.  
  1631. aux = insert_value_node_Table("", "", "", "");
  1632. token = strtok($$->child->notacao, s);
  1633. char var[100];
  1634. strcpy(var, token);
  1635. aux->type = (char *)malloc(sizeof(var)+1);;
  1636. strcpy(aux->type,var);
  1637.  
  1638.  
  1639. }
  1640.  
  1641.  
  1642.  
  1643.  
  1644. if(strcmp(aux->type, "") == 0){
  1645.  
  1646. //printf("\n-----ERRO!-----\n");
  1647.  
  1648.  
  1649. }else{
  1650.  
  1651. if(strcmp(aux->type, "boolean")==0 || strcmp(aux->type, "String")==0){
  1652. //printf("\n-----ERRO!-----\n");
  1653. }else{
  1654. char aux_string[100];
  1655. strcpy(aux_string, " - ");
  1656. strcat(aux_string, aux->type);
  1657. strcat(aux_string, "\0");
  1658.  
  1659.  
  1660. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1661. strcpy($$->notacao, aux_string);
  1662. }
  1663.  
  1664. }
  1665.  
  1666.  
  1667. }
  1668.  
  1669.  
  1670. }
  1671. | MINUS ExprNew %prec NOT {if(flag == 0){$$ = insertNode("Minus"); insert_child($$, $2);}
  1672. if(imprimir3==1){
  1673.  
  1674. //int erro_aux = 0;
  1675. Table_node *aux;
  1676.  
  1677.  
  1678.  
  1679. char s[5] = " - ";
  1680. char *token;
  1681. if(strcmp($2->type, "Id") == 0){
  1682. aux = find_symbol($$->child->value);
  1683.  
  1684. }else{
  1685.  
  1686. aux = insert_value_node_Table("", "", "", "");
  1687. token = strtok($$->child->notacao, s);
  1688. char var[100];
  1689. strcpy(var, token);
  1690. aux->type = (char *)malloc(sizeof(var)+1);;
  1691. strcpy(aux->type, var);
  1692.  
  1693.  
  1694. }
  1695.  
  1696.  
  1697.  
  1698.  
  1699. if(strcmp(aux->type, "") == 0 || strcmp(aux->type, "String")==0){
  1700.  
  1701. //printf("\n-----ERRO!-----\n");
  1702.  
  1703.  
  1704. }else{
  1705. if(strcmp(aux->type, "boolean")==0){
  1706. //printf("\n-----ERRO!-----\n");
  1707. }else{
  1708.  
  1709. char aux_string[100];
  1710. strcpy(aux_string, " - ");
  1711. strcat(aux_string, aux->type);
  1712. strcat(aux_string, "\0");
  1713.  
  1714.  
  1715. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1716. strcpy($$->notacao, aux_string);
  1717. }
  1718.  
  1719. }
  1720.  
  1721.  
  1722. }
  1723.  
  1724.  
  1725.  
  1726.  
  1727. }
  1728. | NOT ExprNew {if(flag == 0){$$ = insertNode("Not"); insert_child($$, $2);}
  1729. if(imprimir3==1){
  1730. //int erro_aux = 0;
  1731. Table_node *aux;
  1732.  
  1733.  
  1734.  
  1735. char s[5] = " - ";
  1736. char *token;
  1737. if(strcmp($2->type, "Id") == 0){
  1738. aux = find_symbol($$->child->value);
  1739.  
  1740. }else{
  1741.  
  1742. aux = insert_value_node_Table("", "", "", "");
  1743. token = strtok($$->child->notacao, s);
  1744. char var[100];
  1745. strcpy(var, token);
  1746. aux->type = (char *)malloc(sizeof(var)+1);
  1747. strcpy(aux->type, var);
  1748.  
  1749.  
  1750. }
  1751.  
  1752.  
  1753. if(strcmp(aux->type, "") == 0){
  1754.  
  1755. //printf("\n-----ERRO!-----\n");
  1756.  
  1757.  
  1758. }else{
  1759. if(strcmp(aux->type, "boolean")!=0){
  1760. //printf("\n-----ERRO!-----\n");
  1761. }else{
  1762.  
  1763. char aux_string[100];
  1764. strcpy(aux_string, " - ");
  1765. strcat(aux_string, aux->type);
  1766. strcat(aux_string, "\0");
  1767.  
  1768.  
  1769. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1770. strcpy($$->notacao, aux_string);
  1771. }
  1772.  
  1773. }
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779. }
  1780. }
  1781. | ID DOTLENGTH {if(flag == 0){$$ = insertNode("Length"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);}
  1782. if(imprimir3==1){
  1783. Table_node *aux = find_symbol($1);
  1784.  
  1785. if(strcmp(aux->type, "") == 0){
  1786.  
  1787. //printf("\n-----ERRO!-----\n");
  1788. if(strcmp(check_later_vars->type, "") == 0){
  1789.  
  1790. check_later_vars = $$;
  1791.  
  1792.  
  1793.  
  1794. }else{
  1795. insert_next(check_later_vars, $$);
  1796.  
  1797.  
  1798. }
  1799.  
  1800.  
  1801. }else{
  1802.  
  1803.  
  1804. char aux_string[100];
  1805. strcpy(aux_string, " - ");
  1806. strcat(aux_string, aux->type);
  1807. strcat(aux_string, "\0");
  1808.  
  1809.  
  1810. $$->child->notacao=(char *)malloc(sizeof(aux_string)+1);
  1811.  
  1812. strcpy($$->child->notacao, aux_string);
  1813.  
  1814.  
  1815.  
  1816.  
  1817.  
  1818. }
  1819.  
  1820.  
  1821. $$->notacao=(char *)malloc(sizeof(" - int"));strcpy($$->notacao, " - int");;}
  1822. }
  1823. | ID {if(flag == 0){$$ = insert_value_node("Id", $1);}
  1824. if(imprimir3==1){
  1825. Table_node *aux = find_symbol($1);
  1826.  
  1827. if(strcmp(aux->type, "") == 0){
  1828.  
  1829. //printf("\n-----ERRO!-----\n");
  1830. if(strcmp(check_later_vars->type, "") == 0){
  1831.  
  1832. check_later_vars = $$;
  1833.  
  1834.  
  1835.  
  1836. }else{
  1837. insert_next(check_later_vars, $$);
  1838.  
  1839.  
  1840. }
  1841.  
  1842. }else{
  1843.  
  1844.  
  1845. char aux_string[100];
  1846. strcpy(aux_string, " - ");
  1847. strcat(aux_string, aux->type);
  1848. strcat(aux_string, "\0");
  1849.  
  1850.  
  1851. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1852. strcpy($$->notacao, aux_string);
  1853.  
  1854.  
  1855. }
  1856.  
  1857.  
  1858. }
  1859. }
  1860. | OCURV Expr CCURV {if(flag == 0){$$ = $2;}}
  1861. | BOOLLIT {if(flag == 0){$$ = insert_value_node("BoolLit", $1);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - boolean"));strcpy($$->notacao, " - boolean");}}
  1862. | DECLIT {if(flag == 0){$$ = insert_value_node("DecLit", $1);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - int"));strcpy($$->notacao, " - int");}}
  1863. | REALLIT {if(flag == 0){$$ = insert_value_node("RealLit", $1);}if(imprimir3==1){$$->notacao=(char *)malloc(sizeof(" - double"));strcpy($$->notacao, " - double");}}
  1864. | OCURV error CCURV {if(flag == 0){$$ = NULL;}}
  1865. ;
  1866.  
  1867. Assignment: ID ASSIGN Expr {if(flag == 0){$$ = insertNode("Assign"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux); insert_child($$, $3);}
  1868. if(imprimir3==1){
  1869.  
  1870. Table_node *aux = find_symbol($1);
  1871.  
  1872. if(strcmp(aux->type, "") == 0){
  1873.  
  1874. //printf("\n-----ERRO!-----\n");
  1875. if(strcmp(check_later_vars->type, "") == 0){
  1876.  
  1877. check_later_vars = $$;
  1878.  
  1879.  
  1880.  
  1881. }else{
  1882. insert_next(check_later_vars, $$);
  1883.  
  1884.  
  1885. }
  1886.  
  1887.  
  1888.  
  1889. }else{
  1890.  
  1891.  
  1892. char aux_string[100];
  1893.  
  1894. strcpy(aux_string, " - ");
  1895.  
  1896. strcat(aux_string, aux->type);
  1897. strcat(aux_string, "\0");
  1898.  
  1899.  
  1900. $$->child->notacao=(char *)malloc(sizeof(aux_string)+1);
  1901.  
  1902. strcpy($$->child->notacao, aux_string);
  1903.  
  1904. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1905. strcpy($$->notacao,aux_string);
  1906.  
  1907. }
  1908.  
  1909. }
  1910.  
  1911.  
  1912. }
  1913. ;
  1914.  
  1915.  
  1916. MethodInvocation: ID OCURV CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);}
  1917. if(imprimir3==1){
  1918. Table_node *aux = find_symbol_Method($1, "()");
  1919.  
  1920. if(strcmp(aux->type, "") == 0){
  1921.  
  1922. //printf("\n-----ERRO!-----\n");
  1923. if(strcmp(check_later->type, "") == 0){
  1924. check_later = $$;
  1925.  
  1926. }else{
  1927. insert_next(check_later, $$);
  1928.  
  1929. }
  1930.  
  1931.  
  1932. }else{
  1933.  
  1934.  
  1935. char aux_string[100];
  1936. strcpy(aux_string, " - ");
  1937. strcat(aux_string, aux->type);
  1938. strcat(aux_string, "\0");
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1945. strcpy($$->notacao, aux_string);
  1946.  
  1947.  
  1948. char aux_string2[100];
  1949. strcpy(aux_string2, " - ");
  1950. strcat(aux_string2, aux->paramsType);
  1951. strcat(aux_string2, "\0");
  1952.  
  1953.  
  1954.  
  1955. $$->child->notacao=(char *)malloc(sizeof(aux_string2)+1);
  1956. strcpy($$->child->notacao, aux_string2);
  1957.  
  1958. }
  1959.  
  1960.  
  1961. }
  1962.  
  1963.  
  1964.  
  1965.  
  1966.  
  1967. }
  1968. | ID OCURV Expr CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux); insert_child($$, $3);}
  1969. if(imprimir3==1){
  1970. Table_node *aux;
  1971. if(strcmp($3->type, "Id") == 0){
  1972. Table_node *aux_type = find_symbol($3->value);
  1973. if(strcmp(aux_type->type, "") != 0){
  1974. char type[20];
  1975. strcpy(type, "(");
  1976. strcat(type, aux_type->type);
  1977. strcat(type, ")\0");
  1978. aux = find_symbol_Method($1, type);
  1979. }else{
  1980.  
  1981. aux =insert_value_node_Table("", "", "", "");
  1982. }
  1983.  
  1984.  
  1985.  
  1986.  
  1987. }else{
  1988. if(strcmp($3->notacao, "") != 0){
  1989. char s[5] = " - ";
  1990. char *token;
  1991. token = strtok($3->notacao, s);
  1992. char type[20];
  1993. strcpy(type, "(");
  1994. strcat(type, token);
  1995. strcat(type, ")\0");
  1996. aux = find_symbol_Method($1, type);
  1997.  
  1998. }else{
  1999.  
  2000. aux =insert_value_node_Table("", "", "", "");
  2001.  
  2002. }
  2003. }
  2004.  
  2005.  
  2006.  
  2007.  
  2008.  
  2009. if(strcmp(aux->type, "") == 0){
  2010.  
  2011. //printf("\n-----ERRO!-----\n");
  2012. if(strcmp(check_later->type, "") == 0){
  2013. check_later = $$;
  2014.  
  2015. }else{
  2016. insert_next(check_later, $$);
  2017.  
  2018. }
  2019.  
  2020. }else{
  2021.  
  2022.  
  2023. char aux_string[100];
  2024. strcpy(aux_string, " - ");
  2025. strcat(aux_string, aux->type);
  2026. strcat(aux_string, "\0");
  2027.  
  2028.  
  2029.  
  2030.  
  2031.  
  2032. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  2033. strcpy($$->notacao, aux_string);
  2034.  
  2035.  
  2036. char aux_string2[100];
  2037. strcpy(aux_string2, " - ");
  2038. strcat(aux_string2, aux->paramsType);
  2039. strcat(aux_string2, "\0");
  2040.  
  2041.  
  2042.  
  2043. $$->child->notacao=(char *)malloc(sizeof(aux_string2)+1);
  2044. strcpy($$->child->notacao, aux_string2);
  2045.  
  2046. }
  2047.  
  2048.  
  2049. }
  2050.  
  2051. }
  2052. | ID OCURV Expr CommaExpr CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);insert_child($$, $3); insert_child($$, $4);}
  2053. if(imprimir3==1){
  2054. Table_node *aux ;
  2055. int flag=0;
  2056. if(strcmp($3->type, "Id") == 0){
  2057. Table_node *aux_type = find_symbol($3->value);
  2058. if(strcmp(aux_type->type, "") != 0){
  2059. char type[20];
  2060. strcpy(type, "(");
  2061. strcat(type, aux_type->type);
  2062. AST_Tree_node *commaexpr = $4;
  2063. while(commaexpr != NULL){
  2064.  
  2065. if(strcmp(commaexpr->type, "Id") == 0){
  2066. Table_node *aux_type2 = find_symbol(commaexpr->value);
  2067. if(strcmp(aux_type2->type, "")!= 0){
  2068. strcat(type, aux_type2->type);
  2069.  
  2070. }else{
  2071. flag =1;
  2072. break;
  2073. }
  2074. }else{
  2075. if(strcmp(commaexpr->notacao, "") != 0){
  2076. char s[5] = " - ";
  2077. char *token;
  2078. token = strtok(commaexpr->notacao, s);
  2079. strcat(type, token);
  2080.  
  2081. }else{
  2082.  
  2083. flag = 1;
  2084. break;
  2085. }
  2086. }
  2087. commaexpr = commaexpr->brother;
  2088. }
  2089. strcat(type, ")\0");
  2090. if(flag == 0){
  2091.  
  2092. aux = find_symbol_Method($1, type);
  2093. }else{
  2094.  
  2095. aux = find_symbol_Method($1, type);
  2096. }
  2097.  
  2098.  
  2099. }else{
  2100.  
  2101. aux =insert_value_node_Table("", "", "", "");
  2102. }
  2103.  
  2104.  
  2105.  
  2106.  
  2107. }else{
  2108. if(strcmp($3->notacao, "") != 0){
  2109. char s[5] = " - ";
  2110. char *token;
  2111. token = strtok($3->notacao, s);
  2112. char type[20];
  2113. strcpy(type, "(");
  2114. strcat(type, token);
  2115. AST_Tree_node *commaexpr = $4;
  2116. while(commaexpr != NULL){
  2117.  
  2118. if(strcmp(commaexpr->type, "Id") == 0){
  2119. Table_node *aux_type2 = find_symbol(commaexpr->value);
  2120. if(strcmp(aux_type2->type, "")!= 0){
  2121. strcat(type, aux_type2->type);
  2122.  
  2123. }else{
  2124. flag =1;
  2125. break;
  2126. }
  2127. }else{
  2128. if(strcmp(commaexpr->notacao, "") != 0){
  2129. char s[5] = " - ";
  2130. char *token;
  2131. token = strtok(commaexpr->notacao, s);
  2132. strcat(type, token);
  2133.  
  2134. }else{
  2135.  
  2136. flag = 1;
  2137. break;
  2138. }
  2139. }
  2140. commaexpr = commaexpr->brother;
  2141. }
  2142. strcat(type, ")\0");
  2143. if(flag == 0){
  2144.  
  2145. aux = find_symbol_Method($1, type);
  2146. }else{
  2147.  
  2148. aux = find_symbol_Method($1, type);
  2149. }
  2150.  
  2151.  
  2152. }else{
  2153.  
  2154. aux =insert_value_node_Table("", "", "", "");
  2155.  
  2156. }
  2157. }
  2158.  
  2159. if(strcmp(aux->type, "") == 0){
  2160.  
  2161. //printf("\n-----ERRO!-----\n");
  2162.  
  2163. if(strcmp(check_later->type, "") == 0){
  2164. check_later = $$;
  2165.  
  2166. }else{
  2167. insert_next(check_later, $$);
  2168.  
  2169. }
  2170. }else{
  2171.  
  2172.  
  2173. char aux_string[100];
  2174. strcpy(aux_string, " - ");
  2175. strcat(aux_string, aux->type);
  2176. strcat(aux_string, "\0");
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  2183. strcpy($$->notacao, aux_string);
  2184.  
  2185.  
  2186. char aux_string2[100];
  2187. strcpy(aux_string2, " - ");
  2188. strcat(aux_string2, aux->paramsType);
  2189. strcat(aux_string2, "\0");
  2190.  
  2191.  
  2192.  
  2193. $$->child->notacao=(char *)malloc(sizeof(aux_string2)+1);
  2194. strcpy($$->child->notacao, aux_string2);
  2195.  
  2196. }
  2197.  
  2198.  
  2199. }
  2200.  
  2201.  
  2202.  
  2203. }
  2204. | ID OCURV error CCURV {flag = 1;$$=NULL;}
  2205. ;
  2206.  
  2207. CommaExpr: COMMA Expr {if(flag == 0){$$ = $2;}}
  2208. | CommaExpr COMMA Expr {if(flag == 0){$$ = $1; insert_brother($$, $3);}}
  2209. ;
  2210.  
  2211. ParseArgs: PARSEINT OCURV ID OSQUARE Expr CSQUARE CCURV {if(flag == 0){$$ = insertNode("ParseArgs"); nodeAux = insert_value_node("Id", $3); insert_child($$, nodeAux); insert_child($$, $5);}
  2212. if(imprimir3==1){
  2213.  
  2214. Table_node *aux = find_symbol($3);
  2215.  
  2216. if(strcmp(aux->type, "") == 0){
  2217.  
  2218. //printf("\n-----ERRO!-----\n");
  2219.  
  2220.  
  2221. }else{
  2222.  
  2223.  
  2224. char aux_string[100];
  2225.  
  2226. strcpy(aux_string, " - ");
  2227. strcat(aux_string, aux->type);
  2228. strcat(aux_string, "\0");
  2229.  
  2230.  
  2231. $$->child->notacao=(char *)malloc(sizeof(aux_string)+1);
  2232. strcpy($$->child->notacao, aux_string);
  2233.  
  2234.  
  2235. }
  2236.  
  2237.  
  2238. }
  2239.  
  2240.  
  2241.  
  2242. }
  2243. | PARSEINT OCURV error CCURV {flag =1;$$=NULL;}
  2244. ;
  2245.  
  2246.  
  2247.  
  2248. %%
  2249.  
  2250. void yyerror(const char* s){
  2251. if(flag ==0){
  2252. flag = 1;
  2253. }
  2254. printf("Line %d, col %d: %s: %s\n",n_linha,(int)(n_coluna - strlen(yytext)),s,yytext);
  2255. }
  2256.  
  2257. void insert_next(AST_Tree_node* first, AST_Tree_node *node){
  2258.  
  2259. AST_Tree_node* aux = first;
  2260.  
  2261. if(aux!=NULL && node!=NULL)
  2262. {
  2263. while(aux->next != NULL)
  2264. {
  2265. aux = aux->next;
  2266. }
  2267. aux->next = node;
  2268. }
  2269.  
  2270.  
  2271. }
  2272.  
  2273. void insert_brother(AST_Tree_node* brother, AST_Tree_node* node)
  2274. {
  2275.  
  2276.  
  2277. AST_Tree_node* aux = brother;
  2278.  
  2279. if(aux!=NULL && node!=NULL)
  2280. {
  2281. while(aux->brother != NULL)
  2282. {
  2283. aux = aux->brother;
  2284. }
  2285. aux->brother = node;
  2286. }
  2287. }
  2288.  
  2289. void insert_brother_Table(Table_node* brother, Table_node* node)
  2290. {
  2291.  
  2292.  
  2293. Table_node* aux = brother;
  2294.  
  2295. if(aux!=NULL && node!=NULL)
  2296. {
  2297. while(aux->brother != NULL)
  2298. {
  2299. aux = aux->brother;
  2300. }
  2301. aux->brother = node;
  2302. }
  2303. }
  2304.  
  2305.  
  2306. void insert_child(AST_Tree_node* father, AST_Tree_node* child)
  2307. {
  2308.  
  2309. AST_Tree_node* temp = father->child;
  2310.  
  2311. if(temp==NULL)
  2312. {
  2313. father->child = child;
  2314. }
  2315. else
  2316. {
  2317.  
  2318. insert_brother(father->child, child);
  2319. }
  2320.  
  2321. }
  2322.  
  2323. void insert_child_Table(Table_node* father, Table_node* child)
  2324. {
  2325.  
  2326. Table_node* temp = father->child;
  2327.  
  2328. if(temp==NULL)
  2329. {
  2330. father->child = child;
  2331. }
  2332. else
  2333. {
  2334.  
  2335. insert_brother_Table(father->child, child);
  2336. }
  2337.  
  2338. }
  2339.  
  2340.  
  2341. AST_Tree_node* insert_value_node(char* node, char* value){
  2342.  
  2343. AST_Tree_node* new_node = insertNode(node);
  2344. new_node->value = value;
  2345. return new_node;
  2346.  
  2347. }
  2348.  
  2349. Table_node* insert_value_node_Table(char* name, char* type, char* paramsType, char* flag){
  2350.  
  2351. Table_node* new_node = insertNode_Table(name);
  2352. new_node->type = type;
  2353. new_node->paramsType = paramsType;
  2354. new_node->flag=flag;
  2355. return new_node;
  2356.  
  2357. }
  2358.  
  2359.  
  2360.  
  2361. AST_Tree_node* insertNode(char* node){
  2362. AST_Tree_node* new_node;
  2363. new_node = (AST_Tree_node *)(malloc(sizeof(AST_Tree_node)));
  2364. if(new_node != NULL){
  2365. new_node->child = NULL;
  2366. new_node->brother = NULL;
  2367. new_node->value = NULL;
  2368. new_node->type = node;
  2369. new_node->notacao=NULL;
  2370. new_node->next = NULL;
  2371. }
  2372. return new_node;
  2373. }
  2374.  
  2375. Table_node* insertNode_Table(char* node){
  2376. Table_node* new_node;
  2377. new_node = (Table_node *)(malloc(sizeof(Table_node)));
  2378. if(new_node != NULL){
  2379.  
  2380. new_node->child = NULL;
  2381. new_node->brother = NULL;
  2382. new_node->name=node;
  2383. new_node->type=NULL;
  2384. new_node->paramsType=NULL;
  2385. new_node->flag=NULL;
  2386.  
  2387. }
  2388. return new_node;
  2389. }
  2390.  
  2391.  
  2392.  
  2393.  
  2394. void printTree(AST_Tree_node *node, int level){
  2395. printPoints(level);
  2396.  
  2397. if(strcmp(node->type, "Id") == 0 || strcmp(node->type, "BoolLit") == 0 || strcmp(node->type, "DecLit") == 0 || strcmp(node->type, "RealLit") == 0 || strcmp(node->type, "StrLit") == 0 ){
  2398. printTerminal(node);
  2399. }
  2400. else{
  2401. if(imprimir3==1 && node->notacao!=NULL){
  2402. if(strcmp(node->notacao,"")!=0){
  2403.  
  2404.  
  2405. printf("%s%s\n", node->type,node->notacao);
  2406. }
  2407. }else{
  2408.  
  2409. printf("%s\n", node->type);
  2410. }
  2411.  
  2412.  
  2413. }
  2414.  
  2415. AST_Tree_node *child = node->child;
  2416.  
  2417. if(child != NULL){
  2418. printTree(child, level+1);
  2419.  
  2420. while(child->brother != NULL){
  2421. child = child->brother;
  2422. printTree(child, level+1);
  2423. }
  2424.  
  2425. }
  2426.  
  2427. }
  2428.  
  2429.  
  2430. void printTerminal(AST_Tree_node *node){
  2431. if(imprimir3==1 && node->notacao!=NULL){
  2432.  
  2433. printf("%s(%s)%s\n", node->type, node->value, node->notacao);
  2434.  
  2435. }else{
  2436.  
  2437. printf("%s(%s)\n", node->type, node->value);
  2438. }
  2439. }
  2440. void printPoints(int n){
  2441. while(n > 0){
  2442. printf("..");
  2443. n--;
  2444. }
  2445. }
  2446.  
  2447.  
  2448. void printTable(Table_node *node){
  2449.  
  2450. int count =0;
  2451. int flag2 = 0;
  2452.  
  2453. printf("===== %s %s Symbol Table =====\n", node->type, node->name);
  2454. if(node->child !=NULL){
  2455. Table_node *aux = node->child;
  2456. while(flag2 !=1){
  2457.  
  2458.  
  2459.  
  2460. printf("%s\t%s\t%s\n", aux->name, aux->paramsType, aux->type);
  2461.  
  2462.  
  2463. if(aux->child != NULL){
  2464. count++;
  2465. }
  2466. if(aux->brother != NULL)
  2467. {
  2468. aux = aux->brother;
  2469. }else{
  2470. flag2 =1 ;
  2471.  
  2472. }
  2473. }
  2474. }
  2475. printf("\n");
  2476. if( count != 0){
  2477. Table_node *aux2 = node->child;
  2478.  
  2479. while(count != 0){
  2480.  
  2481. if(aux2->child != NULL){
  2482.  
  2483. count--;
  2484. Table_node *aux3 = aux2->child;
  2485. printf("===== Method %s%s Symbol Table =====\n", aux2->name, aux2->paramsType);
  2486. while(aux3 != NULL){
  2487.  
  2488.  
  2489. if(strcmp(aux3->flag, "") == 0){
  2490.  
  2491. printf("%s\t%s\t%s\n", aux3->name, aux3->paramsType, aux3->type);
  2492.  
  2493. }else{
  2494.  
  2495. printf("%s\t%s\t%s\t%s\n", aux3->name, aux3->paramsType, aux3->type, aux3->flag);
  2496. }
  2497.  
  2498. aux3 = aux3->brother;
  2499. if(aux3==NULL){
  2500. printf("\n");
  2501.  
  2502. }
  2503. }
  2504.  
  2505.  
  2506.  
  2507. }
  2508.  
  2509. aux2 = aux2->brother;
  2510.  
  2511. }
  2512. }
  2513.  
  2514.  
  2515. }
  2516.  
  2517.  
  2518.  
  2519. Table_node *check_later_vars_func(char *name){
  2520.  
  2521.  
  2522. if(head_table->child != NULL){
  2523.  
  2524. Table_node* child_class = head_table->child;
  2525. while(child_class !=NULL){
  2526. //printf("\nfuncao: %s ; paramsType:%s; type:%s\n", child_class->name, child_class->paramsType, child_class->type);
  2527.  
  2528. if(strcmp(child_class->paramsType, "") == 0){
  2529. printf("\n CLASS NAME:%s ; TARGET name:%s \n", child_class->name, name);
  2530. if(strcmp(child_class->name, name) == 0){
  2531. printf("\n RETURNS CLASS NAME:%s ; TARGET name:%s \n", child_class->name, name);
  2532. return child_class;
  2533.  
  2534. }
  2535.  
  2536. }
  2537.  
  2538. child_class = child_class->brother;
  2539.  
  2540. }
  2541.  
  2542. }
  2543.  
  2544. Table_node *cannot_find_var = insert_value_node_Table("", "", "", "");
  2545. return cannot_find_var;
  2546.  
  2547.  
  2548.  
  2549.  
  2550. }
  2551.  
  2552.  
  2553.  
  2554. Table_node *check_later_func(char *name, char *type){
  2555.  
  2556.  
  2557. if(head_table->child != NULL){
  2558.  
  2559. Table_node* child_class = head_table->child;
  2560. while(child_class !=NULL){
  2561. //printf("\nfuncao: %s ; paramsType:%s; type:%s\n", child_class->name, child_class->paramsType, child_class->type);
  2562.  
  2563. if(strcmp(child_class->paramsType, type) == 0){
  2564.  
  2565. if(strcmp(child_class->name, name) == 0){
  2566. return child_class;
  2567.  
  2568. }
  2569.  
  2570. }
  2571.  
  2572. child_class = child_class->brother;
  2573.  
  2574. }
  2575.  
  2576. }
  2577.  
  2578. Table_node *cannot_find_method = insert_value_node_Table("", "", "", "");
  2579. return cannot_find_method;
  2580.  
  2581.  
  2582.  
  2583.  
  2584. }
  2585. int parse_args_func(char *paramsType, char* params){
  2586.  
  2587.  
  2588. const char s[] = "),(";
  2589. char *token_paramsType;
  2590. char *token_params;
  2591.  
  2592. token_paramsType = strtok(paramsType,s);
  2593. token_params = strtok(params, s);
  2594. while( token_paramsType != NULL && token_params != NULL)
  2595. {
  2596. token_paramsType = strtok(NULL, s);
  2597. token_params = strtok(NULL,s);
  2598.  
  2599.  
  2600.  
  2601. }
  2602. return 0;
  2603.  
  2604. }
  2605. Table_node* find_symbol_Method(char *name, char *params){
  2606.  
  2607. if(head_table->child != NULL){
  2608.  
  2609. Table_node* child_class = head_table->child;
  2610. while(child_class !=NULL){
  2611. //printf("\nfuncao: %s ; paramsType:%s; type:%s\n", child_class->name, child_class->paramsType, child_class->type);
  2612. if(strcmp(child_class->paramsType, params) == 0){
  2613.  
  2614. if(strcmp(child_class->name, name) == 0){
  2615. return child_class;
  2616.  
  2617. }
  2618.  
  2619. }else{
  2620. if(parse_args_func(child_class->paramsType, params) == 1){
  2621. if(strcmp(child_class->name, name) == 0){
  2622. return child_class;
  2623.  
  2624. }
  2625.  
  2626. }
  2627.  
  2628.  
  2629. }
  2630.  
  2631. child_class = child_class->brother;
  2632.  
  2633. }
  2634.  
  2635. }
  2636.  
  2637. Table_node *cannot_find_method = insert_value_node_Table("", "", "", "");
  2638. return cannot_find_method;
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644. }
  2645.  
  2646. Table_node* find_symbol(char* name){
  2647.  
  2648. if(head_table->child != NULL){
  2649. Table_node* child_class = head_table->child;
  2650. while(child_class !=NULL){
  2651. if(strcmp(child_class->name, name) == 0){
  2652.  
  2653. return child_class;
  2654.  
  2655.  
  2656. }
  2657.  
  2658.  
  2659.  
  2660. child_class = child_class->brother;
  2661. }
  2662. }
  2663.  
  2664. if(current_Method->child !=NULL){
  2665.  
  2666. Table_node* child_Method = current_Method->child;
  2667. while(child_Method !=NULL){
  2668.  
  2669. if(strcmp(child_Method->name, name)==0){
  2670.  
  2671. return child_Method;
  2672.  
  2673.  
  2674. }
  2675.  
  2676.  
  2677.  
  2678. child_Method = child_Method->brother;
  2679.  
  2680. }
  2681.  
  2682.  
  2683. }
  2684.  
  2685.  
  2686. Table_node *cannot_find_symbol = insert_value_node_Table("", "", "", "");
  2687. return cannot_find_symbol;
  2688.  
  2689.  
  2690.  
  2691. }
  2692.  
  2693.  
  2694.  
  2695.  
  2696.  
  2697.  
  2698.  
  2699. int main(int argc, char *argv[])
  2700. {
  2701. int imprimir2 =0;
  2702.  
  2703. head_table= insert_value_node_Table("", "Class", "", "");
  2704. varDecl= insert_value_node_Table("", "", "", "");
  2705. current_Method= insert_value_node_Table("", "", "", "");
  2706. check_later= insert_value_node("", "");
  2707. check_later->next = NULL;
  2708. check_later_vars= insert_value_node("", "");
  2709. check_later_vars->next = NULL;
  2710.  
  2711. if(argc>1){
  2712. if(strcmp(argv[1],"-l")==0){
  2713. imprimir=1;
  2714. yylex();
  2715. }
  2716. else if(strcmp(argv[1],"-1")==0){
  2717. yylex();
  2718. }
  2719. else if(strcmp(argv[1],"-2")==0){
  2720. parse=1;
  2721. yyparse();
  2722.  
  2723.  
  2724. }else if(flag ==0 && strcmp(argv[1],"-t")==0){
  2725. parse = 1;
  2726. imprimir2 = 1;
  2727. yyparse();
  2728.  
  2729. }else if(strcmp(argv[1],"-s")==0){
  2730. parse=1;
  2731. imprimir3=1;
  2732.  
  2733. yyparse();
  2734.  
  2735. }
  2736. }else{
  2737. parse = 1;
  2738. yyparse();
  2739.  
  2740.  
  2741. }
  2742. if(imprimir2 == 1 && flag == 0){
  2743.  
  2744. printTree(head, 0);
  2745.  
  2746.  
  2747. }
  2748. if(imprimir3==1 && flag==0){
  2749.  
  2750. printTable(head_table);
  2751. printTree(head,0);
  2752. }
  2753.  
  2754. return 0;
  2755. }
Add Comment
Please, Sign In to add comment