Compiler Lab
Compiler Lab
Design a lexical analyzer for given language and the lexical analyzer should ignore
redundant spaces, tabs and new lines. It should also ignore comments. 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.
Source Code
main.cpp:
#include <stdio.h>
#include <ctype.h>
#define MAX_IDENTIFIER_LENGTH 50
iput.txt:
int main() {
FILE *file = fopen("input.txt", "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
tokenize(file);
fclose(file);
return 0;
}
Output
Source Code
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
if (isComment(line)) {
printf("The given line is a comment.\n");
} else {
printf("The given line is not a comment.\n");
}
return 0;
}
Output
Source Code
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main() {
char str[100];
if (match_a(str)) {
printf("The string matches 'a'.\n");
} else if (match_ab_star(str)) {
printf("The string matches 'a*b+'.\n");
} else if (match_abb(str)) {
printf("The string matches 'abb'.\n");
} else {
printf("The string does not match any of the given patterns.\n");
}
return 0;
}
Output