如何在c ++中将行语句解析为标记? [英] How do I parse a line statement into tokens in c++?

查看:58
本文介绍了如何在c ++中将行语句解析为标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用c ++创建一个解析器程序,这样我就可以将每个单词/符号分成标记。我试图按空格分割它,以后将被'(',')','*','/'等分开。输入Hello World时出现问题,只是d保存。任何帮助,谢谢





I want to create a parser program in c++ so i can split each word / symbol into tokens. Am trying to split it by space, later will be split by '(',')','*','/', etc. I got a problem when input "Hello World", it just "d" which is saved. Any help, Thanks


void main(){
	clrscr();
	char input[100], token[100], *temp[100];
	int lengthLine, i, pos, posTok;

	do{
		cout << ">>> "; cin.getline(input, 256);
		lengthLine = strlen(input);

		posTok = 0;

		for (pos=0;pos<lengthLine;pos++){
			if (input[pos] != ' '){
				strcpy(temp[posTok],&input[pos]);
			}else{
				posTok++;
			}
		}

		for (i=0;i<=posTok;i++){
			cout << temp[posTok] << endl;
		}
	}while(strcmp(input,"exit") != 0);
	getch();
}



[edit]已添加代码块 - OriginalGriff [/ edit]


[edit]Code block added - OriginalGriff[/edit]

推荐答案

问题是你没有打破它 - 看看你的代码。

你声明temp和一个100指针到char值的数组,你使用 strcpy 将整个剩余行复制到其中:

The problem is that you aren't breaking it very well - look at your code.
You declare temp and an array of 100 "pointer-to-char" values, and you use strcpy to copy the whole remaining line into it:
strcpy(temp[posTok],&input[pos]);

但是你永远不会给指针赋值,所以它们都是随机的。

你永远不会终止令牌。

你需要做什么设置temp,但随后使用malloc为每个令牌分配空间。

我要做的是:

1 )创建一个令牌指针数组。

2)使用malloc分配令牌字符串空间,并将第一个char设置为'\0'。将其设置为下一个免费令牌数组插槽。

3)循环输入字符串中的每个字符

3.1)如果是空格,则为'\0'到令牌,并分配一个新的令牌空间,如(2)所示。

3.2)如果不是,将字符复制到令牌。

4)Don'忘记使用免费来释放你分配的内存!



有意义吗?

But...you never assign any values to the pointers, so they are all a bit random.
And you never terminate the "tokens" either.
What you need to do it set up temp, but then use malloc to allocate space for each token as you go.
What I would do is:
1) Create an array of token pointers.
2) Use malloc to allocate a "token string space", and set the first char to '\0'. Set it into the next free token array slot.
3) Loop thorugh each character in the input string
3.1) If it's a space copy a '\0' to the token, and allocate a new token space as in (2).
3.2) If it isn't, copy the character to the token.
4) Don't forget to use free to release the memory you allocated!

Make sense?


这篇关于如何在c ++中将行语句解析为标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