console.c
Description du code
console.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
Code c (console.c) (706 lignes)
/* * console.c : output (terminal and files) helper file * Part of the compiler project for LSD10 language * Gaudry Stéphane * More information on http://www.gaudry.be/langages-lex-yacc-intro.html */ #include <stdio.h> #include <stdlib.h> #include <string.h> #if(VERBOSE_LEVEL<=DEB_E) #include <errno.h> #endif #if(VERBOSE_LEVEL<=DEB_I) #include <time.h> #endif //includes for the var args #include <stdarg.h> //end of var args includes #include "common.h" #include "graphVizHelper.h" #include "symbolsTableDataRepresentation.h" extern int lexLinesCount; extern char* yytext; extern int *yylineno; extern FILE *yyin; extern AstNode *rootNode; extern DebugInfo *debugInfo; void printHTMLTree(FILE *htmlFile, AstNode *node, int depth); void printHTMLNode(FILE *htmlFile, AstNode *node, int depth); void printXMLTree(FILE *htmlFile, AstNode *node, int depth); void printXMLNode(FILE *htmlFile, AstNode *node, int depth); /**************************************************************/ /** * Generates Yacc error with custom message * todo: 2 function; this is used only to call the yyerror function and producing a exit(EXIT_SUCCESS), * and another to print KO with an exit(EXIT_FAILURE) on compiler inexpected bug (like allocation error) * Exposed method */ void onError(char* errorMsg, char* compilerFile, int compilerLine, AstNode *node) { if(node!=NULL) { lexLinesCount=node->debug->line; //lexCharsCountBeforeToken=node->debug->line; todo : yylloc.first_line?? debugInfo=node->debug; } yytext=NULL; #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_W, errorMsg, compilerFile, compilerLine); #endif yyerror(errorMsg); } void onNotForwardDeclarationError(AstNode *current, AstNode *found) { char errorStr[1024];//todo: minimize length char *str = errorStr; if(current!=found) { str, "Backward declaration failure for %s %s line %d char %d, possible forward declaration on %s %s line %d col %d", typeToString(current->type), current->info->name, current->debug->line, current->debug->linePsn, typeToString(found->type), found->info->name, found->debug->line, found->debug->linePsn ); } else { str, "Backward declaration failure for %s %s line %d col %d", typeToString(current->type), current->info->name, current->debug->line, current->debug->linePsn ); } onError(str, __FILE__, __LINE__, current); } void onNotIntegerNotBooleanTypeError(AstNode *node, char *file, int line) { char errorStr[1024];//todo: minimize length char *str = errorStr; str, "Type check failure : %s or %s expected, but %s found for '%s' line %d col %d (compiler %s, line %d)", typeToString(AST_INTEGER_VAR_TYPE), typeToString(AST_BOOLEAN_VAR_TYPE), typeToString(node->info->computedType), node->info->name, node->debug->line, node->debug->linePsn, file, line ); lexLinesCount=node->debug->line; yytext=NULL; yyerror(str); //failure for the parsed code, but success for the compiler (it must stop here) } /** * Generates Yacc error if a type is not recognized * Exposed method */ void onUnrecognizedTypeError(int type, char* file, int line) { char errorStr[1024];//todo: minimize length onError(errorStr, file, line, NULL); } /** * Print custom message on stdout, depending on the Debug level * Exposed method */ void printMsg(int msgType, char *msg, char *file, int line) { /*if(VERBOSE_LEVEL>msgType)return;*/ switch(msgType) { case DEB_W : printf(";\n;\tWarning : '%s' On %s, Line %d\n;\t-------------------------------------------------------------\n", msg, file, line); break; case DEB_E : printf(";\n;\tError : '%s' On %s, Line %d\n;\t-------------------------------------------------------------\n", msg, file, line); break; case DEB_EXEC : break; default : break; } return; } /******************************************************************************/ void printDebugTree(char *htmlFileName, char *xmlFileName) { FILE * htmlFile; if(htmlFileName==NULL)htmlFileName=AST_HTML_FILE; if(xmlFileName==NULL)xmlFileName=AST_XML_FILE; if(htmlFile!=NULL) { printHTMLTree(htmlFile, rootNode, 0); #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_EXEC,"\t...OK HTML printed", __FILE__, __LINE__); #endif } else { #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_EXEC,"Printing AST into HTML file ... Can't open file", __FILE__, __LINE__); #endif } if(htmlFile!=NULL) { printXMLTree(htmlFile, rootNode, 0); #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_EXEC,"\t...OK XML printed", __FILE__, __LINE__); #endif } else { #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_EXEC,"Printing AST into XML file ... Can't open file", __FILE__, __LINE__); #endif } } /** * Generates an HTML file and an XML file with AST nodes. * These files are generated on the current directory. * An error message is displayed if a problem occurs on opening files * Exposed method */ void printTree() { printDebugTree(NULL, NULL); } //alternate row int altRowSTI=0; void printScopeStack(ScopeStack *scopeStack, FILE * htmlFile, int topItem) { if(scopeStack!=NULL) { int altRow=0; altRowSTI=0; if(topItem!=0) { htmlFile, //"<table width=\"100%%\">", "<table width=\"100%%\" border=\"1\" cellpadding=\"2\" cellspacing=\"1\" class=\"table\">" ); htmlFile, "<thead><tr><td colspan=\"5\">Pile du symbole : %s</td></tr>", scopeStack->declarationNode->info->name ); fprintf(htmlFile, "<tr><th>Profondeur</th><th>Portée</th><th>Type</th><th>Ligne</th><th>Col</th></tr>"); } htmlFile, (++altRow%2==0)? "<tr class=\"td\">": "<tr class=\"td2\">" ); htmlFile, "<td>%d</td><td>%d</td><td>%s</td><td>%d</td><td>%d</td></tr>", scopeStack->declarationNode->info->scopeDepth, scopeStack->declarationNode->info->scopeId, typeToString(scopeStack->declarationNode->info->type), scopeStack->declarationNode->debug->line, scopeStack->declarationNode->debug->linePsn ); printScopeStack(scopeStack->parentPtr, htmlFile, 0); if(topItem!=0) { } } } int printSymbolsTableHeader() { FILE * htmlFile; char *htmlFileName=SYMBOLSTABLE_HTML_FILE; if(htmlFile==NULL) { return EXIT_FAILURE; } return EXIT_SUCCESS; } int printSymbolsTableFooter() { //printSymbolsTableItem(); FILE * htmlFile; //time_t genTime = time(NULL); char *htmlFileName=SYMBOLSTABLE_HTML_FILE; if(htmlFile==NULL) { return EXIT_FAILURE; } return EXIT_SUCCESS; } // first null index int startNull = INITIAL_INT; // @todo: define a astnodedebug as parameter to show where // this scope enter/exit was found int printSymbolsTableItem(char *title) { if(declarations!=NULL) { FILE * htmlFile; //time_t genTime = time(NULL); char *htmlFileName=SYMBOLSTABLE_HTML_FILE; if(htmlFile!=NULL) { fprintf(htmlFile, "<?php \n?><h2>%s (portée actuelle : %d)</h2>", title, scopeHelperGetCurrentScope()); //fprintf(htmlFile, "<table width=\"100%%\" border=\"1\"><thead>"); fprintf(htmlFile, "<?php \n?><table width=\"100%%\" border=\"1\" cellpadding=\"2\" cellspacing=\"1\" class=\"table\"><thead>"); fprintf(htmlFile, "<tr><td>Index</td><td>Liste chaînée de <pre>SymTableEntry</pre></td></tr>"); int i; for(i=0;i<TABLES_SIZE;i++) { if(declarations[i]==NULL) { if(startNull==INITIAL_INT) { startNull=i; } } else { if(startNull!=-INITIAL_INT) { if(startNull<-i-1) { } else { } startNull = INITIAL_INT; } SymTableEntry *entry = declarations[i]; while(entry!=NULL) { printScopeStack(entry->scopeStack, htmlFile, 1); entry = entry->next; } } } if(startNull!=INITIAL_INT) { if(startNull<i-1) { } else { } startNull = INITIAL_INT; } return EXIT_SUCCESS; } } else { printMsg(DEB_EXEC,"-------------------------Symbols table is null", __FILE__, __LINE__); } return EXIT_FAILURE; } char *variableUsageToString(VariableUsage usage) { switch(usage) { case VAR_USAGE_NEVER: return "n'est jamais utilisée"; break; case VAR_USAGE_SOMETIMES: return "est parfois utilisée"; break; case VAR_USAGE_ALWAYS: return "est toujours utilisée"; break; default: return "[Erreur (valeur hors norme)]"; } } void printScopeUsage(ScopeStack *scopeStack) { if(scopeStack!=NULL) { "; %s(...) : la %s '%s' %s\n", scopeStack->functionNode==NULL?"Program":scopeStack->functionNode->info->name, scopeStack->declarationNode->type==NODE_TYPE_FUNCTION?"fonction":"variable", scopeStack->declarationNode->info->name, variableUsageToString(scopeStack->usage) ); printScopeUsage(scopeStack->parentPtr); } } int printSymbolsUsage() { if(declarations!=NULL) { int i; for(i=0;i<TABLES_SIZE;i++) { if(declarations[i]!=NULL) { SymTableEntry *entry = declarations[i]; while(entry!=NULL) { printScopeUsage(entry->scopeStack); entry = entry->next; } } } return EXIT_SUCCESS; } else { printMsg(DEB_EXEC,"Can't print it: Symbols table is null", __FILE__, __LINE__); } return EXIT_FAILURE; } void printScopeDebug(ScopeStack *scopeStack, int tableId, int stackId) { if(scopeStack!=NULL) { "\n; Table[%3d][%2d]= %7s %10s line %3d, col %3d; scope %d; %s function, ", tableId, stackId, typeToString(scopeStack->declarationNode->info->type), scopeStack->declarationNode->info->name, scopeStack->declarationNode->debug->line, scopeStack->declarationNode->debug->linePsn, scopeStack->declarationNode->info->scopeId, scopeStack->functionNode==NULL?"No ":scopeStack->functionNode->info->name ); printScopeDebug(scopeStack->parentPtr, tableId, ++stackId); } } int printSymbolsTableDebug(char*file, int line) { printMsg(DEB_EXEC,"-------------------------Symbols table state", file, line); if(declarations!=NULL) { int i; for(i=0;i<TABLES_SIZE;i++) { if(declarations[i]!=NULL) { SymTableEntry *entry = declarations[i]; while(entry!=NULL) { printScopeDebug(entry->scopeStack, i, 0); entry = entry->next; } } } return EXIT_SUCCESS; } else { printMsg(DEB_EXEC,"-------------------------Symbols table is null", __FILE__, __LINE__); } return EXIT_FAILURE; } /******************************************************************************/ /** * Prints an AST node into the html file * Internal business */ void printHTMLNode(FILE *file, AstNode *node, int depth) { if(node!=NULL) { if(node->info!=NULL) { file, "<br /><b>Type :</b> %s <br /><b>Nom :</b> %s<br /><b>Valeur :</b> %d\n", typeToString(node->info->type), node->info->name, node->info->value ); while(node->rightBrother!=NULL) { printNode(file, node->rightBrother, depth); }*/ } /** * Prints the AST into the html file * Internal business */ void printHTMLTree(FILE *file, AstNode *node, int depth) { if(node!=NULL) { printHTMLNode(file, node, depth); printHTMLTree(file, node->left, depth+1); printHTMLTree(file, node->right, depth+1); }/** * Prints an AST node into the xml file * Internal business */ void printXMLNode(FILE *file, AstNode *node, int depth) { if(node!=NULL) { if(node->parent!=NULL) { fprintf(file, "<parent><type>%s</type><pmemaddress>%p</pmemaddress></parent>", typeToString(node->parent->type), node->parent); }else { } if(node->info!=NULL) { file, "<info><rightmemaddress>%p</rightmemaddress><leftmemaddress>%p</leftmemaddress><infotype>%s</infotype><infoname>%s</infoname><infoval>%d</infoval></info>\n", node->right, node->left, typeToString(node->info->type), node->info->name, node->info->value, depth ); } } }/** * Prints the AST into the xml file * Internal business */ void printXMLTree(FILE *file, AstNode *node, int depth) { printXMLNode(file, node, depth); if(node!=NULL) { printXMLTree(file, node->left, depth+1); printXMLTree(file, node->right, depth+1); } } /******************************************************************************/ /** * Returns a human readable string from a given type constant value * Expected : one of the DEB_xxx constants * Exposed method */ char* debugLevelToString(int type) { char *str = NULL; switch(type) { case DEB_L: str = "Lex tokens"; break; case DEB_Y: str = "Yacc parsing"; break; case DEB_O: str = "Misc files"; break; case DEB_I: str = "Information messages"; break; case DEB_P: str = "P-code generation"; break; case DEB_W: str = "Warning messages"; break; case DEB_EXEC: str = "Minimum excution messages"; break; case DEB_E: str = "Error messages"; break; case DEB_NONE: str = "Silent (no other output than p-code, and OK or KO)"; break; default: str = "undefined"; #if(VERBOSE_LEVEL<=DEB_W) char str[1024];//todo: minimize length printMsg(DEB_W, str, __FILE__, __LINE__); #endif break; } return str; } /******************************************************************************/ /** * Generates p-code output on stdout, * and (if GENERATE_PCODE_FILE is set to 1) write it into a file * source http://www.pps.jussieu.fr/~rifflet/enseignements/LC4/vararg.html * Only one digit format allowed (examples %s, %d, %f, %l) */ void printPCode(FILE* pcodeFile, char *format, ...) { if(PCODE_GENERATION_BYPASS) { return; } va_list p_list; int i; long l; double f; char *s; int validFormat; validFormat=0; #if(VERBOSE_LEVEL==DEB_NONE) if(*format!=';') { #endif while (*format != 0) { switch(*format){ case '%' : validFormat=1; break; case 'd': /* type int */ if(validFormat==1) { if(pcodeFile!=NULL){ } } break; case 'l': /* type long */ if(validFormat==1) { if(pcodeFile!=NULL){ } } break; case 'f' : /* type double */ if(validFormat==1) { if(pcodeFile!=NULL){ } } break; case 's' : /* type char[] */ if(validFormat==1) { if(pcodeFile!=NULL){ } } break; // case 'c' : /* type char * */ // if(validFormat==1) // { // s = va_arg(p_list, char *); // if(pcodeFile!=NULL){ // fprintf(pcodeFile, "%s", s); // } // printf("%s", s); // } // break; default : /* type char[] */ validFormat=0; break; } if(validFormat==0) { if(pcodeFile!=NULL){ } } format++; } #if(VERBOSE_LEVEL==DEB_NONE) } #endif }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732218065 21/11/2024 20:41:05 |
Warning
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.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 07/03/2010, last modified the 28/10/2018
Source of the printed document:https://www.gaudry.be/en/langages-lsd10-source-rf-project/source//console.c.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
- ↑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.
References
- IHDCB332 - Théorie des langages : Syntaxe et sémantique : PY Schobbens,
Syntaxe et sémantique
(January 2010)
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.