Falexom

Untitled

Oct 24th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. #include <string.h>
  2.  
  3. #include "MyLangLexer.h"
  4. #include "MyLangParser.h"
  5. #include <antlr3.h>
  6. #include <dirent.h>
  7. #include <stdio.h>
  8. #include <stdbool.h>
  9. #include <stdlib.h>
  10.  
  11. bool currTreeOn = false;
  12. pANTLR3_BASE_TREE currNode = NULL;
  13. pANTLR3_BASE_TREE sourceNode = NULL;
  14. pANTLR3_BASE_TREE lastArraySuffixNode = NULL;
  15. int counter = 0;
  16.  
  17. pANTLR3_BASE_TREE rebuildTree(pANTLR3_BASE_TREE tree) {
  18. if (tree == NULL) {
  19. return NULL;
  20. }
  21.  
  22. char* currentNode = (char*)tree->toString(tree)->chars;
  23. unsigned int childCount = tree->getChildCount(tree);
  24.  
  25. for (unsigned int i = 0; i < childCount; ++i) {
  26. pANTLR3_BASE_TREE child = (pANTLR3_BASE_TREE)tree->getChild(tree, i);
  27. char* childNode = (char*)child->toString(child)->chars;
  28. if (strcmp(currentNode, "Body") == 0) {
  29. sourceNode = tree;
  30. }
  31. if (strcmp(childNode, "ArrayTokenSuffix") == 0 && currTreeOn) {
  32. if (lastArraySuffixNode == NULL) {
  33. currNode->addChild(currNode, child);
  34. } else {
  35. lastArraySuffixNode->addChild(lastArraySuffixNode, child);
  36. }
  37. lastArraySuffixNode = child;
  38. tree->deleteChild(tree, i);
  39. --i;
  40. --childCount;
  41. continue;
  42. }
  43. if (strcmp(childNode, "ArrayToken") == 0 && !currTreeOn) {
  44. currNode = child;
  45. currTreeOn = true;
  46. lastArraySuffixNode = NULL;
  47. counter = 2;
  48. } else if (strcmp(childNode, "ArrayToken") != 0 && currTreeOn) {
  49. counter--;
  50. } else if (strcmp(childNode, "ArrayToken") != 0 && counter <= 0) {
  51. counter = 0;
  52. currTreeOn = false;
  53. }
  54. rebuildTree(child);
  55. }
  56.  
  57. return tree;
  58. }
  59.  
  60.  
  61. void drawTree(const pMyLangParser parser, pANTLR3_BASE_TREE tree) {
  62. rebuildTree(tree);
  63. pANTLR3_STRING output = parser->adaptor->makeDot(parser->adaptor, tree);
  64. FILE *file = fopen("tree.dot", "w");
  65. if (file == NULL) {
  66. perror("Could not open file for writing");
  67. return;
  68. }
  69. fprintf(file, "%s", output->chars);
  70. fclose(file);
  71.  
  72. const char *generateTree = "dot -Tpng tree.dot -o output.png";
  73. int ret = system(generateTree);
  74. if (ret != 0) {
  75. perror("Error generating PNG");
  76. }
  77. }
  78.  
  79. void makeTree(char *content) {
  80. const pANTLR3_UINT8 input_string = (pANTLR3_UINT8) content;
  81.  
  82. const pANTLR3_INPUT_STREAM input = antlr3StringStreamNew(input_string, ANTLR3_ENC_8BIT,
  83. strlen(input_string), (pANTLR3_UINT8)"SPOparser");
  84. const pMyLangLexer lex = MyLangLexerNew(input);
  85. const pANTLR3_COMMON_TOKEN_STREAM tokens = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lex));
  86. const pMyLangParser parser = MyLangParserNew(tokens);
  87. const MyLangParser_source_return r = parser->source(parser);
  88. if (parser->pParser->rec->state->errorCount > 0) {
  89. parser->free(parser);
  90. tokens->free(tokens);
  91. lex->free(lex);
  92. input->close(input);
  93. return;
  94. }
  95.  
  96. pANTLR3_BASE_TREE ast = r.tree;
  97. drawTree(parser, ast);
  98. printf("end");
  99. parser->free(parser);
  100. tokens->free(tokens);
  101. lex->free(lex);
  102. input->close(input);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment