\\p1 =:5:p2=:10:=>interface_p-6ren">
gpt4 book ai didi

c - C 中的智能分词器

转载 作者:行者123 更新时间:2023-11-30 20:28:18 24 4
gpt4 key购买 nike

我必须用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com