我尝试存储输入数据并检查哪个是关键字,但是它同时适用于标识符和关键字。我希望它将显示不同的部分标识符是显示标识符和关键字是显示关键字部分。看到以下代码:请帮助我。我已经将输出图片上传到此处,并标记了我想要的内容。Output
#include<stdio.h>
#include<string.h>
void lexicalAnalyzerk(char s[]);
int isIdent(char ch);
int isAlpha(char ch);
int isDisit(char ch);
char A[100], ident[100][32];
int kyi=0, kyj=0, idi=0, idj=0, i=0, j=0;
char *key[32]={"char","int","float","double","short","long","signed","unsigned","if","else","for","do","while",
"switch","case","void","break","continue","return","default","goto","static","sizeof","typedef",
"auto","const","struct","enum","volatile","extern","register","union"};
int main()
{
gets(A);
lexicalAnalyzerk(A);
return 0;
}
void lexicalAnalyzerk(char s[])
{
while(s[i])
{
if(isIdent(s[i]))
{
while(isIdent(s[i]))
{
ident[idi][idj] = s[i];
idj++;
i++;
}
ident[idi][idj] = '\0';
idi++;
idj = 0;
}
else
i++;
}
printf("\nThis Are Identifier:\n");
for(i=0; i<=idi; i++)
{
printf("%s\n", ident[i]);
}
printf("\nThis Are Keyword:\n");
for (j=0; j<=idi; j++) {
for (i=0; i<31; i++) {
if(strcmp(key[i], ident[j])==0){
printf("%s\n", key[i]);
}
}
}
}
int isIdent(char ch)
{
if(isAlpha(ch) || isDisit(ch) || ch == '_')
return 1;
else
return 0;
}
int isAlpha(char ch)
{
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
return 1;
else
return 0;
}
int isDisit(char ch)
{
if(ch>='0' && ch<='9')
return 1;
else
return 0;
}
答案 0 :(得分:0)
到目前为止,您正在将所有输入的字符串打印为标识符。
但是只有在string不是关键字的情况下,才需要将print string作为标识符。
因此修改以下for循环。
printf("\nThis Are Identifier:\n");
for(i=0; i<=idi; i++)
{
printf("%s\n", ident[i]);
}
为
printf("\nThis Are Identifier:\n");
for(j=0; j< idi; j++)
{
for (i=0; i<32; i++)
if(strcmp(key[i], ident[j])==0) break;
if (i == 32)
printf("%s\n", ident[j]);
}
注意:请勿使用
gets
。了解更多信息Why not to use gets and what is the alternative to it。
答案 1 :(得分:0)
我同意Kiran在回答中提到的那样,您的代码将所有字符串打印为标识符。因此,更改for
循环,如下所示打印出标识符:
printf("\nThis Are Identifier:\n");
for(j = 0; j < idi; j++)
{
for (i = 0; i < 32; i++)
if(!strcmp(key[i], ident[j]))
break;
if (i == 32)
printf("%s\n", ident[j]);
}
另一项更改:尽管gets()
有效,但最好不要使用它,因为它被认为是危险的。 Refer to this article saying why gets()
shouldn't be used.
而是使用:scanf("%[^\n]%*c",A);
代替gets()
。
尝试此更新的代码:
#include < stdio.h >
#include < string.h >
void lexicalAnalyzerk(char s[]);
int isIdent(char ch);
int isAlpha(char ch);
int isDisit(char ch);
char A[100], ident[100][32];
int kyi = 0, kyj = 0, idi = 0, idj = 0, i = 0, j = 0;
char * key[32] = {"char", "int", "float", "double", "short", "long", "signed", "unsigned", "if", "else", "for", "do", "while", "switch", "case", "void", "break", "continue", "return", "default", "goto", "static", "sizeof", "typedef", "auto", "const", "struct", "enum", "volatile", "extern", "register", "union"};
int main() {
scanf("%[^\n]%*c", A);
lexicalAnalyzerk(A);
return 0;
}
void lexicalAnalyzerk(char s[]) {
while (s[i]) {
if (isIdent(s[i])) {
while (isIdent(s[i])) {
ident[idi][idj] = s[i];
idj++;
i++;
}
ident[idi][idj] = '\0';
idi++;
idj = 0;
} else
i++;
}
printf("\nThis Are Identifier:\n");
for (j = 0; j < idi; j++) {
for (i = 0; i < 32; i++)
if (strcmp(key[i], ident[j]) == 0) break;
if (i == 32)
printf("%s\n", ident[j]);
}
printf("\nThis Are Keyword:\n");
for (j = 0; j <= idi; j++) {
for (i = 0; i < 31; i++) {
if (strcmp(key[i], ident[j]) == 0) {
printf("%s\n", key[i]);
}
}
}
}
int isIdent(char ch) {
if (isAlpha(ch) || isDisit(ch) || ch == '_')
return 1;
else
return 0;
}
int isAlpha(char ch) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
return 1;
else
return 0;
}
int isDisit(char ch) {
if (ch >= '0' && ch <= '9')
return 1;
else
return 0;
}
输出:
int sum = a + b; This Are Identifier: sum a b This Are Keyword: int