jeudi 7 juillet 2016

Writing a custom tokenizer in C for an LC3 Assembler

Hi I'm supposed to write an lc3 assembler (??) for class in C but I'M stuck at writing the tokenizer, the code is clean no warnings but it gives me segmentation fault. after the first line.

consider n r and ; and new line to skip comments, consider anything other than alphanumeric or , or space wrong

(first time poster btw. really love the people here! hopefully i'll get good enough to contribute someday!)

thanks to all.

file input (run.asm)

.ORIG 0X3000
HALT

tokenizer.

// 512 lines max
// 10 token max
// 32 char tokean max.
char code[512][10][32];


//skips comment,
void Tokenize(FILE *fileIn){
    int i = 0;
    int j = 0;
    int k = 0;
    char in = '�';
    bool freshLine = false;
    bool freshToken = false;

    while((in = fgetc(fileIn)) != EOF){

        if((in >= 'A' && in <= 'Z') || (in >= 'a' && in <= 'z') || (in >= '0' && in <= '9') || in == '-' || in == '.'){
            if(freshLine){
                code[i][j][k] = '�';
                freshLine = false;
                freshToken = false;
                k=0;
                j= 0;
                i++;
                printf("n");
            }
            else if(freshToken){
                code[i][j][k] = '�';
                freshToken = false;
                k=0;
                j++;
                printf(" ^ ");
            }
            code[i][j][k++] = in;
            printf("%c", in);
        }
        else if(in == ';' || in == 'n' || in == 'r'){
            freshLine = true;
        }
        else{
            freshToken = true;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire