Lab Report Merge (G.rabbi)
Lab Report Merge (G.rabbi)
Student Details
Name ID
GOLAM RABBI 221902328
1.
2. OBJECTIVES/AIM
3. IMPLEMENTATION
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_IDENTIFIER_LENGTH 50
return 0;
return 0;
return 1;
int main() {
char identifier[MAX_IDENTIFIER_LENGTH];
scanf("%s", identifier);
if (isIdentifier(identifier))
else
}
• Write a program to recognize constants
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_CONSTANT_LENGTH 50
int dotCount = 0;
return 0;
if (str[i] == '.') {
dotCount++;
if (dotCount > 1)
return 0;
return 1;
}
int main() {
char constant[MAX_CONSTANT_LENGTH];
scanf("%s", constant);
if (isConstant(constant))
else
return 0;
#include <string.h>
#include <ctype.h>
#define MAX_IDENTIFIER_LENGTH 50
char keywords[32][10] = {
};
for (int i = 0; i < 32; i++) {
if (strcmp(keywords[i], str) == 0)
return 1;
return 0;
return 0;
return 0;
return 1;
int main() {
char token[MAX_IDENTIFIER_LENGTH];
scanf("%s", token);
if (isKeyword(token))
else if (isIdentifier(token))
else
return 0;
}
• Write a program to ignore the comments in the given input source program.
#include <stdio.h>
int main() {
int current_char;
break;
comment_closed = 1;
prev_char = current_char;
if (current_char ==
EOF) break;
} else {
putchar(prev_char);
prev_char = current_char;
if (prev_char != EOF)
putchar(prev_char);
return 0;
}
4. Discussion
In this lab experiment this functions provide essential functionalities for lexical analysis
and preprocessing:
• Comment Ignoring: Removes comments from C source files, aiding in code readability.
Objectives
1. Write a program to recognize comments and count the number of letters in
these comments.
if (comment != NULL) {
total_letters += strlen(comment + 2);
}
line = strtok(NULL, "\n");
}
return total_letters;
}
int main() {
const char *code_example =
"// This is a comment\n"
#include <stdio.h>
#include <string.h>
*standalone_comments = 0;
const char *line = strtok(strdup(code), "\n");
while (line != NULL) {
if (strstr(line, "//") != NULL) {
if (strstr(line, "//") == line) {
(*standalone_comments)++;
} else {
(*inline_comments)++;
}
}
line = strtok(NULL, "\n");
}
}
int main() {
const char *code_example =
"// This is a standalone comment\n"
"printf(\"Hello, World!\\n\"); // This is an inline comment\n";
return 0;
}
Task 3: Find the Number of Lines with Comments
#include <stdio.h>
#include <string.h>
int comment_lines = 0;
const char *line = strtok(strdup(code), "\n");
while (line != NULL) {
return comment_lines;
}
int main() {
const char *code_example =
"// This is a comment\n"
}
Conclusion
We successfully created programs to recognize comments, count the number of
letters in comments, identify the types of comments, and find the number of lines
with comments.
These tools help in analyzing and understanding the comment usage in C code.
#include <stdio.h>
#include <stdbool.h>
int main() {
#include <stdio.h>
#include <stdbool.h>
switch (state) {
case 0: if (str[i] == 'a') state = 0; else if (str[i] == 'b') state = 1; else return false; break;
case 1: if (str[i] == 'b') state = 1; else if (str[i] == 'c') state = 2; else return false; break;
case 2: if (str[i] == 'c') state = 2; else return false; break;
}
}
return state == 2;
}
int main() {
Conclusion
We successfully created programs to recognize strings under the rules "a+bc+" and
"ab+c+".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char stack[MAX];
int top = -1;
void push(char item) {
}
}
char pop() {
char item;
if (top < 0) {
exit(1);
} else {
item = stack[top];
top = top - 1;
return item;
}
}
case '*':
case '/':
return 2;
default:
return 0;
}
}
if (infix[i] == ')') {
infix[i] = '(';
item = infix[i];
if (item == '(') {
push(item);
} else if (item == ')') {
x = pop();
while (x != '(') {
prefix[j++] = x;
x = pop();
}
} else if (isOperator(item)) {
x = pop();
}
push(x);
push(item);
} else {
prefix[j++] = item;
}
}
prefix[j] = '\0';
strrev(prefix);
}
int main() {
char infix[] = "a+(b-c)";
char prefix[MAX];
infixToPrefix(infix, prefix);
printf("%s\n", prefix); // Output: +a-bc
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char stack[MAX];
top = top + 1;
stack[top] = item;
}
char pop() {
char item;
if (top < 0) {
exit(1);
} else {
item = stack[top];
top = top - 1;
return item;
}
switch (symbol) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
int i, j = 0;
char item, x;
push(item);
} else if (item == ')') {
x = pop();
while (x != '(') {
postfix[j++] = x;
x = pop();
}
} else if (isOperator(item)) {
x = pop();
}
push(x);
push(item);
} else {
postfix[j++] = item;
}
}
postfix[j] = '\0';
}
int main() {
char infix[] = "a+(b-c)";
char postfix[MAX];
infixToPostfix(infix, postfix);
printf("%s\n", postfix); // Output: abc-+
return 0;
}
Conclusion
We successfully created programs to convert the infix expression a+(b-c) to
prefix and postfix operators in C.
Objectives
1. Implement a C program to calculate the first and follow sets for a given grammar.
Code
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 10
char production[MAX][MAX], first[MAX], follow[MAX];
int count, n = 0, m = 0;
void find_follow(char);
void add_to_array(char[], char);
int main() {
int i, choice;
char c, ch;
n = 0;
}
find_follow(production[0][0]);
printf("Follow(%c) = { $ ", production[0][0]);
for (i = 0; i < m; i++) {
m = 0;
}
return 0;
}
void find_first(char c, int q1, int q2) {
int j;
if (!(isupper(c))) {
add_to_array(first, c);
return;
}
else
find_first(production[q1][q2], q1, q2 + 1);
} else if (!isupper(production[j][2])) {
add_to_array(first, production[j][2]);
} else {
find_first(production[j][2], j, 3);
}
}
}
}
void find_follow(char c) {
int i, j;
if (production[0][0] == c) {
add_to_array(follow, '$');
}
for (i = 0; i < count; i++) {
for (j = 2; j < strlen(production[i]); j++) {
if (production[i][j] == c) {
if (production[i][j + 1] != '\0') {
find_first(production[i][j + 1], 0, 0);
for (int k = 0; k < n; k++) {
if (first[k] != '#') {
add_to_array(follow, first[k]);
}
}
}
}
}
}
return;
}
}
array[n++] = value;
}
Conclusion
We implemented a C program to calculate the first and follow sets for a given
grammar.
The program effectively computes and displays the first and follow sets for the
provided grammar rules.
Lab Report 6: LL(2) Parsing Construction
Objectives
1. Write a C program for constructing an LL(2) parser.
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char production[MAX][MAX];
int count;
} NonTerminal;
typedef struct {
NonTerminal nonTerminals[MAX];
int count;
} Grammar;
Grammar grammar;
if (strcmp(grammar.nonTerminals[i].production[0], lhs) == 0) {
strcpy(grammar.nonTerminals[i].production[grammar.nonTerminals[i].count], rhs);
grammar.nonTerminals[i].count++;
return;
}
}
strcpy(grammar.nonTerminals[grammar.count].production[0], lhs);
strcpy(grammar.nonTerminals[grammar.count].production[1], rhs);
grammar.nonTerminals[grammar.count].count = 2;
grammar.count++;
char stack[MAX];
int top = -1;
stack[++top] = '$';
stack[++top] = grammar.nonTerminals[0].production[0][0];
int i = 0;
} else {
int found = 0;
for (int j = 0; j < grammar.count; j++) {
if (stack[top] == grammar.nonTerminals[j].production[0][0]) {
for (int k = 1; k < grammar.nonTerminals[j].count; k++) {
if (grammar.nonTerminals[j].production[k][0] == input[i] ||
is_terminal(grammar.nonTerminals[j].production[k][0])) {
top--;
}
found = 1;
break;
}
}
if (found) break;
}
}
if (!found) {
int main() {
grammar.count = 0;
add_production("S", "aA");
add_production("A", "b");
char input[MAX];
printf("Enter the input string: ");
scanf("%s", input);
strcat(input, "$");
parse(input);
return 0;
}
Conclusion
We successfully created programs to recognize comments, count the number of
letters in comments, identify the types of comments, and find the number of lines
with comments.
These tools help in analyzing and understanding the comment usage in C code.
Lab Report 8 : Compute FIRST for the
regular expression a(alb)"a
Objectives:
1. Understand the concept of FIRST set in the context of regular expressions.
2. Implement a C program to compute the FIRST set for a given regular expression.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
if (regex[0] == 'a') {
if (regex[1] == '(' && regex[2] == 'a' && regex[3] == '|' && regex[4] == 'b' && regex[5] ==
')' && regex[6] == '*') {
}
}
int main() {
computeFirstSet(regex1);
computeFirstSetForStar(regex2);
return 0;
}
Conclusion:
In this lab exercise, we explored the concept of FIRST sets for two regular expressions. The
FIRST set helps identify the first symbols that can appear in a string derived from a given
regular expression. By implementing the FIRST set computation in C, we demonstrated how
to analyze regular expressions programmatically to determine their initial symbols. This
exercise enhances understanding of parsing and pattern recognition techniques essential in
compiler design and language processing tasks.