cd-lab-ans
cd-lab-ans
cd-lab-ans
#include <stdio.h>
#include <ctype.h>
if (isalpha(code[i])) {
// Collect keyword or identifier
j = 0;
while (isalnum(code[i])) buffer[j++] = code[i++];
buffer[j] = '\0';
int isKeyword = 0;
for (int k = 0; k < 5; k++) {
if (strcmp(buffer, keywords[k]) == 0) {
printf("Keyword: %s\n", buffer);
isKeyword = 1;
break;
}
}
if (!isKeyword) printf("Identifier: %s\n", buffer);
} else if (isdigit(code[i])) {
// Collect number
j = 0;
while (isdigit(code[i])) buffer[j++] = code[i++];
buffer[j] = '\0';
printf("Number: %s\n", buffer);
} else if (strchr("+-*/=;", code[i])) {
// Operators and symbols
printf("Operator/Symbol: %c\n", code[i++]);
} else {
// Unknown character
printf("Unknown: %c\n", code[i++]);
}
}
}
int main() {
char code[256];
printf("Enter code: ");
fgets(code, 256, stdin);
analyze(code);
return 0;
3rd a)Program
#include <stdio.h>
#include <ctype.h>
int main() {
char expression[100];
printf("Enter an arithmetic expression: ");
fgets(expression, sizeof(expression), stdin);
if (isValidExpression(expression)) {
printf("The expression is valid.\n");
} else {
printf("The expression is invalid.\n");
}
return 0;
}
3rd b)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char variable[100];
return 0;
}
3rd c)
#include <stdio.h>
#include <string.h>
int main() {
char input[100];
if (isValidIf(input)) {
printf("Valid 'if' syntax.\n");
} else if (isValidFor(input)) {
printf("Valid 'for' syntax.\n");
} else if (isValidWhile(input)) {
printf("Valid 'while' syntax.\n");
} else {
printf("Invalid control structure syntax.\n");
}
return 0;
}
3rd d)Program
#include <stdio.h>
int main() {
char operator;
float num1, num2;
return 0;
}
4th Program
#include <stdio.h>
int main() {
int a = 5, b = 10, c, d;
// Simple expression: d = a + b * 2
printf("Generating TAC for the expression: d = a + b * 2\n");
return 0;
}
5th Program
#include <stdio.h>
int main() {
int num;
float decimal;
char letter;
// Type checking
printf("The type of num is: %s\n", (sizeof(num) == sizeof(int)) ? "int" : "unknown");
printf("The type of decimal is: %s\n", (sizeof(decimal) == sizeof(float)) ? "float" : "unknown");
printf("The type of letter is: %s\n", (sizeof(letter) == sizeof(char)) ? "char" : "unknown");
return 0;
}
6th Program
check
lab manual is easy, brothers
7th Program
#include <stdio.h>
#include <string.h>
int main() {
char tac[100];