graphVizHelper.c

Description du code

graphVizHelper.c est un fichier du projet Compilateur LSD010.
Ce fichier est situé dans /var/www/bin/sniplets/lsd010/.

Projet Compilateur LSD010 :

Compilateur LSD010 développé dans le cadre du cours de syntaxe et sémantiqueref 1

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. }

Structure et Fichiers du projet

Afficher/masquer...


Répertoires contenus dans /var/www/bin/sniplets/lsd010/project/source/ 
IcôneNomTailleModification
Pas de sous-répertoires.
IcôneNomTailleModification
| _ Répertoire parent0 octets1714277165 28/04/2024 06:06:05
Fichiers contenus dans /var/www/bin/sniplets/lsd010/project/source/ 
IcôneNomTailleModificationAction
IcôneNomTailleModificationAction
Afficher le fichier .o|.ographVizHelper.o4.72 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.cast.c27.73 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hconst.h4.01 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.cy.tab.c84.53 Ko31/10/2018 18:32:39-refusé-
Afficher le fichier .y|.ylsd10.y22.88 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.olex.yy.o18.88 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .c|.cscopeStack.c3.69 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hgraphVizHelper.h573 octets31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hsymbolsTableDataRepresentation.h1.31 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.osymbolsTable.o5.2 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hcommon.h1.08 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.cscopeHelper.c4.09 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.ohashCode.o1.45 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hconsole.h2.21 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .o|.oconsole.o12.23 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .|lsd10lsd1075.26 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hhashCode.h1020 octets31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hsymbolsTable.h2.65 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hpcode.h417 octets31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.oast.o11.45 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hscopeStack.h2.2 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .c|.cgraphVizHelper.c6.11 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .l|.llsd10.l6.46 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hy.tab.h4.69 Ko31/10/2018 18:32:39-refusé-
Afficher le fichier .output|.outputy.output81.69 Ko31/10/2018 18:32:39-refusé-
Afficher le fichier .o|.oy.tab.o24.45 Ko31/10/2018 18:32:39-refusé-
Afficher le fichier .c|.clex.yy.c57.93 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hast.h2.39 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .o|.oscopeStack.o1.34 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.oscopeHelper.o2.59 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hastDataRepresentation.h1.87 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.csymbolsTable.c14.91 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .c|.cpcode.c23.45 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .c|.chashCode.c3.94 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.cconsole.c18.01 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hscopeHelper.h719 octets31/10/2018 18:32:38-refusé-

Avertissement

Ce code présente une manière possible d'implémenter un compilateur, et certains choix peuvent être discutés.
Cependant, il peut donner des pistes pour démarrer, ou approcher certains concepts, et je tenterais par la suite de mettre à jour le code.

Utilisation de l'explorateur de code

  • Navigation :
    • Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
    • Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
    • La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
    • Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
  • Affichage :
    • Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
  • Actions :
    • Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.

Document créé le 07/03/2010, dernière modification le 28/10/2018
Source du document imprimé : https://www.gaudry.be/langages-lsd10-source-rf-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.

Notes

  1. a,b LSD010 : Langage Simple et Didactique Il existe une un certain nombre d'interprétations de l'acronyme LSD (Langage Symbolique Didactique, Langage Sans Difficulté, Langage Simple et Didactique). LSD010 est la version 2010 de la suite LSD80, LSD_02, LSD03, LSD04, LSD05, LSD06, LSD07, LSD08, et LSD09.

Table des matières Haut

Références

  1. livre Langue du document :fr IHDCB332 - Théorie des langages : Syntaxe et sémantique : PY Schobbens, Syntaxe et sémantique (January 2010)

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut