从字符串标记字符串时出错

发布于 2024-11-19 18:17:19 字数 1333 浏览 4 评论 0原文

我想从 SIC 汇编代码生成符号表。为此,我尝试将每个字符串与汇编代码分开。 因此,在尝试第一步时,我尝试了这段代码。

在这里, 我尝试过的是逐行读取文件中的汇编代码。然后将行中的字符串分隔为标记。

当我删除标记分隔部分(如代码中所述)时,我将按预期打印所有行。 但是,如果我与令牌分离一起运行,则第一行将被读取,并且令牌将按照我的预期进行分离。但对于第二行,我收到了分段错误错误。 我无法追踪我哪里出错了。

所以,我需要专家的解释。

提前致谢。

 FILE* sourceCode = fopen("/home/muthu/LangFiles/SIC/PASS1/PROGRAM.txt","r");
  if(checkForFileOpeningErrors() == ERROR) //我使用单独的函数
      返回 EXIT_FAILURE; //终止程序。
  int 最大线长度 = 50;
  同时(1)
  {
           字符* lineReader = NULL; // 因为 getline 将重新分配。
       if( getline(&lineReader,(size_t*)&maxLineLength,sourceCode) == -1 )
           休息;
       printf("%s",lineReader);

// 令牌分离从这里开始....如果我注释掉这一部分,我就会打印所有行 
            字符*字读器;
      wordReader = strtok(lineReader,"\n");
      printf("%s\n",wordReader);
      同时(1)
      {
           wordReader = strtok(NULL,"\n");
           printf("%s\n",wordReader);
      }
// 令牌分离到此结束...
  }

我的文件:

COPY START 1000  
FIRST STL RETADR 
CLOOP JSUB RDREC  
LDA LENGTH
COMP ZERO
JEQ ENDFIL
JSUB WRREC 
J CLOOP
.
.
.
END

我的示例输出:

muthu@muthu-G31M-ES2L:~/LangFiles/PASS1$ ./a.out
All Files successfully opened!! Operation has begun...
COPY START 1000 

COPY
START
1000
segmentation Fault.

I want to generate a symbol table from a SIC Assembly code. For that I have tried to separate every strings form the assembly code.
Hence while attempting first step for that, I tried this code.

Here,
What I have tried is to read the assembly code form a file line by line. And then to separate the strings in the line to tokens.

When I remove the token separation section (as mentioned in the code) I am getting all lines printed as expected.
But If I run along with token separation, the first line is getting read and the tokens are separated as I expected. But for the second line I am getting error as segmentation fault.
I couldn't trace where I went wrong.

So, I need explanations form experts.

Thanks in advance.

 FILE* sourceCode =  fopen("/home/muthu/LangFiles/SIC/PASS1/PROGRAM.txt","r");
  if(checkForFileOpeningErrors() == ERROR)  //Iam using separate function
      return EXIT_FAILURE;  //Terminate the program.
  int maxLineLength = 50;
  while(1)
  {
           char* lineReader = NULL;  // since getline will reallocate.
       if( getline(&lineReader,(size_t*)&maxLineLength,sourceCode) == -1 )
           break;
       printf("%s",lineReader);

// TOKEN SEPARATION STARTS HERE....   If I comment this section out iam getting all lines printed 
            char* wordReader;
      wordReader = strtok(lineReader," \n");
      printf("%s\n",wordReader);
      while(1)
      {
           wordReader = strtok(NULL," \n");
           printf("%s\n",wordReader);
      }
// TOKEN SEPARATION ENDS HERE....
  }

My FILE:

COPY START 1000  
FIRST STL RETADR 
CLOOP JSUB RDREC  
LDA LENGTH
COMP ZERO
JEQ ENDFIL
JSUB WRREC 
J CLOOP
.
.
.
END

My Sample output:

muthu@muthu-G31M-ES2L:~/LangFiles/PASS1$ ./a.out
All Files successfully opened!! Operation has begun...
COPY START 1000 

COPY
START
1000
segmentation Fault.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲欢浪云 2024-11-26 18:17:19

您预计该循环何时终止?

  while(1)
  {
       wordReader = strtok(NULL," \n");
       printf("%s\n",wordReader);
  }

When do you expect this loop to terminate?

  while(1)
  {
       wordReader = strtok(NULL," \n");
       printf("%s\n",wordReader);
  }
终难遇 2024-11-26 18:17:19

呃,你使用了 strtok() 错误。当找不到更多令牌时,它将返回 NULL,并且您不应该尝试打印它。另外,你的循环是永无止境的,这将是一个问题。

循环可能看起来像这样,因为现在,您在每次迭代时将相同的参数传递给 strtok() ,这当然永远不会成功:

for(wordReader = strtok(lineReader, " \n");
    wordReader != NULL;
    wordReader = strtok(NULL, " \n"))
{
   printf("found token: '%s'\n", wordReader);
}

Uh, you're using strtok() wrong. It will return NULL when no more tokens are found, and you shouldn't try to print that. Also, your loop is never-ending, which will be a problem.

The loop should probably look something like this, since as-is now, you pass the same argument to strtok() on every iteration, which will of course never succeed:

for(wordReader = strtok(lineReader, " \n");
    wordReader != NULL;
    wordReader = strtok(NULL, " \n"))
{
   printf("found token: '%s'\n", wordReader);
}
峩卟喜欢 2024-11-26 18:17:19

您永远不会跳出 while(1) 循环,因此它会尝试永远继续下去,但当 strtok 返回 NULL 时会导致分段错误。

您需要检查 strtok 是否返回 NULL,并在此时跳出循环。

You never break out of the while(1) loop, so it tries to go on forever, but causes a segmentation fault when strtok returns NULL.

You need to check whether strtok returns NULL, and break out of the loop at that point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文