c - C 中的智能分词器

标签 c tokenize

我必须用 c/c++ 编写一个标记生成器,以便我必须解析以下形式的字符串

char pSignature[] = "work.\\top =>\\p1 =:5:p2=:10:=>interface_ports:=dut";

并填充\p1 5 和 p2 10 等对。有人可以建议我任何好的方法。使用 strtok 的问题是如何在 =>interface_ports 发生之前结束它。 下面是我写的代码:

int main() {
  char pSignature[] = "work.\\top =>\\p1 =:5:p2=:10:=>interface_ports:=dut";
  char* mParamName = NULL;
  char* mParamVal = NULL;
  char* sTemp = pSignature;
  bool bIsLibState = true;
  bool bIsModState = false;
  bool bIsEscaped = false;
  while (*sTemp != '\0') {
    // Extract library ..
    if (bIsLibState) {
      if (*sTemp == '.') {
        bIsLibState = false;
        bIsModState = true;
      }
      sTemp++;
    }
    else if (bIsModState) {
    // Extract moduleName..
      if (*sTemp == '\\') {
        bIsEscaped = true;
      }
      if (bIsEscaped) {
        if (*sTemp == ' ') {
          bIsModState = false;
          bIsEscaped = false;
          sTemp++;
          sTemp += 2;
          break;
        }
        else 
          sTemp++;
      }
      else {
        if (*(sTemp+1) == '=' && *(sTemp+2) == '>') {
          bIsModState = false;
          sTemp++;
          sTemp += 2;
          break;
        }
        else
          sTemp++;
      }
    }
  }

  char* tmp = sTemp;
  char* mStr = sTemp;
  bool bEscaped = false;
  while(tmp != NULL)
  {
    if (*tmp == '\\') {
      tmp = strtok(mStr, " ");
        bEscaped  = true;
    }
    else
      tmp = strtok(mStr, "=:");
    if (!strcmp(tmp,">interface_ports"))
      break;
    mStr = NULL;
    mParamName = tmp;

    tmp = strtok(mStr, "=:");
    if (!strcmp(tmp,">interface_ports"))
      break;
    mParamVal = tmp;
    cout << mParamName <<"  " << mParamVal << endl;
    //if (mParamName && mParamVal) {
    //  symCharPair* paramPair = new symCharPair(VeIntern(mParamName), mParamVal);
    //  pParamValueList->AddTail(paramPair);
    //}
  }
return 0;
}

最佳答案

如果您的输入字符串始终采用这种形式

work.\\top =>\\p1 =:5:p2=:10:=>interface_ports:=dut

那么你可以做一些更简单的事情:

 #include <string.h>

 const char *input = "work.\\top =>\\p1 =:5:p2=:10:=>interface_ports:=dut";

 // find first occurrence of "=>"
 const char *start = strstr(input, "=>");

 // find first occurrence of ":=>"
 const char *end= strstr(input, ":=>");

 if (start == NULL || end == NULL)
     exit(-1);

 int length = end - start - 2 ; // the -2 is to skip the "=>"
 char *pairs = malloc(length + 1); // +1 for the terminating \0
 strncpy(pairs, start + 2, length);

现在 pairs 应该包含 \p1 =:5:p2=:10 您可能可以更轻松地处理它。

关于c - C 中的智能分词器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8748295/

相关文章:

c - 在 C 中使用宏处理操作

python - 变形金刚 : Asking to pad but the tokenizer does not have a padding token

编程语言解析器的 Java 字符串标记化

c - 当编译时宽度未知时,如何编写接受二维数组的函数?

python - 值错误 : cannot reshape array of size 3800 into shape (1, 200)

c++ - 从字符串中提取数字 C++

javascript - 除非转义,否则 RegEx 不允许字符

c++ - 尝试使用 libjpeg 将 YUV 图像压缩为 jpeg 时出现奇怪的图像

我可以在 dma_sync_single_for_cpu 之后调用 dma_unmap_single 吗?

c++ - 图像数据作为 gstreamer 中的源