在C中为LC3汇编程序编写自定义标记生成器

时间:2016-06-18 00:59:08

标签: c file-io tokenize lc3

嗨我应该为C中的类编写一个lc3汇编程序(??),但我仍然坚持编写tokenizer,代码很干净没有警告,但它给了我分段错误。在第一行之后。

考虑\ n \ r和;和新行跳过评论, 考虑除字母数字或空格错误以外的任何内容

(第一次海报顺便说一下。真的很喜欢这里的人们!希望有一天我能够做出足够的贡献!)

感谢所有人。

文件输入(run.asm)

.ORIG 0X3000
HALT

分词器。

// 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 = '\0';
    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] = '\0';
                freshLine = false;
                freshToken = false;
                k=0;
                j= 0;
                i++;
                printf("\n");
            }
            else if(freshToken){
                code[i][j][k] = '\0';
                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;
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

如果您有分段错误,我可以推荐工具来调查错误。

此程序包含您的函数Tokenize(未更改),并且编译和运行时没有错误。

#include <stdio.h>
#include <stdbool.h>

// 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 = '\0';
    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] = '\0';
                freshLine = false;
                freshToken = false;
                k = 0;
                j = 0;
                i++;
                printf("\n");
            }
            else if (freshToken) {
                code[i][j][k] = '\0';
                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;
        }
    }
}

int main(int argc, char **argv) {
    int ch;

}

您可以使用分析程序并确认没有段错误:

~/C/gnu> valgrind ./a.out 
==18584== Memcheck, a memory error detector
==18584== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18584== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==18584== Command: ./a.out
==18584== 
==18584== 
==18584== HEAP SUMMARY:
==18584==     in use at exit: 0 bytes in 0 blocks
==18584==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==18584== 
==18584== All heap blocks were freed -- no leaks are possible
==18584== 
==18584== For counts of detected and suppressed errors, rerun with: -v
==18584== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)