Skip to content

Latest commit

 

History

History
43 lines (36 loc) · 1.03 KB

.md

File metadata and controls

43 lines (36 loc) · 1.03 KB

1. Demonstrate a lexical analyzer for given language. Although the syntax specification states that identifiers can be arbitrarily long, you may restrict the length to some reasonable value. Simulate the same in C language.

#include <stdio.h>
#include <string.h>
int main()
{
  char keywords[32][8] = {
      "auto", "double", "int", "struct", "break", "else", "long",
      "switch", "case", "enum", "register", "typedef", "char",
      "extern", "return", "union", "const", "float", "short",
      "unsigned", "continue", "for", "signed", "void", "default",
      "goto", "sizeof", "volatile", "do", "if", "static", "while"};
  char input[6];
  int i, flag = 0;

  printf("Enter value : ");
  scanf("%s", input);

  for (i = 0; i < 32; i++)
    if (strcmp(input, keywords[i]) == 0)
      flag = 1;

  if (flag == 1)
    printf("%s is a keyword.", input);
  else
    printf("%s is not a keyword.", input);
}

Output :-
Enter value : int 
int is a keyword.

Enter value : okay     
okay is not a keyword.