WhaleSpunk

Untitled

May 1st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.79 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.  
  14. }AST_Tree_node;
  15.  
  16. typedef struct Table{
  17. char *name;
  18. char *type;
  19. char *paramsType;
  20. char *flag;
  21. struct Table* brother;
  22. struct Table* child;
  23. }Table_node;
  24.  
  25. Table_node *head_table = NULL;
  26.  
  27.  
  28. AST_Tree_node *head = NULL;
  29.  
  30. AST_Tree_node* insertNode(char* node);
  31. AST_Tree_node* insert_value_node(char* node, char* value);
  32. void insert_child(AST_Tree_node* father, AST_Tree_node* child);
  33. void insert_brother(AST_Tree_node* brother, AST_Tree_node* node);
  34. void printTree(AST_Tree_node* aux, int level);
  35. void printTerminal(AST_Tree_node *node);
  36. void printPoints(int n);
  37.  
  38. void printTable(Table_node *node);
  39. Table_node* insertNode_Table(char* node);
  40. Table_node* insert_value_node_Table(char* name, char* type, char* paramsType, char* flag);
  41. void insert_child_Table(Table_node* father, Table_node* child);
  42. void insert_brother_Table(Table_node* brother, Table_node* node);
  43.  
  44.  
  45. int parse = 0;
  46. int flag=0;
  47. int aux2 = 0;
  48. int imprimir=0;
  49. int yylex(void);
  50. void yyerror(const char *s);
  51.  
  52. extern int n_linha;
  53. extern int n_coluna;
  54. extern char * yytext;
  55. extern int yyleng;
  56.  
  57. Table_node *aux_node_table;
  58.  
  59. AST_Tree_node *nodeAux;
  60. AST_Tree_node *nodeAux2;
  61. AST_Tree_node *nodeAux3;
  62. AST_Tree_node *nodeAux4;
  63. AST_Tree_node *nodeAux5;
  64. Table_node *nodeAux_Table;
  65. Table_node *nodeAux_Table2;
  66. Table_node *nodeAux_Table3;
  67. Table_node *nodeAux_Table4;
  68.  
  69.  
  70.  
  71. Table_node *varDecl;
  72.  
  73.  
  74. %}
  75.  
  76. %union{
  77. char *str;
  78. struct AST_Tree *node_struct;
  79. }
  80.  
  81. %token <str> ID STRLIT BOOLLIT DECLIT REALLIT
  82.  
  83. %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
  84.  
  85. %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
  86.  
  87.  
  88.  
  89. %left COMMA
  90. %right ASSIGN
  91. %left OR
  92. %left AND
  93. %left EQ NEQ
  94. %left LT GT LEQ GEQ
  95. %left MINUS PLUS
  96. %left STAR DIV MOD
  97. %right NOT
  98. %left OCURV OBRACE OSQUARE CCURV CBRACE CSQUARE
  99.  
  100. %nonassoc ELSE
  101.  
  102. %%
  103.  
  104.  
  105.  
  106.  
  107. Program: CLASS ID OBRACE FieldDecl_MethodDecl_SEMI CBRACE {if(flag == 0)
  108. {$$ = head = insertNode("Program");nodeAux = insert_value_node("Id", $2); insert_child($$, nodeAux);
  109. //head_table = insert_value_node_Table($2, "Class", "", "");
  110. //printf("\nHEAD TABLE TYPE: %s\n", head_table->type);
  111. head_table->name = $2;
  112. if($4!= NULL){
  113. insert_child($$, $4);
  114.  
  115. }
  116. }
  117. }
  118. | CLASS ID OBRACE CBRACE {if(flag==0){
  119. $$ = head = insertNode("Program");
  120. nodeAux = insert_value_node("Id", $2);
  121. insert_child($$, nodeAux);head_table= insertNode_Table($2);
  122. aux_node_table=insertNode_Table($2);
  123. //head_table= insert_value_node_Table($2, "Class", "", "");
  124. head_table->name = $2;
  125. }
  126. }
  127. ;
  128.  
  129. FieldDecl_MethodDecl_SEMI: FieldDecl_MethodDecl_SEMI FieldDecl {if(flag ==0){if($1 != NULL){$$ =$1;insert_brother($$, $2);}else{$$ = $2;}}}
  130. | FieldDecl_MethodDecl_SEMI MethodDecl {if(flag == 0){if($1 != NULL){$$=$1; insert_brother($$, $2);}else{$$ = $2;}}}
  131. | FieldDecl_MethodDecl_SEMI SEMI {if(flag == 0){if($1 != NULL){$$ = $1;}else{$$ = NULL;}}}
  132. | FieldDecl {if(flag ==0 ){$$ = $1;}}
  133. | MethodDecl {if(flag == 0){$$ = $1 ;}}
  134. | SEMI {if(flag == 0){$$ = NULL;}}
  135. ;
  136.  
  137.  
  138.  
  139.  
  140.  
  141. FieldDecl: PUBLIC STATIC Type ID CommaID SEMI {if(flag ==0){
  142. int i=0;
  143. int length4 = strlen($3->type);
  144. char* lower4 = ( char* )malloc( length4 + 1 );
  145. if(strcmp($3->type, "Bool")==0){
  146. nodeAux_Table = insert_value_node_Table($4, "boolean", "", "");
  147. }else{
  148.  
  149. lower4[length4] = 0;
  150. for(i = 0; i < length4; i++ )
  151. {
  152. lower4[i] = tolower( $3->type[i] );
  153. }
  154.  
  155.  
  156. nodeAux_Table = insert_value_node_Table($4, lower4, "", "");
  157.  
  158.  
  159. }
  160.  
  161. insert_child_Table(head_table, nodeAux_Table);
  162.  
  163. $$ = insertNode("FieldDecl");
  164. insert_child($$, $3);
  165. AST_Tree_node *nodeAuxaux = insert_value_node("Id", $4);
  166. insert_child($$, nodeAuxaux);
  167. nodeAux2 = $5;
  168. AST_Tree_node *node = nodeAux2;
  169. AST_Tree_node *copy;
  170. AST_Tree_node *copy2;
  171. Table_node *copy_table;
  172. while(node != NULL){
  173.  
  174.  
  175.  
  176. if(strcmp($3->type, "Bool")==0){
  177. insert_child_Table(head_table, insert_value_node_Table(node->value, "boolean", "", ""));
  178. }else{
  179.  
  180. insert_child_Table(head_table, insert_value_node_Table(node->value, lower4, "", ""));
  181. }
  182.  
  183.  
  184.  
  185. copy = insertNode($3->type);
  186. copy2 = insert_value_node("Id", node->value);
  187. nodeAux3 = insertNode("FieldDecl");
  188. insert_brother($$, nodeAux3);
  189. insert_child(nodeAux3, copy);
  190. insert_child(nodeAux3, copy2);
  191. node = nodeAux2->brother;
  192. nodeAux2 = nodeAux2->brother;
  193. }
  194. }
  195. }
  196.  
  197. | PUBLIC STATIC Type ID SEMI {if(flag == 0){
  198. int i=0;
  199. int length7 = strlen($3->type);
  200. char* lower7 = ( char* )malloc( length7 + 1 );
  201. $$ = insertNode("FieldDecl");
  202. insert_child($$, $3);
  203. nodeAux = insert_value_node("Id", $4);
  204. insert_child($$, nodeAux);
  205.  
  206.  
  207.  
  208.  
  209. if(strcmp($3->type, "Bool")==0){
  210.  
  211. nodeAux_Table = insert_value_node_Table($4, "boolean", "", "");
  212. }else{
  213.  
  214. lower7[length7] = 0;
  215. for(i = 0; i < length7; i++ )
  216. {
  217. lower7[i] = tolower( $3->type[i] );
  218. }
  219.  
  220.  
  221. nodeAux_Table = insert_value_node_Table($4, lower7, "", "");
  222.  
  223.  
  224. }
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. insert_child_Table(head_table, nodeAux_Table);}
  234. }
  235. | error SEMI {flag = 1;$$=NULL;}
  236. ;
  237.  
  238. CommaID: CommaID COMMA ID {if(flag == 0){$$ = $1 ; nodeAux = insert_value_node("Id", $3); insert_brother($$, nodeAux);}}
  239. | COMMA ID {if(flag == 0){$$ = insert_value_node("Id", $2);}}
  240. ;
  241.  
  242. MethodDecl: PUBLIC STATIC MethodHeader MethodBody {if(flag ==0 ){
  243. int i=0;
  244. $$ = insertNode("MethodDecl");
  245. insert_child($$, $3);
  246. insert_child($$, $4);
  247.  
  248.  
  249. nodeAux_Table = insertNode_Table($3->child->brother->value);
  250.  
  251.  
  252.  
  253. nodeAux_Table->type = $3->child->type;
  254.  
  255.  
  256.  
  257.  
  258. if(strcmp(nodeAux_Table->type, "Bool")==0){
  259. nodeAux_Table->type = "boolean";
  260. insert_child_Table(nodeAux_Table, insert_value_node_Table("return", "boolean", "", ""));
  261. }else{
  262. int length2 = strlen(nodeAux_Table->type);
  263. char* lower2 = ( char* )malloc( length2 + 1 );
  264. lower2[length2] = 0;
  265. for(i = 0; i < length2; i++ )
  266. {
  267. lower2[i] = tolower( nodeAux_Table->type[i] );
  268. }
  269. nodeAux_Table->type = lower2;
  270.  
  271. insert_child_Table(nodeAux_Table, insert_value_node_Table("return", lower2,"",""));
  272. }
  273.  
  274. nodeAux4 = $3->child->brother->brother;
  275.  
  276. char param[100];
  277. strcpy(param, "(");
  278. AST_Tree_node *node = nodeAux4->child;
  279. while(node!=NULL){
  280.  
  281. if(strcmp(node->child->type, "Bool")==0){
  282. strcat(param, "boolean");
  283. insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "boolean", "", "param"));
  284. }else if(strcmp(node->child->type, "StringArray")==0){
  285. strcat(param, "String[]");
  286. insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "String[]", "", "param"));
  287.  
  288. }else{
  289.  
  290. int length = strlen(node->child->type);
  291. char* lower = ( char* )malloc( length + 1 );
  292. lower[length] = 0;
  293. for(i = 0; i < length; i++ )
  294. {
  295. lower[i] = tolower( node->child->type[i] );
  296. }
  297.  
  298.  
  299. strcat(param, lower);
  300. insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, lower, "", "param"));
  301.  
  302. }
  303.  
  304. if( node->brother != NULL){
  305. strcat(param, ", ");
  306. }
  307.  
  308.  
  309.  
  310. node = node->brother;
  311.  
  312.  
  313. }
  314. strcat(param, ")\0");
  315. nodeAux_Table->paramsType =(char *)malloc(sizeof(param)+1);
  316. strcpy(nodeAux_Table->paramsType, param);
  317. nodeAux_Table->flag = "";
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330. insert_child_Table(nodeAux_Table, varDecl);
  331. varDecl = insert_value_node_Table("", "", "", "");
  332.  
  333.  
  334.  
  335.  
  336.  
  337. insert_child_Table(head_table, nodeAux_Table);
  338.  
  339.  
  340.  
  341.  
  342.  
  343. }
  344. }
  345. ;
  346.  
  347. MethodHeader: Type ID OCURV FormalParams CCURV {if(flag == 0){
  348. $$ = insertNode("MethodHeader");
  349. insert_child($$, $1);
  350. nodeAux = insert_value_node("Id", $2);
  351. insert_child($$, nodeAux);
  352. insert_child($$, $4);
  353.  
  354.  
  355.  
  356.  
  357. }
  358. }
  359. | Type ID OCURV CCURV {if(flag == 0){$$ = insertNode("MethodHeader"); insert_child($$, $1); nodeAux = insert_value_node("Id", $2); insert_child($$, nodeAux);nodeAux3=insertNode("MethodParams"); insert_child($$,nodeAux3);}}
  360. | VOID ID OCURV FormalParams CCURV {if(flag == 0){$$ = insertNode("MethodHeader"); nodeAux = insertNode("Void"); insert_child($$, nodeAux); nodeAux2 = insert_value_node("Id", $2); insert_child($$, nodeAux2); insert_child($$, $4);}}
  361. | VOID ID OCURV CCURV {if(flag == 0){$$ = insertNode("MethodHeader"); nodeAux = insertNode("Void"); insert_child($$, nodeAux); nodeAux2 = insert_value_node("Id", $2); insert_child($$, nodeAux2);nodeAux3=insertNode("MethodParams"); insert_child($$,nodeAux3);}}
  362. ;
  363.  
  364.  
  365.  
  366.  
  367.  
  368. FormalParams: Type ID CommaTypeID {if(flag == 0){
  369. $$ = insertNode("MethodParams");
  370. nodeAux2 = insertNode("ParamDecl");
  371. insert_child($$, nodeAux2);
  372. insert_child(nodeAux2, $1);
  373. nodeAux = insert_value_node("Id", $2);
  374. insert_child(nodeAux2, nodeAux);
  375. nodeAux2 = $3;
  376. AST_Tree_node *node = nodeAux2;
  377. AST_Tree_node *copy;
  378. AST_Tree_node *copy2;
  379. while(node!=NULL){
  380.  
  381. copy = insertNode(node->type);
  382. copy2 = insert_value_node("Id", node->brother->value);
  383. nodeAux3 = insertNode("ParamDecl");
  384. insert_child($$, nodeAux3);
  385. insert_child(nodeAux3, copy);
  386. insert_child(nodeAux3, copy2);
  387. node = node->brother->brother;
  388.  
  389. }
  390.  
  391. }}
  392. | 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);}}
  393. | 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);}}
  394. ;
  395.  
  396.  
  397. CommaTypeID: COMMA Type ID {if(flag == 0){$$ = $2; nodeAux = insert_value_node("Id", $3); insert_brother($$, nodeAux);}}
  398. | CommaTypeID COMMA Type ID {if(flag ==0){$$ = $1; insert_brother($$, $3); nodeAux2 = insert_value_node("Id", $4); insert_brother($$, nodeAux2);}}
  399. ;
  400.  
  401.  
  402. MethodBody: OBRACE VarDecl_Statement CBRACE {if(flag == 0){$$ = insertNode("MethodBody"); if($2!=NULL){insert_child($$, $2);}}}
  403. | OBRACE CBRACE {if(flag == 0){$$ = insertNode("MethodBody");}}
  404. ;
  405.  
  406.  
  407. VarDecl_Statement: VarDecl {if(flag == 0){$$ = $1;}}
  408. | Statement {if(flag == 0){$$ = $1;}}
  409. | VarDecl_Statement VarDecl {if(flag == 0){if($1!=NULL){$$=$1;insert_brother($1,$2);}else{$$=$2;}}}
  410. | VarDecl_Statement Statement {if(flag == 0){if($1!=NULL){$$ = $1;if($2!=NULL){insert_brother($1,$2);}}else{$$=$2;}}}
  411.  
  412.  
  413. VarDecl: Type ID CommaID SEMI {if(flag == 0){
  414. int i = 0;
  415. int length5 = strlen($1->type);
  416. char* lower5 = ( char* )malloc( length5 + 1 );
  417. $$ = insertNode("VarDecl");
  418. insert_child($$,$1);
  419.  
  420. nodeAux = insert_value_node("Id", $2);
  421. insert_child($$, nodeAux);
  422. nodeAux2 = $3;
  423. AST_Tree_node *node = nodeAux2;
  424. AST_Tree_node *copy;
  425. AST_Tree_node *copy2;
  426.  
  427. if(strcmp($1->type, "Bool") == 0){
  428.  
  429. if(strcmp(varDecl->type, "")==0){
  430. varDecl = insert_value_node_Table($2, "boolean", "", "");
  431. }else{
  432. insert_brother_Table(varDecl, insert_value_node_Table($2, "boolean", "", ""));
  433.  
  434. }
  435.  
  436.  
  437.  
  438.  
  439. }else{
  440.  
  441. lower5[length5] = 0;
  442. for(i = 0; i < length5; i++ )
  443. {
  444. lower5[i] = tolower( $1->type[i] );
  445. }
  446.  
  447.  
  448.  
  449. if(strcmp(varDecl->type, "") == 0){
  450. varDecl = insert_value_node_Table($2, lower5, "", "");
  451. }else{
  452. insert_brother_Table(varDecl, insert_value_node_Table($2, lower5, "", ""));
  453.  
  454. }
  455.  
  456. }
  457.  
  458. while(node != NULL){
  459.  
  460. if(strcmp($1->type, "Bool") == 0){
  461.  
  462. insert_brother_Table(varDecl, insert_value_node_Table(node->value, "boolean", "", ""));
  463.  
  464. }else{
  465. insert_brother_Table(varDecl, insert_value_node_Table(node->value, lower5, "", ""));
  466.  
  467. }
  468.  
  469.  
  470. copy = insertNode($1->type);
  471. copy2 = insert_value_node("Id", node->value);
  472. nodeAux3 = insertNode("VarDecl");
  473. insert_brother($$, nodeAux3);
  474. insert_child(nodeAux3, copy);
  475. insert_child(nodeAux3, copy2);
  476. node = nodeAux2->brother;
  477. nodeAux2 = nodeAux2->brother;
  478. }}}
  479.  
  480. | Type ID SEMI {if(flag == 0){
  481. int i = 0;
  482. int length6 = strlen($1->type);
  483. char* lower6 = ( char* )malloc( length6 + 1 );
  484.  
  485.  
  486. if(strcmp($1->type, "Bool") == 0){
  487.  
  488. if(strcmp(varDecl->type, "")== 0){
  489.  
  490. varDecl = insert_value_node_Table($2, "boolean", "", "");
  491. }else{
  492.  
  493. insert_brother_Table(varDecl, insert_value_node_Table($2, "boolean", "", ""));
  494.  
  495. }
  496.  
  497. }else{
  498.  
  499. lower6[length6] = 0;
  500. for(i = 0; i < length6; i++ )
  501. {
  502. lower6[i] = tolower( $1->type[i] );
  503. }
  504.  
  505.  
  506. if(strcmp(varDecl->type, "")==0){
  507. varDecl = insert_value_node_Table($2, lower6, "", "");
  508. }else{
  509. insert_brother_Table(varDecl, insert_value_node_Table($2, lower6, "", ""));
  510.  
  511. }
  512.  
  513.  
  514. }
  515.  
  516.  
  517.  
  518.  
  519. $$ = insertNode("VarDecl");
  520. insert_child($$, $1);
  521. nodeAux2 = insert_value_node("Id", $2);
  522. insert_child($$, nodeAux2);}
  523. }
  524. ;
  525.  
  526. Type: BOOL {if(flag == 0){$$ = insertNode("Bool");}}
  527. | INT {if(flag == 0){$$ = insertNode("Int");}}
  528. | DOUBLE {if(flag == 0){$$ =insertNode("Double");}}
  529. ;
  530.  
  531. Statement: OBRACE Statement_aux CBRACE {if(flag ==0){
  532. if($2!=NULL){
  533. if($2->brother!=NULL){
  534. nodeAux=insertNode("Block");
  535. insert_child(nodeAux,$2);
  536. $$=nodeAux;
  537. }
  538. else{
  539. $$=$2;
  540. }
  541. }
  542. else{
  543. $$=NULL;
  544. }
  545. }}
  546. | OBRACE CBRACE {if(flag==0){$$ = NULL;}}
  547. | IF OCURV Expr CCURV Statement {
  548. if(flag==0){
  549. $$=insertNode("If");
  550. insert_child($$,$3);
  551. nodeAux = $5;
  552. int num=0;
  553. if(nodeAux != NULL){
  554. AST_Tree_node *node = nodeAux;
  555. while(node!= NULL){
  556. node = node->brother;
  557. num++;
  558. }
  559. if(num==1){
  560. insert_child($$,nodeAux);
  561. nodeAux2 = insertNode("Block");
  562. insert_child($$, nodeAux2);
  563. }
  564. if(num>1){
  565. nodeAux2 = insertNode("Block");
  566. nodeAux3 = insertNode("Block");
  567. insert_child(nodeAux2, nodeAux);
  568. insert_child($$,nodeAux2);
  569. insert_brother(nodeAux2,nodeAux3);
  570. }
  571. }
  572. if(nodeAux==NULL)
  573. {
  574. nodeAux2 = insertNode("Block");
  575. nodeAux3 = insertNode("Block");
  576. insert_child($$, nodeAux2);
  577. insert_child($$, nodeAux3);
  578. }
  579. }
  580. }
  581.  
  582. | IF OCURV Expr CCURV Statement ELSE Statement {
  583. if(flag==0){
  584. $$=insertNode("If");
  585. insert_child($$,$3);
  586. nodeAux = $5;
  587. int num=0, num2=0;
  588.  
  589. if(nodeAux != NULL){
  590. AST_Tree_node *node = nodeAux;
  591. while(node!= NULL){
  592. node = node->brother;
  593. num++;
  594. }
  595. if(num==1){
  596. insert_child($$,nodeAux);
  597.  
  598. }
  599. if(num>1){
  600. nodeAux2 = insertNode("Block");
  601. insert_child(nodeAux2, nodeAux);
  602. insert_child($$,nodeAux2);
  603.  
  604. }
  605. }
  606. else if(nodeAux==NULL)
  607. {
  608. nodeAux2 = insertNode("Block");
  609. insert_child($$, nodeAux2);
  610.  
  611. }
  612.  
  613. if($7!=NULL)
  614. {
  615. AST_Tree_node *node2 = $7;
  616. while(node2!= NULL){
  617. node2 = node2->brother;
  618. num2++;
  619. }
  620. if(num2==1){
  621. insert_child($$,$7);
  622.  
  623. }
  624. if(num2>1){
  625. nodeAux2 = insertNode("Block");
  626. insert_child(nodeAux2, $7);
  627. insert_child($$,nodeAux2);
  628.  
  629. }
  630. }
  631.  
  632. else if($7==NULL)
  633. {
  634. nodeAux2 = insertNode("Block");
  635. insert_child($$, nodeAux2);
  636. }
  637. }
  638.  
  639.  
  640. }
  641. | WHILE OCURV Expr CCURV Statement {
  642. if(flag==0){
  643. $$=insertNode("While");
  644. insert_child($$,$3);
  645. nodeAux = $5;
  646. int num=0;
  647. if(nodeAux != NULL){
  648. AST_Tree_node *node = nodeAux;
  649. while(node!= NULL){
  650. node = node->brother;
  651. num++;
  652. }
  653. if(num==1){
  654. insert_child($$,nodeAux);
  655.  
  656. }
  657. if(num>1){
  658. nodeAux2 = insertNode("Block");
  659. insert_child(nodeAux2, nodeAux);
  660. insert_child($$,nodeAux2);
  661.  
  662. }
  663. }
  664. if(nodeAux==NULL)
  665. {
  666. nodeAux2 = insertNode("Block");
  667. insert_child($$, nodeAux2);
  668.  
  669. }
  670. }
  671. }
  672. | DO Statement WHILE OCURV Expr CCURV SEMI {
  673. if(flag==0){
  674. $$=insertNode("DoWhile");
  675. if($2==NULL)
  676. {
  677. nodeAux2 = insertNode("Block");
  678. insert_child($$, nodeAux2);
  679.  
  680. }else{
  681. insert_child($$,$2);
  682. }
  683.  
  684.  
  685. nodeAux = $5;
  686. int num=0;
  687. if(nodeAux != NULL){
  688. AST_Tree_node *node = nodeAux;
  689. while(node!= NULL){
  690. node = node->brother;
  691. num++;
  692. }
  693. if(num==1){
  694. insert_child($$,nodeAux);
  695.  
  696. }
  697. if(num>1){
  698. nodeAux2 = insertNode("Block");
  699. insert_child(nodeAux2, nodeAux);
  700. insert_child($$,nodeAux2);
  701.  
  702. }
  703. }
  704.  
  705. }
  706. }
  707. | PRINT OCURV Expr CCURV SEMI {
  708. if(flag==0){
  709.  
  710. $$=insertNode("Print");
  711. insert_child($$,$3);
  712. }
  713. }
  714. | PRINT OCURV STRLIT CCURV SEMI {
  715. if(flag==0){
  716.  
  717.  
  718. $$=insertNode("Print");
  719. nodeAux = insert_value_node("StrLit", $3);
  720. insert_child($$, nodeAux);
  721. }
  722. }
  723. | SEMI {
  724. if(flag==0){
  725. $$=NULL;
  726. }
  727. }
  728. | Assignment SEMI {
  729. if(flag==0){
  730. $$=$1;
  731. }
  732. }
  733. | MethodInvocation SEMI {
  734. if(flag==0){
  735. $$=$1;
  736. }
  737. }
  738. | ParseArgs SEMI {
  739. if(flag==0){
  740. $$=$1;
  741. }
  742. }
  743. | RETURN SEMI {
  744. if(flag==0){
  745. $$ = insertNode("Return");
  746. }
  747. }
  748. | RETURN Expr SEMI {
  749. if(flag==0){
  750. $$ = insertNode("Return");
  751. insert_child($$,$2);
  752.  
  753. }
  754. }
  755. | error SEMI {flag = 1;$$=NULL;}
  756. ;
  757.  
  758.  
  759.  
  760. Statement_aux: Statement {if(flag==0){$$=$1;}}
  761. | Statement_aux Statement {if(flag==0){if($1!=NULL){$$ = $1; insert_brother($1,$2);}else{$$=$2;}}}
  762. ;
  763.  
  764.  
  765.  
  766. Expr: ExprNew {if(flag == 0){$$ = $1;}}
  767. | Assignment {if(flag == 0){$$ = $1;}}
  768. ;
  769.  
  770. ExprNew: MethodInvocation {if(flag == 0){$$ = $1;}}
  771. | ParseArgs {if(flag == 0){$$ = $1;}}
  772. | ExprNew AND ExprNew {if(flag == 0){$$ = insertNode("And"); insert_child($$, $1); insert_child($$, $3);}}
  773. | ExprNew OR ExprNew {if(flag == 0){$$ = insertNode("Or"); insert_child($$, $1); insert_child($$, $3);}}
  774. | ExprNew EQ ExprNew {if(flag == 0){$$ = insertNode("Eq"); insert_child($$, $1); insert_child($$, $3);}}
  775. | ExprNew GEQ ExprNew {if(flag == 0){$$ = insertNode("Geq"); insert_child($$, $1); insert_child($$, $3);}}
  776. | ExprNew GT ExprNew {if(flag == 0){$$ = insertNode("Gt"); insert_child($$, $1); insert_child($$, $3);}}
  777. | ExprNew LEQ ExprNew {if(flag == 0){$$ = insertNode("Leq"); insert_child($$, $1); insert_child($$, $3);}}
  778. | ExprNew LT ExprNew {if(flag == 0){$$ = insertNode("Lt"); insert_child($$, $1); insert_child($$, $3);}}
  779. | ExprNew NEQ ExprNew {if(flag == 0){$$ = insertNode("Neq"); insert_child($$, $1); insert_child($$, $3);}}
  780. | ExprNew PLUS ExprNew {if(flag == 0){$$ = insertNode("Add"); insert_child($$, $1); insert_child($$, $3);}}
  781. | ExprNew MINUS ExprNew {if(flag == 0){$$ = insertNode("Sub"); insert_child($$, $1); insert_child($$, $3);}}
  782. | ExprNew STAR ExprNew {if(flag == 0){$$ = insertNode("Mul"); insert_child($$, $1); insert_child($$, $3);}}
  783. | ExprNew DIV ExprNew {if(flag == 0){$$ = insertNode("Div"); insert_child($$, $1); insert_child($$, $3);}}
  784. | ExprNew MOD ExprNew {if(flag == 0){$$ = insertNode("Mod"); insert_child($$, $1); insert_child($$, $3);}}
  785. | PLUS ExprNew %prec NOT {if(flag == 0){$$ = insertNode("Plus"); insert_child($$, $2);}}
  786. | MINUS ExprNew %prec NOT {if(flag == 0){$$ = insertNode("Minus"); insert_child($$, $2);}}
  787. | NOT ExprNew {if(flag == 0){$$ = insertNode("Not"); insert_child($$, $2);}}
  788. | ID DOTLENGTH {if(flag == 0){$$ = insertNode("Length"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);}}
  789. | ID {if(flag == 0){$$ = insert_value_node("Id", $1);}}
  790. | OCURV Expr CCURV {if(flag == 0){$$ = $2;}}
  791. | BOOLLIT {if(flag == 0){$$ = insert_value_node("BoolLit", $1);}}
  792. | DECLIT {if(flag == 0){$$ = insert_value_node("DecLit", $1);}}
  793. | REALLIT {if(flag == 0){$$ = insert_value_node("RealLit", $1);}}
  794. | OCURV error CCURV {if(flag == 0){$$ = NULL;}}
  795. ;
  796.  
  797. Assignment: ID ASSIGN Expr {if(flag == 0){$$ = insertNode("Assign"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux); insert_child($$, $3);}}
  798. ;
  799.  
  800.  
  801. MethodInvocation: ID OCURV CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);}}
  802. | ID OCURV Expr CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux); insert_child($$, $3);}}
  803. | 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);}}
  804. | ID OCURV error CCURV {flag = 1;$$=NULL;}
  805. ;
  806.  
  807. CommaExpr: COMMA Expr {if(flag == 0){$$ = $2;}}
  808. | CommaExpr COMMA Expr {if(flag == 0){$$ = $1; insert_brother($$, $3);}}
  809. ;
  810.  
  811. 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);}}
  812. | PARSEINT OCURV error CCURV {flag =1;$$=NULL;}
  813. ;
  814.  
  815.  
  816.  
  817. %%
  818.  
  819. void yyerror(const char* s){
  820. if(flag ==0){
  821. flag = 1;
  822. }
  823. printf("Line %d, col %d: %s: %s\n",n_linha,(int)(n_coluna - strlen(yytext)),s,yytext);
  824. }
  825.  
  826.  
  827.  
  828. void insert_brother(AST_Tree_node* brother, AST_Tree_node* node)
  829. {
  830.  
  831.  
  832. AST_Tree_node* aux = brother;
  833.  
  834. if(aux!=NULL && node!=NULL)
  835. {
  836. while(aux->brother != NULL)
  837. {
  838. aux = aux->brother;
  839. }
  840. aux->brother = node;
  841. }
  842. }
  843.  
  844. void insert_brother_Table(Table_node* brother, Table_node* node)
  845. {
  846.  
  847.  
  848. Table_node* aux = brother;
  849.  
  850. if(aux!=NULL && node!=NULL)
  851. {
  852. while(aux->brother != NULL)
  853. {
  854. aux = aux->brother;
  855. }
  856. aux->brother = node;
  857. }
  858. }
  859.  
  860.  
  861. void insert_child(AST_Tree_node* father, AST_Tree_node* child)
  862. {
  863.  
  864. AST_Tree_node* temp = father->child;
  865.  
  866. if(temp==NULL)
  867. {
  868. father->child = child;
  869. }
  870. else
  871. {
  872.  
  873. insert_brother(father->child, child);
  874. }
  875.  
  876. }
  877.  
  878. void insert_child_Table(Table_node* father, Table_node* child)
  879. {
  880.  
  881. Table_node* temp = father->child;
  882.  
  883. if(temp==NULL)
  884. {
  885. father->child = child;
  886. }
  887. else
  888. {
  889.  
  890. insert_brother_Table(father->child, child);
  891. }
  892.  
  893. }
  894.  
  895.  
  896. AST_Tree_node* insert_value_node(char* node, char* value){
  897.  
  898. AST_Tree_node* new_node = insertNode(node);
  899. new_node->value = value;
  900. return new_node;
  901.  
  902. }
  903.  
  904. Table_node* insert_value_node_Table(char* name, char* type, char* paramsType, char* flag){
  905.  
  906. Table_node* new_node = insertNode_Table(name);
  907. new_node->type = type;
  908. new_node->paramsType = paramsType;
  909. new_node->flag=flag;
  910. return new_node;
  911.  
  912. }
  913.  
  914.  
  915.  
  916. AST_Tree_node* insertNode(char* node){
  917. AST_Tree_node* new_node;
  918. new_node = (AST_Tree_node *)(malloc(sizeof(AST_Tree_node)));
  919. if(new_node != NULL){
  920. new_node->child = NULL;
  921. new_node->brother = NULL;
  922. new_node->value = NULL;
  923. new_node->type = node;
  924. }
  925. return new_node;
  926. }
  927.  
  928. Table_node* insertNode_Table(char* node){
  929. Table_node* new_node;
  930. new_node = (Table_node *)(malloc(sizeof(Table_node)));
  931. if(new_node != NULL){
  932.  
  933. new_node->child = NULL;
  934. new_node->brother = NULL;
  935. new_node->name=node;
  936. new_node->type=NULL;
  937. new_node->paramsType=NULL;
  938. new_node->flag=NULL;
  939.  
  940. }
  941. return new_node;
  942. }
  943.  
  944.  
  945.  
  946.  
  947. void printTree(AST_Tree_node *node, int level){
  948. printPoints(level);
  949.  
  950. 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 ){
  951. printTerminal(node);
  952. }
  953. else{
  954.  
  955. printf("%s\n", node->type);
  956.  
  957.  
  958. }
  959.  
  960. AST_Tree_node *child = node->child;
  961.  
  962. if(child != NULL){
  963. printTree(child, level+1);
  964.  
  965. while(child->brother != NULL){
  966. child = child->brother;
  967. printTree(child, level+1);
  968. }
  969.  
  970. }
  971.  
  972. }
  973.  
  974.  
  975. void printTerminal(AST_Tree_node *node){
  976.  
  977. printf("%s(%s)\n", node->type, node->value);
  978. }
  979. void printPoints(int n){
  980. while(n > 0){
  981. printf("..");
  982. n--;
  983. }
  984. }
  985.  
  986.  
  987. void printTable(Table_node *node){
  988. Table_node *aux = node->child;
  989. int count =0;
  990. int flag2 = 0;
  991. int i =0;
  992.  
  993. printf("==== %s %s Symbol Table ====\n", node->type, node->name);
  994. while(flag2 !=1){
  995.  
  996.  
  997.  
  998.  
  999. printf("%s\t%s\t%s\n", aux->name, aux->paramsType, aux->type);
  1000.  
  1001.  
  1002.  
  1003.  
  1004. if(aux->child != NULL){
  1005. count++;
  1006. }
  1007. if(aux->brother != NULL)
  1008. {
  1009. aux = aux->brother;
  1010. }else{
  1011. flag2 =1 ;
  1012.  
  1013. }
  1014. }
  1015. printf("\n");
  1016. Table_node *aux2 = node->child;
  1017. while(count != 0){
  1018.  
  1019. if(aux2->child != NULL){
  1020.  
  1021. count--;
  1022. Table_node *aux3 = aux2->child;
  1023. printf("==== Method %s%s Symbol Table ====\n", aux2->name, aux2->paramsType);
  1024. while(aux3 != NULL){
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030. if(strcmp(aux3->flag, "") == 0){
  1031.  
  1032. printf("%s\t%s\t%s\n", aux3->name, aux3->paramsType, aux3->type);
  1033.  
  1034. }else{
  1035.  
  1036. printf("%s\t%s\t%s\t%s\n", aux3->name, aux3->paramsType, aux3->type, aux3->flag);
  1037. }
  1038.  
  1039. aux3 = aux3->brother;
  1040. }
  1041. printf("\n");
  1042. }
  1043.  
  1044. aux2 = aux2->brother;
  1045.  
  1046.  
  1047. }
  1048.  
  1049.  
  1050.  
  1051.  
  1052. }
  1053.  
  1054.  
  1055.  
  1056.  
  1057. int main(int argc, char *argv[])
  1058. {
  1059. int imprimir2 =0;
  1060. head_table= insert_value_node_Table("", "Class", "", "");
  1061. varDecl= insert_value_node_Table("", "", "", "");
  1062. if(argc>1){
  1063. if(strcmp(argv[1],"-l")==0){
  1064. imprimir=1;
  1065. yylex();
  1066. }
  1067. else if(strcmp(argv[1],"-1")==0){
  1068. yylex();
  1069. }else if(flag ==0 && strcmp(argv[1],"-t")==0){
  1070. parse = 1;
  1071. imprimir2 = 1;
  1072. yyparse();
  1073.  
  1074. }
  1075. }else{
  1076. parse = 1;
  1077. yyparse();
  1078.  
  1079.  
  1080. }
  1081. if(imprimir2 == 1 && flag == 0){
  1082. printTable(head_table);
  1083. printTree(head, 0);
  1084.  
  1085.  
  1086. }
  1087.  
  1088.  
  1089.  
  1090. return 0;
  1091. }
Add Comment
Please, Sign In to add comment