WhaleSpunk

Untitled

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