graphVizHelper.c

Description du code

Génération d'une image d'un arbre syntaxique abstrait.
Classe d'intégration de l'outil GraphViz. Compilateur LSD010

Code source ou contenu du fichier

  1. /*
  2.  * graphVizHelper.c : Helper for the graphViz tool (generation of images from a tree)
  3.  * @see http://www.graphviz.org/doc/info/command.html
  4.  * Part of the compiler project for LSD10 language
  5.  * Gaudry Stéphane
  6.  * More information on http://www.gaudry.be/programmer-arbre-image-noeuds.html
  7.  */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include "common.h"
  13. #include "graphVizHelper.h"
  14. #include "y.tab.h"
  15. #if(VERBOSE_LEVEL<=DEB_E)
  16. #include <errno.h>
  17. #endif
  18. extern AstNode *rootNode;
  19.  
  20. /*
  21.  * **********************************************************
  22.  * Internal business
  23.  * **********************************************************
  24.  */
  25.  
  26. #define GRAPH_RIGHT 0
  27. #define GRAPH_LEFT 1
  28. #define GRAPH_ROOT -1
  29.  
  30. int isGraphVisInstalled()
  31. {
  32. return 1;//todo : get it dynamically
  33. }
  34.  
  35. /**
  36.  * http://www.graphviz.org/doc/info/command.html
  37.  */
  38. void generateImage(char *dotFileName)
  39. {
  40. time_t now;
  41. struct tm *timeinfo;
  42.  
  43. now = time(NULL);//time ( &now );
  44. timeinfo = localtime ( &now );
  45.  
  46. char commandString[100];
  47. //todo : set the timeinfo int values on a 2 digits format
  48. commandString,
  49. "%slsd10_Img%d%d%d%d%d%d.jpg %s",
  50. "dot -Tjpg -o",
  51. timeinfo->tm_year+1900,
  52. timeinfo->tm_mon+1,
  53. timeinfo->tm_mday,
  54. timeinfo->tm_hour,
  55. timeinfo->tm_min,
  56. timeinfo->tm_sec,
  57. dotFileName);
  58. printf("\n;\tGenerating image from dot file : %s\n",commandString);
  59.  
  60. int ret = 0;
  61. ret = system (commandString);
  62. //printf ("ret = %d\n", ret);
  63.  
  64. }
  65.  
  66. void printGraphDotHeader(FILE * graphFile)
  67. {
  68.  
  69. if(graphFile!=NULL)
  70. {
  71. fprintf(graphFile, "digraph LSD10{\n");
  72. fprintf(graphFile, "\tbgcolor=white \n");
  73. fprintf(graphFile, "\tnode [");
  74. fprintf(graphFile, "color=\"#9DACBF\", ");
  75. fprintf(graphFile, "fontcolor=\"#000000\", ");
  76. fprintf(graphFile, "style=filled");
  77. fprintf(graphFile, "];\n");
  78. fprintf(graphFile, "\tedge [arrowsize=2, color=\"#000000\"];\n");
  79. }
  80. }
  81.  
  82. void printGraphDotFooter(FILE * graphFile)
  83. {
  84.  
  85. if(graphFile!=NULL)
  86. {
  87.  
  88. time_t now;
  89. struct tm *timeinfo;
  90.  
  91. now = time(NULL);//time ( &now );
  92. timeinfo = localtime ( &now );
  93. // fprintf(graphFile, "}\n");
  94. // fprintf(graphFile, "digraph INFO{\n");
  95. graphFile,
  96. "\t\"Generated by the SSHD09 LSD010 compiler\\n%d/%d/%d %dHr %d\\nWith GraphViz engine\" [",
  97. timeinfo->tm_mday,
  98. timeinfo->tm_mon+1,
  99. timeinfo->tm_year+1900,
  100. timeinfo->tm_hour,
  101. timeinfo->tm_min,
  102. timeinfo->tm_sec
  103. );
  104. fprintf(graphFile, "shape=box, ");
  105. fprintf(graphFile, "color=\"#FF9933\", ");
  106. fprintf(graphFile, "fontcolor=\"#000000\"");
  107. fprintf(graphFile, "];\n");
  108. fprintf(graphFile, "}\n");
  109. }
  110. }
  111. /**
  112.  * Prints an AST node into the graph file
  113.  * Pre-condition : dotFile not null
  114.  */
  115. void printGraphNode(FILE *dotFile, AstNode *node, int depth, int psn)
  116. {
  117. if(node!=NULL)
  118. {
  119. if(node->parent!=NULL)
  120. {
  121. fprintf(dotFile, "\t\"%p\" -> ", node->parent);
  122. }
  123. else
  124. {
  125. fprintf(dotFile, "\t");
  126. }
  127. dotFile,
  128. "\"%p\";\n\t\"%p\" [shape=%s, color=\"%s\", fontcolor=\"%s\", label=\"%s%s%s%s : \\n%s %s\\nLine %d char %d\"];\n",//print position?
  129. node,
  130. node,
  131. //node->type>LEX_FIRST_TOKEN&&node->type<LEX_LAST_TOKEN?"polygon":"box",
  132. psn==GRAPH_LEFT?"hexagon":(psn==GRAPH_RIGHT?"box":"doublecircle"),
  133. (node->type==NODE_TYPE_DECLARATION
  134. || node->type==NODE_TYPE_FUNCTION
  135. || node->type==NODE_TYPE_PARAM_DECL
  136. )?"#677E96":psn==GRAPH_LEFT?"#e6e8f2":(psn==GRAPH_RIGHT?"#AAB5C6":"#C0C0C0"),
  137. (node->type==NODE_TYPE_DECLARATION
  138. || node->type==NODE_TYPE_FUNCTION
  139. || node->type==NODE_TYPE_PARAM_DECL
  140. )?"#FFFFFF":"#000000",
  141. typeToString(node->type),
  142. node->subtype!=NODE_TYPE_NOTHING?" (":"",
  143. node->subtype!=NODE_TYPE_NOTHING?typeToString(node->subtype):"",
  144. node->subtype!=NODE_TYPE_NOTHING?")":"",
  145. typeToString(node->info->type),
  146. node->info->name,
  147. node->debug->line,
  148. node->debug->linePsn
  149. );
  150. printGraphNode(dotFile, node->left, depth+1, GRAPH_LEFT);
  151. printGraphNode(dotFile, node->right, depth+1, GRAPH_RIGHT);
  152. }
  153. //else fprintf(dotFile, "null");
  154. }
  155. void printGraphDotFromXML(char *xmlFileName, char *dotFileName)
  156. {
  157.  
  158. FILE * graphFile;
  159. time_t genTime = time(NULL);
  160. if(xmlFileName==NULL)xmlFileName=AST_XML_FILE;
  161. if(dotFileName==NULL)dotFileName=GRAPHVIZ_CONFIG_FILE;
  162. graphFile = fopen(dotFileName, "w");
  163. if(graphFile!=NULL)
  164. {
  165. printf("\n;\tGenerating AST Graph from %s into %s file ... (%s, col %d)", xmlFileName, dotFileName, __FILE__, __LINE__);
  166. printGraphDotHeader(graphFile);
  167. //todo : read info from XML file to generate the dot file
  168. fprintf(graphFile, "Warning : reading from XML is not yet implemented\n");
  169. printGraphDotFooter(graphFile);
  170. fclose(graphFile);
  171.  
  172. #if(VERBOSE_LEVEL<=DEB_EXEC)
  173. printMsg(DEB_EXEC,"...OK Graph printed", __FILE__, __LINE__);
  174. #endif
  175.  
  176. if(isGraphVisInstalled()==1)
  177. {
  178. generateImage(dotFileName);
  179. }
  180. }
  181. else
  182. {
  183. #if(VERBOSE_LEVEL<=DEB_EXEC)
  184. printMsg(DEB_EXEC,"Printing AST into Graph file ... Can't open file", __FILE__, __LINE__);
  185. printMsg(DEB_E, (char *)strerror(errno), __FILE__, __LINE__);
  186. #endif
  187. }
  188. }
  189.  
  190. void printGraphDotFromAST(char *dotFileName)
  191. {
  192.  
  193. FILE * graphFile;
  194. time_t genTime = time(NULL);
  195. if(dotFileName==NULL)dotFileName=GRAPHVIZ_CONFIG_FILE;
  196. graphFile = fopen(dotFileName, "w");
  197. if(graphFile!=NULL)
  198. {
  199. printf("\n;\tGenerating AST Graph from last compilation into %s file ... (%s, col %d)", dotFileName, __FILE__, __LINE__);
  200. printGraphDotHeader(graphFile);
  201. printGraphNode(graphFile, rootNode, 0, GRAPH_ROOT);
  202. printGraphDotFooter(graphFile);
  203. fclose(graphFile);
  204.  
  205. #if(VERBOSE_LEVEL<=DEB_EXEC)
  206. printMsg(DEB_EXEC,"...OK Graph printed", __FILE__, __LINE__);
  207. #endif
  208. }
  209. else
  210. {
  211. #if(VERBOSE_LEVEL<=DEB_EXEC)
  212. printMsg(DEB_EXEC,"Printing AST into Graph file ... Can't open file", __FILE__, __LINE__);
  213. printMsg(DEB_E, (char *)strerror(errno), __FILE__, __LINE__);
  214. #endif
  215. }
  216. }
  217.  
  218. /*
  219.  * **********************************************************
  220.  * Implementation of the header exposed items
  221.  * See graphVizHelper.h for these functions comments
  222.  * **********************************************************
  223.  */
  224.  
  225. void printGraph()
  226. {
  227. printGraphDotFromAST(GRAPHVIZ_CONFIG_FILE);
  228. if(isGraphVisInstalled()==1)
  229. {
  230. generateImage(GRAPHVIZ_CONFIG_FILE);
  231. }
  232. }

