WhaleSpunk

Untitled

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