Compilateur LSD010 développé dans le cadre du cours de syntaxe et sémantique[ref 1]
/* * 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 FILE *yyin; extern AstNode *rootNode; extern DebugInfo *debugInfo; /**************************************************************/ /** * 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 */ { 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); } { if(current!=found) { sprintf( 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 { sprintf( 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); } { sprintf( 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) exit(EXIT_SUCCESS); } /** * Generates Yacc error if a type is not recognized * Exposed method */ { sprintf(errorStr, "Unrecognized type=%s (%d) on %s line %d", typeToString(type), type, file, line); onError(errorStr, file, line, NULL); } /** * Print custom message on stdout, depending on the Debug level * Exposed method */ { /*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; } /******************************************************************************/ { FILE * htmlFile; time_t genTime = time(NULL); if(htmlFileName==NULL)htmlFileName=AST_HTML_FILE; if(xmlFileName==NULL)xmlFileName=AST_XML_FILE; htmlFile = fopen(htmlFileName, "w"); if(htmlFile!=NULL) { fprintf(htmlFile, "<html><head><title>AST</title></head><body>\n"); fprintf(htmlFile, "<h1>LSD010 AST</h1>\n"); fprintf(htmlFile, "<p>Document généré le %s par le compilateur LSD0<sup>10</sup></p>", asctime(localtime(&genTime))); printHTMLTree(htmlFile, rootNode, 0); fprintf(htmlFile, "</body></html>\n"); fclose(htmlFile); #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 } htmlFile = fopen(xmlFileName==NULL?AST_XML_FILE:xmlFileName, "w"); if(htmlFile!=NULL) { fprintf(htmlFile, "<lsd010>\n"); fprintf(htmlFile, "<![CDATA[\nDocument généré le %s par le compilateur LSD010\n]]>\n", asctime(localtime(&genTime))); printXMLTree(htmlFile, rootNode, 0); fprintf(htmlFile, "</lsd010>\n"); fclose(htmlFile); #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 */ { printDebugTree(NULL, NULL); } //alternate row { if(scopeStack!=NULL) { altRowSTI=0; if(topItem!=0) { fprintf( htmlFile, //"<table width=\"100%%\">", "<table width=\"100%%\" border=\"1\" cellpadding=\"2\" cellspacing=\"1\" class=\"table\">" ); fprintf( 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>"); fprintf(htmlFile, "</thead><tbody>"); } fprintf( htmlFile, (++altRow%2==0)? "<tr class=\"td\">": "<tr class=\"td2\">" ); fprintf( 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) { fprintf(htmlFile, "</tbody></table>"); } } } { FILE * htmlFile; time_t genTime = time(NULL); htmlFile = fopen(htmlFileName, "w"); if(htmlFile==NULL) { return EXIT_FAILURE; } fprintf(htmlFile, "<html><head><title>Table des symboles</title></head><body>\n"); fprintf(htmlFile, "<h1>Etats de la table des symboles LSD010</h1>\n"); fprintf(htmlFile, "<p>Document généré le %s par le compilateur LSD0<sup>10</sup></p>", asctime(localtime(&genTime))); fclose(htmlFile); return EXIT_SUCCESS; } { //printSymbolsTableItem(); FILE * htmlFile; //time_t genTime = time(NULL); htmlFile = fopen(htmlFileName, "a+"); if(htmlFile==NULL) { return EXIT_FAILURE; } fprintf(htmlFile, "\n</body></html>\n"); fclose(htmlFile); return EXIT_SUCCESS; } // first null index // @todo: define a astnodedebug as parameter to show where // this scope enter/exit was found { if(declarations!=NULL) { FILE * htmlFile; //time_t genTime = time(NULL); htmlFile = fopen(htmlFileName, "a+"); 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>"); fprintf(htmlFile, "</thead><tbody>"); int i; for(i=0;i<TABLES_SIZE;i++) { if(declarations[i]==NULL) { if(startNull==INITIAL_INT) { startNull=i; } } else { if(startNull!=-INITIAL_INT) { fprintf(htmlFile, ++altRowSTI%2==0?"<tr class=\"td\">":"<tr class=\"td2\">"); if(startNull<-i-1) { fprintf(htmlFile, "<td>%d...%d</td><td>NULL</td></tr>", startNull, i-1); } else { fprintf(htmlFile, "<td>%d</td><td>NULL</td></tr>", i-1); } startNull = INITIAL_INT; } fprintf(htmlFile, ++altRowSTI%2==0?"<tr class=\"td\">":"<tr class=\"td2\">"); fprintf(htmlFile, "<td>%d</td><td>",i); SymTableEntry *entry = declarations[i]; while(entry!=NULL) { printScopeStack(entry->scopeStack, htmlFile, 1); entry = entry->next; } fprintf(htmlFile, "</td></tr>"); } } if(startNull!=INITIAL_INT) { fprintf(htmlFile, ++altRowSTI%2==0?"<tr class=\"td\">":"<tr class=\"td2\">"); if(startNull<i-1) { fprintf(htmlFile, "<td>%d...%d</td><td>NULL</td></tr>", startNull, i-1); } else { fprintf(htmlFile, "<td>%d</td><td>NULL</td></tr>", i-1); } startNull = INITIAL_INT; } fprintf(htmlFile, "</tbody></table>"); fclose(htmlFile); return EXIT_SUCCESS; } } else { printMsg(DEB_EXEC,"-------------------------Symbols table is null", __FILE__, __LINE__); } return EXIT_FAILURE; } { 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)]"; } } { 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); } } { 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; } { 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); } } { 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 */ { if(node!=NULL) { fprintf(file, "<li><b>Noeud :</b> %s (%d)\n", typeToString(node->type), node->type); if(node->info!=NULL) { fprintf( 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 ); }else fprintf(file, "<li>Info null (%d)\n",depth); fprintf(file, "</li>\n");/*char *name, int type, int value while(node->rightBrother!=NULL) { printNode(file, node->rightBrother, depth); }*/ }else fprintf(file, "<li>Noeud null (%d)</li>\n",depth); } /** * Prints the AST into the html file * Internal business */ { if(node!=NULL) { fprintf(file, "<ul>\n"); printHTMLNode(file, node, depth); printHTMLTree(file, node->left, depth+1); printHTMLTree(file, node->right, depth+1); fprintf(file, "</ul>\n"); }else fprintf(file, "<ul><li>Noeud null (%d)</li></ul>\n",depth); }/** * Prints an AST node into the xml file * Internal business */ { if(node!=NULL) { fprintf(file, "%s<depth>%d</depth>", typeToString(node->type), depth); fprintf(file, "<memaddress>%p</memaddress>", node); if(node->parent!=NULL) { fprintf(file, "<parent><type>%s</type><pmemaddress>%p</pmemaddress></parent>", typeToString(node->parent->type), node->parent); }else { fprintf(file, "<parent>NULL</parent>"); } if(node->info!=NULL) { fprintf( 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 ); } } else fprintf(file, "null"); }/** * Prints the AST into the xml file * Internal business */ { fprintf(file, "<node%d>\n", depth); printXMLNode(file, node, depth); if(node!=NULL) { printXMLTree(file, node->left, depth+1); printXMLTree(file, node->right, depth+1); } fprintf(file, "</node%d>\n", depth); } /******************************************************************************/ /** * Returns a human readable string from a given type constant value * Expected : one of the DEB_xxx constants * Exposed method */ { 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) sprintf(str, "Undefined constant value '%d'", type); 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) */ { if(PCODE_GENERATION_BYPASS) { return; } va_list p_list; int i; long l; double f; int validFormat; validFormat=0; va_start(p_list, format); #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){ fprintf(pcodeFile, "%d", i); } } break; case 'l': /* type long */ if(validFormat==1) { if(pcodeFile!=NULL){ fprintf(pcodeFile, "%ld", l); } } break; case 'f' : /* type double */ if(validFormat==1) { if(pcodeFile!=NULL){ fprintf(pcodeFile, "%lf", f); } } break; case 's' : /* type char[] */ if(validFormat==1) { if(pcodeFile!=NULL){ fprintf(pcodeFile, "%s", s); } } 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){ fprintf(pcodeFile, "%c",format[0]); } } format++; } #if(VERBOSE_LEVEL==DEB_NONE) } #endif va_end(p_list); }
| Nom | Taille | Modification | |
| _ | Parent | 0 octets|000000000000 | 02/06/2012 10:17:47 |
| Pas de sous-répertoires. | |||
La base de données est temporairement indisponible
Le site rencontre momentanément quelques problèmes...
La base de données est temporairement indisponible (), ce qui explique que de nombreuses fonctions ne soient temporairement pas accessibles (par exemple les liens de navigation, les sommaires, etc.) et que l'affichage des pages soit beaucoup plus lent.
Veuillez réessayer dans quelques minutes (les tests automatiques sont effectués toutes les 15 minutes).
Je vous présente mes excuses pour le désagrément que cela engendre.
Steph.
Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.
Syntaxe et sémantique(Janvier 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.
Recherche (afficher)
Utilisateur (afficher)
Navigation (masquer)
Apparence (afficher)
Stats (afficher)
Citation (masquer)