Autres extraits de codes en c

  • DisquetteDispo Vérifier la disponibilité du lecteur de disquette
  • Suite de Fibonacci Exemple d'itération en C
  • Suite de Fibonacci Exemple de récursion en C
  • astDataRepresentation.h Représentation de données de l'arbre syntaxique abstrait Compilateur LSD010
  • ast.h Arbre syntaxique abstrait Compilateur LSD010
  • ast.c Arbre syntaxique abstrait Compilateur LSD010
  • symbolsTableDataRepresentation.h Représentation de données de la table des symboles Compilateur LSD010
  • symbolsTable.h Fonctions de gestion de la table des symboles Compilateur LSD010
  • symbolsTable.c Fonctions de gestion de la table des symboles Compilateur LSD010
  • hashCode.h Fonctions de hachage Compilateur LSD010
  • hashCode.c Fonctions de hachage Compilateur LSD010
  • scopeStack.h Fonctions de gestion d'une pile de portées Compilateur LSD010
  • scopeStack.c Fonctions de gestion d'une pile de portées Compilateur LSD010
  • scopeHelper.h Fonctions de gestion de la portée courante Compilateur LSD010
  • console.h Fonctions d'affichage Compilateur LSD010
  • console.c Fonctions d'affichage Compilateur LSD010
  • graphVizHelper.h Génération d'une image d'un arbre syntaxique abstrait.
    Classe d'intégration de l'outil GraphViz. Compilateur LSD010
  • graphVizHelper.c Génération d'une image d'un arbre syntaxique abstrait.
    Classe d'intégration de l'outil GraphViz. Compilateur LSD010
  • common.h Définition des constantes et variables communes Compilateur LSD010
  • pcode.c Génération de p-code Compilateur LSD010
  • pcode.h Génération de p-code Compilateur LSD010
  • Tous les extraits

Document créé le 05/10/2009, dernière modification le 28/10/2018
Source du document imprimé : https://www.gaudry.be/sniplet-rf-lsd010/project/source/graphVizHelper.c.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.