Petit interpréteur en C

Rédigé par BeHuman 2 commentaires
Classé dans : C/C++ Mots clés : interpréteur, C

Aujourd'hui, je vous propose un bout de code en C, vraiment pas compliqué à comprendre. Le but est de vous fournir une base d'un interpréteur de fichier,  qui vous permettras de faire votre petit language script, parseur de paramètres...etc

Source:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Prototypage */
int main(int argc, char *argv[]);
void saveFileInArray(FILE **fichier, char * DATAS, long * SIZE);
void parseDATAS(char * DATAS, long * SIZE);

/* Functions */
void saveFileInArray(FILE **fichier, char * DATAS, long * SIZE) {
        long i=0;
        for(i = 0; i < *SIZE; i++) fscanf(*fichier, "%c", &DATAS[i]);
        fscanf(*fichier, "\0", &DATAS[i+1]);
        fclose(*fichier);
}

void parseDATAS(char * DATAS, long * SIZE) {
    long i=0;
    long numCharCommand=0;
    long numCommand=0;
    long numSpace=0;
    long numSpaceTmp=0;
    for(i = 0; i < *SIZE; i++) {
        if (DATAS[i]== '\n') { // line return
            printf("%c",DATAS[i]);
            printf("-> new commande : n° %lu \n-> chars : %lu \n-> spaces : %lu \n-> position: %lu\n\n",numCommand, numCharCommand, numSpaceTmp, i);
            numCharCommand=0;
            numSpaceTmp=0;
            numCommand++;
        } else if (DATAS[i]== ' ') { // space char
            numCharCommand++;
            numSpace++;
            numSpaceTmp++;
            printf("%c",DATAS[i]);
        } else { // others char
            numCharCommand++;
            printf("%c",DATAS[i]);
        }
    }
    printf("-> total costyle="background:#f8f8f8; border:solid gray; padding:.2em .6em"mmands : %lu \n-> total chars : %lu \n-> total space : %lu\n",numCommand-1, *SIZE, numSpace-1);
}

/* MAIN */
/*Compile: gcc main.c -o out.bin -static*/
int main(int argc, char *argv[]) {
    FILE *fichier;
    fichier=fopen(argv[1], "rb");
    if (fichier != NULL) {
        long i=0;
        fseek (fichier, 0, SEEK_END);
        long SIZE=ftell(fichier);
        fseek (fichier, 0, SEEK_SET);
        char *DATAS= malloc(sizeof(char)*SIZE+1);
        saveFileInArray(&fichier, &DATAS[0], &SIZE);
        parseDATAS(&DATAS[0],&SIZE);
        free(DATAS);
        return 0;
    } else {
        printf("[ERROR] File not found !\n");
        return -1;
    }
}

 

-Dans la fonction main on charge le fichier et on initialise la variable DATAS qui récupèrera les données du fichier.

-Si le fichier existe on va dans la fonction saveFileArray pour charger DATAS avec les données du fichier.

-On ferme le fichier

-On va dans la fonction parseDATAS et on lit DATAS pour analyser son contenue.

On compile le tout avec:

gcc main.c -o nano-interpretor -static
On test avec :
./nano-interpretor main.c
Sortie:
#include <stdio.h>
-> new commande : n° 0 
-> chars : 18 
-> spaces : 1 
-> position: 18

#include <stdlib.h>
-> new commande : n° 1 
-> chars : 19 
-> spaces : 1 
-> position: 38

#include <string.h>
-> new commande : n° 2 
-> chars : 19 
-> spaces : 1 
-> position: 58


-> new commande : n° 3 
-> chars : 0 
-> spaces : 0 
-> position: 59

/* Prototypage */
-> new commande : n° 4 
-> chars : 17 
-> spaces : 2 
-> position: 77

int main(int argc, char *argv[]);
-> new commande : n° 5 
-> chars : 33 
-> spaces : 4 
-> position: 111

void saveFileInArray(FILE **fichier, char * DATAS, long * SIZE);
-> new commande : n° 6 
-> chars : 64 
-> spaces : 8 
-> position: 176

void parseDATAS(char * DATAS, long * SIZE);
-> new commande : n° 7 
-> chars : 43 
-> spaces : 6 
-> position: 220


-> new commande : n° 8 
-> chars : 0 
-> spaces : 0 
-> position: 221

/* Functions */
-> new commande : n° 9 
-> chars : 15 
-> spaces : 2 
-> position: 237

void saveFileInArray(FILE **fichier, char * DATAS, long * SIZE) {
-> new commande : n° 10 
-> chars : 65 
-> spaces : 9 
-> position: 303

        long i=0;
-> new commande : n° 11 
-> chars : 17 
-> spaces : 9 
-> position: 321

        for(i = 0; i < *SIZE; i++) fscanf(*fichier, "%c", &DATAS[i]);
-> new commande : n° 12 
-> chars : 69 
-> spaces : 17 
-> position: 391

        fscanf(*fichier, "\0", &DATAS[i+1]);
-> new commande : n° 13 
-> chars : 44 
-> spaces : 10 
-> position: 436

        fclose(*fichier);
-> new commande : n° 14 
-> chars : 25 
-> spaces : 8 
-> position: 462

}
-> new commande : n° 15 
-> chars : 1 
-> spaces : 0 
-> position: 464


-> new commande : n° 16 
-> chars : 0 
-> spaces : 0 
-> position: 465

void parseDATAS(char * DATAS, long * SIZE) {
-> new commande : n° 17 
-> chars : 44 
-> spaces : 7 
-> position: 510

    long i=0;
-> new commande : n° 18 
-> chars : 13 
-> spaces : 5 
-> position: 524

    long numCharCommand=0;
-> new commande : n° 19 
-> chars : 26 
-> spaces : 5 
-> position: 551

    long numCommand=0;
-> new commande : n° 20 
-> chars : 22 
-> spaces : 5 
-> position: 574

    long numSpace=0;
-> new commande : n° 21 
-> chars : 20 
-> spaces : 5 
-> position: 595

    long numSpaceTmp=0;
-> new commande : n° 22 
-> chars : 23 
-> spaces : 5 
-> position: 619

    for(i = 0; i < *SIZE; i++) {
-> new commande : n° 23 
-> chars : 32 
-> spaces : 11 
-> position: 652

        if (DATAS[i]== '\n') {
-> new commande : n° 24 
-> chars : 30 
-> spaces : 11 
-> position: 683

            printf("%c",DATAS[i]);
-> new commande : n° 25 
-> chars : 34 
-> spaces : 12 
-> position: 718

            printf("-> new commande : n° %lu \n-> chars : %lu \n-> spaces : %lu \n-> position: %lu\n\n",numCommand, numCharCommand, numSpaceTmp, i);
-> new commande : n° 26 
-> chars : 149 
-> spaces : 31 
-> position: 868

            numCharCommand=0;
-> new commande : n° 27 
-> chars : 29 
-> spaces : 12 
-> position: 898

            numSpaceTmp=0;
-> new commande : n° 28 
-> chars : 26 
-> spaces : 12 
-> position: 925

            numCommand++;
-> new commande : n° 29 
-> chars : 25 
-> spaces : 12 
-> position: 951

        } else if (DATAS[i]== ' ') {
-> new commande : n° 30 
-> chars : 36 
-> spaces : 14 
-> position: 988

            numCharCommand++;
-> new commande : n° 31 
-> chars : 29 
-> spaces : 12 
-> position: 1018

            numSpace++;
-> new commande : n° 32 
-> chars : 23 
-> spaces : 12 
-> position: 1042

            numSpaceTmp++;
-> new commande : n° 33 
-> chars : 26 
-> spaces : 12 
-> position: 1069

            printf("%c",DATAS[i]);
-> new commande : n° 34 
-> chars : 34 
-> spaces : 12 
-> position: 1104

        } else {
-> new commande : n° 35 
-> chars : 16 
-> spaces : 10 
-> position: 1121

            numCharCommand++;
-> new commande : n° 36 
-> chars : 29 
-> spaces : 12 
-> position: 1151

            printf("%c",DATAS[i]);
-> new commande : n° 37 
-> chars : 34 
-> spaces : 12 
-> position: 1186

        }
-> new commande : n° 38 
-> chars : 9 
-> spaces : 8 
-> position: 1196

    }
-> new commande : n° 39 
-> chars : 5 
-> spaces : 4 
-> position: 1202

    printf("-> total commands : %lu \n-> total chars : %lu \n-> total space : %lu\n",numCommand-1, *SIZE, numSpace-1);
-> new commande : n° 40 
-> chars : 118 
-> spaces : 20 
-> position: 1321

}
-> new commande : n° 41 
-> chars : 1 
-> spaces : 0 
-> position: 1323


-> new commande : n° 42 
-> chars : 0 
-> spaces : 0 
-> position: 1324

/* MAIN */
-> new commande : n° 43 
-> chars : 10 
-> spaces : 2 
-> position: 1335

/*Compile: gcc main.c -o out.bin -static*/
-> new commande : n° 44 
-> chars : 42 
-> spaces : 5 
-> position: 1378

int main(int argc, char *argv[]) {
-> new commande : n° 45 
-> chars : 34 
-> spaces : 5 
-> position: 1413

    FILE *fichier;
-> new commande : n° 46 
-> chars : 18 
-> spaces : 5 
-> position: 1432

    fichier=fopen(argv[1], "rb");
-> new commande : n° 47 
-> chars : 33 
-> spaces : 5 
-> position: 1466

    if (fichier != NULL) {
-> new commande : n° 48 
-> chars : 26 
-> spaces : 8 
-> position: 1493

        long i=0;
-> new commande : n° 49 
-> chars : 17 
-> spaces : 9 
-> position: 1511

        fseek (fichier, 0, SEEK_END);
-> new commande : n° 50 
-> chars : 37 
-> spaces : 11 
-> position: 1549

        long SIZE=ftell(fichier);
-> new commande : n° 51 
-> chars : 33 
-> spaces : 9 
-> position: 1583

        fseek (fichier, 0, SEEK_SET);
-> new commande : n° 52 
-> chars : 37 
-> spaces : 11 
-> position: 1621

        char *DATAS= malloc(sizeof(char)*SIZE+1);
-> new commande : n° 53 
-> chars : 49 
-> spaces : 10 
-> position: 1671

        saveFileInArray(&fichier, &DATAS[0], &SIZE);
-> new commande : n° 54 
-> chars : 52 
-> spaces : 10 
-> position: 1724

        parseDATAS(&DATAS[0],&SIZE);
-> new commande : n° 55 
-> chars : 36 
-> spaces : 8 
-> position: 1761

        free(DATAS);
-> new commande : n° 56 
-> chars : 20 
-> spaces : 8 
-> position: 1782

        return 0;
-> new commande : n° 57 
-> chars : 17 
-> spaces : 9 
-> position: 1800

    } else {
-> new commande : n° 58 
-> chars : 12 
-> spaces : 6 
-> position: 1813

         printf("[ERROR] File not found !\n");
-> new commande : n° 59 
-> chars : 46 
-> spaces : 13 
-> position: 1860

        return -1;
-> new commande : n° 60 
-> chars : 18 
-> spaces : 9 
-> position: 1879

    }
-> new commande : n° 61 
-> chars : 5 
-> spaces : 4 
-> position: 1885

}
-> new commande : n° 62 
-> chars : 1 
-> spaces : 0 
-> position: 1887

-> total commands : 62 
-> total chars : 1888 
-> total space : 485

2 commentaires

#1  - Attari a dit :

C'est intéressant vos exemples et bien expliqués.

Répondre
#2  - Mathias Bourgogne a dit :

Tu fais du boulot super

Répondre

Écrire un commentaire

Quelle est la deuxième lettre du mot g6nctwfv ?

Fil RSS des commentaires de cet article