#include #include #include #include typedef int transition[20][256]; transition T; typedef char etat[20]; char TMP[80], Lexeme[30], UL[20],Error[256],Mesg[256]; int ptrdeb = 0, ptrfin = 0,ALEC=1; // initialisation des pointeurs et de la variable d'aythorisation de lecture // prototype des fonctions int lex(); void getlex(); void init(transition *T); int instr_SQL(); int inster_create(); int mot_cle_table(); int id(); int chaine(); int liste_id(); int mot_cle_insert(); int mot_cle_into(); int mot_cle_values(); int list_para(); int list_args(); int ParOpen(); int ParClose(); int Virgule(); int Scolon(); int constant(); int mot_cle_type(); //*****************************************ANALYSE LEXICALE********************************************************* //AUTOMATE FINI DETERMINISTE int AFD(transition T, etat F){ int e=0; while ( (T[e][TMP[ptrfin]] != -1) && (ptrfin (virgule id mot_cle_type)* -------Verifier la syntaxe de l'instruction create s'il y a plusieurs id entre les parentheses--------*/ int list_para()// parametres de create est une liste de types avec id { getlex(); if(!strcmp(UL,",")) { getlex(); if(!strcmp(UL,"id")) { getlex(); if(!strcmp(UL,"type")) { if(list_para()) return 1; } } return 0; } ALEC=0; return 1; } int list_args()// arguments de insert ou on a une liste de id { getlex(); if(!strcmp(UL,",")) { getlex(); if(!strcmp(UL,"id")) { if(list_args()) return 1; } return 0; } ALEC=0; return 1; } int list_const() { getlex(); if(!strcmp(UL,",")) { getlex(); if(!strcmp(UL,"const")) { if(list_const()) return 1; } return 0; } ALEC=0; return 1; } //-------------------------------------- Grammaire Create :-------------------------------- int instr_create() { int i=0; getlex(); if(!strcmp(UL,"create")) { getlex(); if(!strcmp(UL,"table")) { printf("%s",UL); getlex(); if(!strcmp(UL,"id")) { printf("%s",UL); strcpy(Mesg,UL); getlex(); if(!strcmp(UL,"(")) { printf("%s",UL); getlex(); if(!strcmp(UL,"id")) { printf("%s",UL); getlex(); if(!strcmp(UL,"type")) { printf("%s",UL); if(list_para()) { printf("%s",UL); getlex(); if(!strcmp(UL,")")) { printf("%s",UL); getlex(); if(!strcmp(UL,";")) { printf("%s",UL);return 1;} } } } } } } } } return 0; } //-------------------------------------- Grammaire Insert:-------------------------------- int instr_insert() { getlex(); if(!strcmp(UL,"insert")) { getlex(); if(!strcmp(UL,"into")) { getlex(); if(!strcmp(UL,"id")) { getlex(); if(!strcmp(UL,"(")) { getlex(); if(!strcmp(UL,"id")) { if(list_args()) { strcpy(Error,"Erreur syntaxe insert: mot cle VALUES absent"); getlex(); if(!strcmp(UL,")")) { getlex(); if(!strcmp(UL,"values")) { strcpy(Error,"Erreur syntaxe insert: expression absente apres VALUES"); getlex(); if(!strcmp(UL,"(")) { getlex(); if(!strcmp(UL,"const")) { if(list_const()) { getlex(); if(!strcmp(UL,")")) { getlex(); if(!strcmp(UL,";")) return 1; } } else// si on a une seule constante { getlex(); if(!strcmp(UL,")")) { getlex(); if(!strcmp(UL,";")) return 1; } } // fin else const } } } } } // fin plusieurs arguments else // si on a un seul id { getlex(); if(!strcmp(UL,")")) { getlex(); if(!strcmp(UL,"values")) { strcpy(Error,"Erreur syntaxe insert: expression absente apres VALUES"); getlex(); if(!strcmp(UL,"(")) { getlex(); if(!strcmp(UL,"const")) { if(list_const()) { getlex(); if(!strcmp(UL,")")) { getlex(); if(!strcmp(UL,";")) return 1; } } else// si on a une seule constante { getlex(); if(!strcmp(UL,")")) { getlex(); if(!strcmp(UL,";")) return 1; } } //fin d else const } } } } } // fin d else id } } } strcpy(Error,"veuillez spécifier un nom de table"); } //strcpy(Error,"mot cle INTO manquant"); } ALEC=0; return 0; } //------------------ Grammaire: instr_SQL()->instr_create() | instr_insert() ------------------ //------------------ Verifier si la chaine saisie equivalent à une instruction sql valide---------- int instr_SQL() { if(instr_create()) return 1; else { ALEC=0; if(instr_insert()) return 1; } return 0; } //--------------------------------fonction main ---------------------------------------- int main() { printf("Exécution TD1:Instruction SQL(exemple de create et insert)\n\n\n\n\n\n\n"); printf("Veillez taper votre requete: \n"); //gets(TMP);//lire les caracteres saisi au clavier et les stockés dans TMP fgets(TMP,80,stdin); ptrdeb=0;ptrfin=0;ALEC=1; if(instr_create()) { ptrdeb=0;ptrfin=0;ALEC=1;//intialiation des variables car il sont modifiés par instr_SQL() printf("===> La table %s a été créee\n\n",UL[2]); } else printf("===> %s\n\n",Error); system("pause"); return 0; getch(); }