在 c 中使用标记化

发布于 2024-11-05 06:03:48 字数 839 浏览 0 评论 0原文

我正在尝试标记一条线并将其放入二维数组中,到目前为止我已经想出了这个,但我觉得我还很遥远:

/**
 * Function to tokenize an input line into seperate tokens
 *
 * The first arg is the line to be tokenized and the second arg points to
 * a 2-dimentional string array. The number of rows of this array should be
 * at least MAX_TOKENS_PER_LINE size, and the number of columns (i.e., length
 * of each string should be at least MAX_TOKEN_SIZE)
 *
 * Returns 0 on success and negative number on failure
 */

int __tokenize(char *line, char tokens[][MAX_TOKEN_SIZE], int *num_tokens){

char *tokenPtr;
tokenPtr = strtok(line, " \t");
    for(int j =0; j<MAX_TOKEN_SIZE; j++){
      while(tokenPtr != NULL){
        if(!(tokens[][j] = tokenPtr)){return -1;}
            num_tokens++;
            tokenPtr = strtok(NULL, " \t");
        }
    }
  return 0;
}

I am trying to tokenize a line and put it into a two dimensional array so far I have come up with this but I feel I am far off:

/**
 * Function to tokenize an input line into seperate tokens
 *
 * The first arg is the line to be tokenized and the second arg points to
 * a 2-dimentional string array. The number of rows of this array should be
 * at least MAX_TOKENS_PER_LINE size, and the number of columns (i.e., length
 * of each string should be at least MAX_TOKEN_SIZE)
 *
 * Returns 0 on success and negative number on failure
 */

int __tokenize(char *line, char tokens[][MAX_TOKEN_SIZE], int *num_tokens){

char *tokenPtr;
tokenPtr = strtok(line, " \t");
    for(int j =0; j<MAX_TOKEN_SIZE; j++){
      while(tokenPtr != NULL){
        if(!(tokens[][j] = tokenPtr)){return -1;}
            num_tokens++;
            tokenPtr = strtok(NULL, " \t");
        }
    }
  return 0;
}

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

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

发布评论

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

评论(3

浮云落日 2024-11-12 06:03:48
int __tokenize(char *line, char tokens[][MAX_TOKEN_SIZE], int *num_tokens)
{
char *tokenPtr;
tokenPtr = strtok(line, " \t");
for (int i = 0; tokenPtr; i++)
{
            tokens[i] = tokenPtr;
            tokenPtr = strtok(NULL, " \t");
}
}

希望这应该有效。

int __tokenize(char *line, char tokens[][MAX_TOKEN_SIZE], int *num_tokens)
{
char *tokenPtr;
tokenPtr = strtok(line, " \t");
for (int i = 0; tokenPtr; i++)
{
            tokens[i] = tokenPtr;
            tokenPtr = strtok(NULL, " \t");
}
}

Hope this should work.

请别遗忘我 2024-11-12 06:03:48

你应该实现一个有限状态机,我刚刚完成我的 shell 命令 Lexer/Parser (LL)
看:如何手动编写(shell)词法分析器

You should implement a finite state machine, I've just finish my shell command Lexer/Parser (LL)
Look : How to write a (shell) lexer by hand

贪了杯 2024-11-12 06:03:48
  1. tokenPtr 未初始化 - 第一次循环时它可能为 NULL,也可能不是 NULL。
  2. strtok 有 2 个参数。如果您想拆分多个字符,请将它们全部包含在第二个字符串中。
  3. strtok调用之后,token指针指向你想要的字符串。现在怎么办?你需要一个地方来存放它。 也许是一个 char* 数组? 或者是一个二维字符数组,如您编辑的原型中
  4. tokens[i] 是 MAX_TOKEN_SIZE 个字符的存储空间。 strtok() 返回一个指向字符串(1 个或多个字符的序列)的指针。您需要将一个复制到另一个中。
  5. 内循环完成了什么?

请注意,char tokens[][MAX] 通常称为二维字符数组。 (或固定长度字符串的一维数组)。二维字符串数组将是 char* tokens[][MAX]

  1. tokenPtr is not initialized - it may or may not be NULL the first time through the loop.
  2. strtok takes 2 arguments. If you want to split on multiple chars, include them all in the 2nd string.
  3. After the strtok call, token pointer points to the string you want. Now what? You need somewhere to store it. Perhaps an array of char*? Or an 2d array of characters, as in your edited prototype.
  4. tokens[i] is storage for MAX_TOKEN_SIZE characters. strtok() returns a pointer to a string (a sequence of 1 or more characters ). You need to copy one into the other.
  5. What is the inner loop accomplishing?

Note that char tokens[][MAX] is usually referred to as a 2-D array of characters. (or a 1-D array of fixed-length strings). A 2-D array of strings would be char* tokens[][MAX]

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