WhaleSpunk

Untitled

May 2nd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 70.40 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);}if(imprimir3==1){
  1029. $$->notacao=" - boolean";
  1030.  
  1031.  
  1032.  
  1033. }}
  1034. | ExprNew MINUS ExprNew {if(flag == 0){$$ = insertNode("Sub"); insert_child($$, $1); insert_child($$, $3);}}
  1035. | ExprNew STAR ExprNew {if(flag == 0){$$ = insertNode("Mul"); insert_child($$, $1); insert_child($$, $3);}}
  1036. | ExprNew DIV ExprNew {if(flag == 0){$$ = insertNode("Div"); insert_child($$, $1); insert_child($$, $3);}}
  1037. | ExprNew MOD ExprNew {if(flag == 0){$$ = insertNode("Mod"); insert_child($$, $1); insert_child($$, $3);}}
  1038. | PLUS ExprNew %prec NOT {if(flag == 0){$$ = insertNode("Plus"); insert_child($$, $2);}}
  1039. | MINUS ExprNew %prec NOT {if(flag == 0){$$ = insertNode("Minus"); insert_child($$, $2);}}
  1040. | NOT ExprNew {if(flag == 0){$$ = insertNode("Not"); insert_child($$, $2);}if(imprimir3==1){$$->notacao=" - boolean";}}
  1041. | ID DOTLENGTH {if(flag == 0){$$ = insertNode("Length"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);}
  1042. if(imprimir3==1){
  1043. Table_node *aux = find_symbol($1);
  1044.  
  1045. if(strcmp(aux->type, "") == 0){
  1046.  
  1047. printf("\n-----ERRO!-----\n");
  1048.  
  1049.  
  1050. }else{
  1051.  
  1052.  
  1053. char aux_string[20];
  1054. strcpy(aux_string, " - ");
  1055. strcat(aux_string, aux->type);
  1056. strcat(aux_string, "\0");
  1057.  
  1058.  
  1059. $$->child->notacao=(char *)malloc(sizeof(aux_string)+1);
  1060. strcpy($$->child->notacao, aux_string);
  1061.  
  1062.  
  1063. }
  1064.  
  1065.  
  1066. $$->notacao=" - int";}
  1067. }
  1068. | ID {if(flag == 0){$$ = insert_value_node("Id", $1);}
  1069. if(imprimir3==1){
  1070. Table_node *aux = find_symbol($1);
  1071.  
  1072. if(strcmp(aux->type, "") == 0){
  1073.  
  1074. printf("\n-----ERRO!-----\n");
  1075.  
  1076.  
  1077. }else{
  1078.  
  1079.  
  1080. char aux_string[20];
  1081. strcpy(aux_string, " - ");
  1082. strcat(aux_string, aux->type);
  1083. strcat(aux_string, "\0");
  1084.  
  1085.  
  1086. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1087. strcpy($$->notacao, aux_string);
  1088.  
  1089.  
  1090. }
  1091.  
  1092.  
  1093. }
  1094. }
  1095. | OCURV Expr CCURV {if(flag == 0){$$ = $2;}}
  1096. | BOOLLIT {if(flag == 0){$$ = insert_value_node("BoolLit", $1);}if(imprimir3==1){$$->notacao=" - boolean";}}
  1097. | DECLIT {if(flag == 0){$$ = insert_value_node("DecLit", $1);}if(imprimir3==1){$$->notacao=" - int";}}
  1098. | REALLIT {if(flag == 0){$$ = insert_value_node("RealLit", $1);}if(imprimir3==1){$$->notacao=" - double";}}
  1099. | OCURV error CCURV {if(flag == 0){$$ = NULL;}}
  1100. ;
  1101.  
  1102. Assignment: ID ASSIGN Expr {if(flag == 0){$$ = insertNode("Assign"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux); insert_child($$, $3);}
  1103. if(imprimir3==1){
  1104. Table_node *aux = find_symbol($1);
  1105.  
  1106. if(strcmp(aux->type, "") == 0){
  1107.  
  1108. printf("\n-----ERRO!-----\n");
  1109.  
  1110.  
  1111. }else{
  1112.  
  1113.  
  1114. char aux_string[20];
  1115. strcpy(aux_string, " - ");
  1116. strcat(aux_string, aux->type);
  1117. strcat(aux_string, "\0");
  1118.  
  1119.  
  1120. $$->child->notacao=(char *)malloc(sizeof(aux_string)+1);
  1121. strcpy($$->child->notacao, aux_string);
  1122.  
  1123.  
  1124. }
  1125.  
  1126.  
  1127. $$->notacao=(char *)malloc(sizeof($$->child->notacao)+1);
  1128. strcpy($$->notacao, $$->child->notacao);
  1129.  
  1130. }
  1131.  
  1132.  
  1133. }
  1134. ;
  1135.  
  1136.  
  1137. MethodInvocation: ID OCURV CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);}
  1138. if(imprimir3==1){
  1139. Table_node *aux = find_symbol_Method($1);
  1140.  
  1141. if(strcmp(aux->type, "") == 0){
  1142.  
  1143. printf("\n-----ERRO!-----\n");
  1144. if(strcmp(check_later->type, "") == 0){
  1145. check_later = $$;
  1146.  
  1147. }else{
  1148. insert_brother(check_later, $$);
  1149.  
  1150. }
  1151.  
  1152.  
  1153. }else{
  1154.  
  1155.  
  1156. char aux_string[20];
  1157. strcpy(aux_string, " - ");
  1158. strcat(aux_string, aux->type);
  1159. strcat(aux_string, "\0");
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1166. strcpy($$->notacao, aux_string);
  1167.  
  1168.  
  1169. char aux_string2[20];
  1170. strcpy(aux_string2, " - ");
  1171. strcat(aux_string2, aux->paramsType);
  1172. strcat(aux_string2, "\0");
  1173.  
  1174.  
  1175.  
  1176. $$->child->notacao=(char *)malloc(sizeof(aux_string2)+1);
  1177. strcpy($$->child->notacao, aux_string2);
  1178.  
  1179. }
  1180.  
  1181.  
  1182. }
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188. }
  1189. | ID OCURV Expr CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux); insert_child($$, $3);}
  1190. if(imprimir3==1){
  1191. Table_node *aux = find_symbol_Method($1);
  1192.  
  1193. if(strcmp(aux->type, "") == 0){
  1194.  
  1195. printf("\n-----ERRO!-----AQUI\n");
  1196. if(strcmp(check_later->type, "") == 0){
  1197. check_later = $$;
  1198.  
  1199. }else{
  1200. insert_brother(check_later, $$);
  1201.  
  1202. }
  1203.  
  1204. }else{
  1205.  
  1206.  
  1207. char aux_string[20];
  1208. strcpy(aux_string, " - ");
  1209. strcat(aux_string, aux->type);
  1210. strcat(aux_string, "\0");
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1217. strcpy($$->notacao, aux_string);
  1218.  
  1219.  
  1220. char aux_string2[20];
  1221. strcpy(aux_string2, " - ");
  1222. strcat(aux_string2, aux->paramsType);
  1223. strcat(aux_string2, "\0");
  1224.  
  1225.  
  1226.  
  1227. $$->child->notacao=(char *)malloc(sizeof(aux_string2)+1);
  1228. strcpy($$->child->notacao, aux_string2);
  1229.  
  1230. }
  1231.  
  1232.  
  1233. }
  1234.  
  1235. }
  1236. | 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);}
  1237. if(imprimir3==1){
  1238. printf("\nnome da funcao: %s", $1);
  1239. Table_node *aux = find_symbol_Method($1);
  1240.  
  1241. if(strcmp(aux->type, "") == 0){
  1242.  
  1243. printf("\n-----ERRO!-----AQUI2\n");
  1244.  
  1245. if(strcmp(check_later->type, "") == 0){
  1246. check_later = $$;
  1247.  
  1248. }else{
  1249. insert_brother(check_later, $$);
  1250.  
  1251. }
  1252. }else{
  1253.  
  1254.  
  1255. char aux_string[20];
  1256. strcpy(aux_string, " - ");
  1257. strcat(aux_string, aux->type);
  1258. strcat(aux_string, "\0");
  1259.  
  1260.  
  1261.  
  1262.  
  1263.  
  1264. $$->notacao=(char *)malloc(sizeof(aux_string)+1);
  1265. strcpy($$->notacao, aux_string);
  1266.  
  1267.  
  1268. char aux_string2[20];
  1269. strcpy(aux_string2, " - ");
  1270. strcat(aux_string2, aux->paramsType);
  1271. strcat(aux_string2, "\0");
  1272.  
  1273.  
  1274.  
  1275. $$->child->notacao=(char *)malloc(sizeof(aux_string2)+1);
  1276. strcpy($$->child->notacao, aux_string2);
  1277.  
  1278. }
  1279.  
  1280.  
  1281. }
  1282.  
  1283.  
  1284.  
  1285. }
  1286. | ID OCURV error CCURV {flag = 1;$$=NULL;}
  1287. ;
  1288.  
  1289. CommaExpr: COMMA Expr {if(flag == 0){$$ = $2;}}
  1290. | CommaExpr COMMA Expr {if(flag == 0){$$ = $1; insert_brother($$, $3);}}
  1291. ;
  1292.  
  1293. 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);}
  1294. if(imprimir3==1){
  1295. Table_node *aux = find_symbol($3);
  1296.  
  1297. if(strcmp(aux->type, "") == 0){
  1298.  
  1299. printf("\n-----ERRO!-----\n");
  1300.  
  1301.  
  1302. }else{
  1303.  
  1304.  
  1305. char aux_string[20];
  1306. strcpy(aux_string, " - ");
  1307. strcat(aux_string, aux->type);
  1308. strcat(aux_string, "\0");
  1309.  
  1310.  
  1311. $$->child->notacao=(char *)malloc(sizeof(aux_string)+1);
  1312. strcpy($$->child->notacao, aux_string);
  1313.  
  1314.  
  1315. }
  1316.  
  1317. }
  1318.  
  1319.  
  1320.  
  1321. }
  1322. | PARSEINT OCURV error CCURV {flag =1;$$=NULL;}
  1323. ;
  1324.  
  1325.  
  1326.  
  1327. %%
  1328.  
  1329. void yyerror(const char* s){
  1330. if(flag ==0){
  1331. flag = 1;
  1332. }
  1333. printf("Line %d, col %d: %s: %s\n",n_linha,(int)(n_coluna - strlen(yytext)),s,yytext);
  1334. }
  1335.  
  1336.  
  1337.  
  1338. void insert_brother(AST_Tree_node* brother, AST_Tree_node* node)
  1339. {
  1340.  
  1341.  
  1342. AST_Tree_node* aux = brother;
  1343.  
  1344. if(aux!=NULL && node!=NULL)
  1345. {
  1346. while(aux->brother != NULL)
  1347. {
  1348. aux = aux->brother;
  1349. }
  1350. aux->brother = node;
  1351. }
  1352. }
  1353.  
  1354. void insert_brother_Table(Table_node* brother, Table_node* node)
  1355. {
  1356.  
  1357.  
  1358. Table_node* aux = brother;
  1359.  
  1360. if(aux!=NULL && node!=NULL)
  1361. {
  1362. while(aux->brother != NULL)
  1363. {
  1364. aux = aux->brother;
  1365. }
  1366. aux->brother = node;
  1367. }
  1368. }
  1369.  
  1370.  
  1371. void insert_child(AST_Tree_node* father, AST_Tree_node* child)
  1372. {
  1373.  
  1374. AST_Tree_node* temp = father->child;
  1375.  
  1376. if(temp==NULL)
  1377. {
  1378. father->child = child;
  1379. }
  1380. else
  1381. {
  1382.  
  1383. insert_brother(father->child, child);
  1384. }
  1385.  
  1386. }
  1387.  
  1388. void insert_child_Table(Table_node* father, Table_node* child)
  1389. {
  1390.  
  1391. Table_node* temp = father->child;
  1392.  
  1393. if(temp==NULL)
  1394. {
  1395. father->child = child;
  1396. }
  1397. else
  1398. {
  1399.  
  1400. insert_brother_Table(father->child, child);
  1401. }
  1402.  
  1403. }
  1404.  
  1405.  
  1406. AST_Tree_node* insert_value_node(char* node, char* value){
  1407.  
  1408. AST_Tree_node* new_node = insertNode(node);
  1409. new_node->value = value;
  1410. return new_node;
  1411.  
  1412. }
  1413.  
  1414. Table_node* insert_value_node_Table(char* name, char* type, char* paramsType, char* flag){
  1415.  
  1416. Table_node* new_node = insertNode_Table(name);
  1417. new_node->type = type;
  1418. new_node->paramsType = paramsType;
  1419. new_node->flag=flag;
  1420. return new_node;
  1421.  
  1422. }
  1423.  
  1424.  
  1425.  
  1426. AST_Tree_node* insertNode(char* node){
  1427. AST_Tree_node* new_node;
  1428. new_node = (AST_Tree_node *)(malloc(sizeof(AST_Tree_node)));
  1429. if(new_node != NULL){
  1430. new_node->child = NULL;
  1431. new_node->brother = NULL;
  1432. new_node->value = NULL;
  1433. new_node->type = node;
  1434. new_node->notacao=NULL;
  1435. }
  1436. return new_node;
  1437. }
  1438.  
  1439. Table_node* insertNode_Table(char* node){
  1440. Table_node* new_node;
  1441. new_node = (Table_node *)(malloc(sizeof(Table_node)));
  1442. if(new_node != NULL){
  1443.  
  1444. new_node->child = NULL;
  1445. new_node->brother = NULL;
  1446. new_node->name=node;
  1447. new_node->type=NULL;
  1448. new_node->paramsType=NULL;
  1449. new_node->flag=NULL;
  1450.  
  1451. }
  1452. return new_node;
  1453. }
  1454.  
  1455.  
  1456.  
  1457.  
  1458. void printTree(AST_Tree_node *node, int level){
  1459. printPoints(level);
  1460.  
  1461. 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 ){
  1462. printTerminal(node);
  1463. }
  1464. else{
  1465. if(imprimir3==1 && node->notacao!=NULL){
  1466. printf("%s%s\n", node->type,node->notacao);
  1467. }else{
  1468.  
  1469. printf("%s\n", node->type);
  1470. }
  1471.  
  1472.  
  1473. }
  1474.  
  1475. AST_Tree_node *child = node->child;
  1476.  
  1477. if(child != NULL){
  1478. printTree(child, level+1);
  1479.  
  1480. while(child->brother != NULL){
  1481. child = child->brother;
  1482. printTree(child, level+1);
  1483. }
  1484.  
  1485. }
  1486.  
  1487. }
  1488.  
  1489.  
  1490. void printTerminal(AST_Tree_node *node){
  1491. if(imprimir3==1 && node->notacao!=NULL){
  1492.  
  1493. printf("%s(%s)%s\n", node->type, node->value, node->notacao);
  1494.  
  1495. }else{
  1496.  
  1497. printf("%s(%s)\n", node->type, node->value);
  1498. }
  1499. }
  1500. void printPoints(int n){
  1501. while(n > 0){
  1502. printf("..");
  1503. n--;
  1504. }
  1505. }
  1506.  
  1507.  
  1508. void printTable(Table_node *node){
  1509.  
  1510. int count =0;
  1511. int flag2 = 0;
  1512.  
  1513. printf("===== %s %s Symbol Table =====\n", node->type, node->name);
  1514. if(node->child !=NULL){
  1515. Table_node *aux = node->child;
  1516. while(flag2 !=1){
  1517.  
  1518.  
  1519.  
  1520. printf("%s\t%s\t%s\n", aux->name, aux->paramsType, aux->type);
  1521.  
  1522.  
  1523. if(aux->child != NULL){
  1524. count++;
  1525. }
  1526. if(aux->brother != NULL)
  1527. {
  1528. aux = aux->brother;
  1529. }else{
  1530. flag2 =1 ;
  1531.  
  1532. }
  1533. }
  1534. }
  1535. printf("\n");
  1536. if( count != 0){
  1537. Table_node *aux2 = node->child;
  1538.  
  1539. while(count != 0){
  1540.  
  1541. if(aux2->child != NULL){
  1542.  
  1543. count--;
  1544. Table_node *aux3 = aux2->child;
  1545. printf("===== Method %s%s Symbol Table =====\n", aux2->name, aux2->paramsType);
  1546. while(aux3 != NULL){
  1547.  
  1548.  
  1549. if(strcmp(aux3->flag, "") == 0){
  1550.  
  1551. printf("%s\t%s\t%s\n", aux3->name, aux3->paramsType, aux3->type);
  1552.  
  1553. }else{
  1554.  
  1555. printf("%s\t%s\t%s\t%s\n", aux3->name, aux3->paramsType, aux3->type, aux3->flag);
  1556. }
  1557.  
  1558. aux3 = aux3->brother;
  1559. if(aux3==NULL){
  1560. printf("\n");
  1561.  
  1562. }
  1563. }
  1564.  
  1565.  
  1566.  
  1567. }
  1568.  
  1569. aux2 = aux2->brother;
  1570.  
  1571. }
  1572. }
  1573.  
  1574.  
  1575. }
  1576. Table_node *check_later_func(char *name, char *type){
  1577.  
  1578.  
  1579. if(head_table->child != NULL){
  1580.  
  1581. Table_node* child_class = head_table->child;
  1582. while(child_class !=NULL){
  1583. //printf("\nfuncao: %s ; paramsType:%s; type:%s\n", child_class->name, child_class->paramsType, child_class->type);
  1584.  
  1585. if(strcmp(child_class->paramsType, type) == 0){
  1586.  
  1587. if(strcmp(child_class->name, name) == 0){
  1588. return child_class;
  1589.  
  1590. }
  1591.  
  1592. }
  1593.  
  1594. child_class = child_class->brother;
  1595.  
  1596. }
  1597.  
  1598. }
  1599.  
  1600. Table_node *cannot_find_method = insert_value_node_Table("", "", "", "");
  1601. return cannot_find_method;
  1602.  
  1603.  
  1604.  
  1605.  
  1606. }
  1607. Table_node* find_symbol_Method(char *name){
  1608.  
  1609. if(head_table->child != NULL){
  1610.  
  1611. Table_node* child_class = head_table->child;
  1612. while(child_class !=NULL){
  1613. //printf("\nfuncao: %s ; paramsType:%s; type:%s\n", child_class->name, child_class->paramsType, child_class->type);
  1614. if(strcmp(child_class->paramsType, "") != 0){
  1615.  
  1616. if(strcmp(child_class->name, name) == 0){
  1617. return child_class;
  1618.  
  1619. }
  1620.  
  1621. }
  1622.  
  1623. child_class = child_class->brother;
  1624.  
  1625. }
  1626.  
  1627. }
  1628.  
  1629. Table_node *cannot_find_method = insert_value_node_Table("", "", "", "");
  1630. return cannot_find_method;
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636. }
  1637.  
  1638. Table_node* find_symbol(char* name){
  1639.  
  1640. if(head_table->child != NULL){
  1641. Table_node* child_class = head_table->child;
  1642. while(child_class !=NULL){
  1643. if(strcmp(child_class->name, name) == 0){
  1644.  
  1645. return child_class;
  1646.  
  1647.  
  1648. }
  1649.  
  1650.  
  1651.  
  1652. child_class = child_class->brother;
  1653. }
  1654. }
  1655.  
  1656. if(current_Method->child !=NULL){
  1657.  
  1658. Table_node* child_Method = current_Method->child;
  1659. while(child_Method !=NULL){
  1660.  
  1661. if(strcmp(child_Method->name, name)==0){
  1662.  
  1663. return child_Method;
  1664.  
  1665.  
  1666. }
  1667.  
  1668.  
  1669.  
  1670. child_Method = child_Method->brother;
  1671.  
  1672. }
  1673.  
  1674.  
  1675. }
  1676.  
  1677.  
  1678. Table_node *cannot_find_symbol = insert_value_node_Table("", "", "", "");
  1679. return cannot_find_symbol;
  1680.  
  1681.  
  1682.  
  1683. }
  1684.  
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691. int main(int argc, char *argv[])
  1692. {
  1693. int imprimir2 =0;
  1694.  
  1695. head_table= insert_value_node_Table("", "Class", "", "");
  1696. varDecl= insert_value_node_Table("", "", "", "");
  1697. current_Method= insert_value_node_Table("", "", "", "");
  1698. check_later= insert_value_node("", "");
  1699.  
  1700. if(argc>1){
  1701. if(strcmp(argv[1],"-l")==0){
  1702. imprimir=1;
  1703. yylex();
  1704. }
  1705. else if(strcmp(argv[1],"-1")==0){
  1706. yylex();
  1707. }
  1708. else if(strcmp(argv[1],"-2")==0){
  1709. parse=1;
  1710. yyparse();
  1711.  
  1712.  
  1713. }else if(flag ==0 && strcmp(argv[1],"-t")==0){
  1714. parse = 1;
  1715. imprimir2 = 1;
  1716. yyparse();
  1717.  
  1718. }else if(strcmp(argv[1],"-s")==0){
  1719. parse=1;
  1720. imprimir3=1;
  1721. yyparse();
  1722. }
  1723. }else{
  1724. parse = 1;
  1725. yyparse();
  1726.  
  1727.  
  1728. }
  1729. if(imprimir2 == 1 && flag == 0){
  1730.  
  1731. printTree(head, 0);
  1732.  
  1733.  
  1734. }
  1735. if(imprimir3==1 && flag==0){
  1736.  
  1737. printTable(head_table);
  1738. printTree(head,0);
  1739. }
  1740.  
  1741. return 0;
  1742. }
Add Comment
Please, Sign In to add comment