CD Rec
CD Rec
No: 710721104116
Ex. No: Develop a Lexical Analyzer to recognize a few patterns in c. Create aSymbol
Date: Table while recognizing identifiers
AIM:
To develop a Lexical Analyzer to recognize a few patterns in c. Create a Symbol Table
while recognizing identifiers.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
int isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
int isIdentifierChar(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
}
};
int i;
for (i = 0; i < 32; i++) {
if (strcmp(str, keywords[i]) == 0)
{
return 1;
}
}
return 0;
}
if (isIdentifierChar(code[i])) {
int j = i + 1;
while (j < length && isIdentifierChar(code[j]))
{
j++;
}
tokenLength = j - i + 1;
token = (char*)malloc(tokenLength * sizeof(char));
strncpy(token, code + i, j - i);
token[j - i] = '\0';
if (isKeyword(token)) {
printf("| %-12s | Keyword |\n", token);
}
else
{
printf("| %-12s | Identifier |\n", token);
}
free(token);
i = j;
continue;
}
switch (code[i]) {
case '+':
2
Dr. N. G. P. Institute of Technology Reg. No: 710721104116
3
Dr. N. G. P. Institute of Technology Reg. No: 710721104116
71072110411
OUTPUT:
RESULT:
Thus, the above c program to develop a lexical analyzer to recognize few patterns and create a
symbol table while recognizing identifiers was written and executed successfully.
4