Sunday 10 January 2021

Implementation of LEXICAL ANALYZER for IF STATEMENT with the help of Lex Tool in C

 Implementation of LEXICAL ANALYZER for IF STATEMENT with the help of Lex Tool-

C Program-

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> int isKeyword(char buffer[]) { char keywords[32][10] = { "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while" }; int i, flag = 0; for (i = 0; i < 32; ++i) { if (strcmp(keywords[i], buffer) == 0) { flag = 1; break; } } return flag; } int main() { char ch, buffer[15], operators[] = "+-*/%="; FILE * fp; int i, j = 0; fp = fopen("program.txt", "r"); if (fp == NULL) { printf("error while opening the file\n"); exit(0); } while ((ch = fgetc(fp)) != EOF) { for (i = 0; i < 6; ++i) { if (ch == operators[i]) printf("%c is operator\n", ch); } if (isalnum(ch)) { buffer[j++] = ch; } else if ((ch == ' ' || ch == '\n') && (j != 0)) { buffer[j] = '\0'; j = 0; if (isKeyword(buffer) == 1) printf("%s is keyword\n", buffer); else printf("%s is indentifier\n", buffer); } } fclose(fp); return 0; }

No comments:

Post a Comment

The Future of Web Development: Why Next.js is Going Viral

  Are you ready to level up your web development game? Look no further than Next.js, the latest sensation in the world of web development th...