0% found this document useful (0 votes)
31 views4 pages

CD Rec

The document describes developing a lexical analyzer in C to recognize patterns in code and create a symbol table while recognizing identifiers. It includes steps to identify tokens, keywords, operators and delimiters and analyze the code by printing the tokens and their types to the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

CD Rec

The document describes developing a lexical analyzer in C to recognize patterns in code and create a symbol table while recognizing identifiers. It includes steps to identify tokens, keywords, operators and delimiters and analyze the code by printing the tokens and their types to the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Dr. N. G. P. Institute of Technology Reg.

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:

Step 1: Start the program.


Step 2: Create a function to identify the given character as token, declared from a to z, A to Z,
0 to 9 and underscore.
Step 3: Declare another function to identify the keywords by using “for”, if the given character is
between 32 it takes as keywords otherwise not.
Step 4: Drawing a table by using linked list concept and free the space of block for another character.
Step 5: For operators using “switch case” and identifying the given symbol is operator or not.
Step 6: All the above steps are repeated until the code is done.
Step 7: In the main program initializing the code to 1000 and getting the code, passing code to function.
Step 8: At last printing the result as table.
Step 9: Stop the program.

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
int isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}

int isConstant(char* str) {


int i = 0;
while (str[i] != '\0') {
if (str[i] < '0' || str[i] > '9')
return 0;
i++;
}
return 1;
}

int isIdentifierChar(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
}

int isKeyword(char* str) {


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"
1
Dr. N. G. P. Institute of Technology Reg. No: 710721104116

};
int i;
for (i = 0; i < 32; i++) {
if (strcmp(str, keywords[i]) == 0)
{
return 1;
}
}
return 0;
}

void analyzeCode(char* code) {


int length = strlen(code);
int i = 0;
int j;
int tokenLength;
char* token;
printf("| | |\n");
printf("| Token | Type |\n");
printf("| | |\n");
while (i < length) {

if (code[i] == ' ' || code[i] == '\t' || code[i] == '\n') {


i++;
continue;
}

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

printf("| + | Operator |\n");


break;
case '-':
printf("| - | Operator |\n");
break;
case '*':
printf("| * | Operator |\n");
break;
case '/':
printf("| / | Operator |\n");
break;
case '=':
printf("| = | Operator |\n");
break;
case '(':
printf("| ( | Delimiter |\n");
break;
case ')':
printf("| ) | Delimiter |\n");
break;
case '{':
printf("| { | Delimiter |\n");
break;
case '}':
printf("| } | Delimiter |\n");
break;
case ';':
printf("| ; | Delimiter |\n");
break;
default:
printf("| %c | Unknown |\n", code[i]);
break;
}
i++;
}
}
int main() {
char code[1000];
printf(“THIYAGARAJ K-710721104116”);
printf("\nEnter the code:");
fgets(code, sizeof(code), stdin);
analyzeCode(code);
printf("|--------------| ------------ |\n");
getch();
}

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

You might also like