-2

i am try to store the input data and check which is keyword but it is working both Identifier and Keyword. i want it will show different section identifier are show identifier and keyword are show keyword section. see this code: Plese help me anyone. i have upload the output picture here and mark this what i want.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;
}
Rajus Lab
  • 1
  • 1

2 Answers2

0

As of now you are printing all the inputted strings as identifiers.

But you need print string as identifier only when string is not a keyword.

Hence modify the below for loop.

printf("\nThis Are Identifier:\n");
for(i=0; i<=idi; i++)
{
    printf("%s\n", ident[i]);
}  

as

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]);
}

Note: Don't use gets. Read more info Why not to use gets and what is the alternative to it.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • 1
    Good answer Kiran... Just another suggestion... OP has used `gets`, hence just add on to your answer an alternative to it, since the compilers these days throw a warning as it its use is considered dangerous. – Harshith Rai Dec 04 '18 at 11:26
  • Brother can you help one thing my exam is tomorrow for that. i want to show identifier and the constant also ex: 100, 10.0 etc. same code before you answer just implement the constant how to works. please help i am very happy for your help. Full code here: http://lander.fun/constant_lexical.c Thanks – Rajus Lab Dec 04 '18 at 14:31
  • @kiranBiradar i want to show the constant like 100/10.0 this is constant and 123abc/ avb123 this are identifier. got it bro? – Rajus Lab Dec 04 '18 at 14:47
0

I agree with Kiran as mentioned in his answer, your code prints all the strings as identifiers. Hence change the for loop which prints out the identifiers as follows:

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]);
}

And another change: though gets() works, it is preferred to not use it, since it is considered dangerous. Refer to this article saying why gets() shouldn't be used. Instead use: scanf("%[^\n]%*c",A); in place of gets().

try this updated code:

#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;
}

the OUTPUT:

int sum = a + b;

This Are Identifier:
sum
a
b

This Are Keyword:
int
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
  • Brother can you help one thing my exam is tomorrow for that. i want to show identifier and the constant also ex: 100, 10.0 etc. same code before you answer just implement the constant how to works. please help i am very happy for your help. Full code here: http://lander.fun/constant_lexical.c Thanks – Rajus Lab Dec 04 '18 at 14:29